<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-24 11:30:49
 -->
<!--
  <secret-set :categoryId="categoryId" @closeText="closeText" @submitText="submitText"></secret-set>
  import secretSet from '@/components/set/secret-set.vue';
 -->
<template>
  <el-dialog :title="!!editRow.materialId ? '编辑' : '新建'" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
    <div class="">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="小程序名称" prop="secretName">
          <limitInput :inputWidth="402" :inputValue.sync="ruleForm.secretName" :holder="'请输入小程序名称'" :getByType="'word'" :maxLength="20"> </limitInput>
        </el-form-item>
        <!-- <el-form-item label="agentid" prop="agentid">
          <limitInput :inputWidth="402" :inputValue.sync="ruleForm.agentid" :holder="'请输入agentid'" :getByType="'word'" :maxLength="80"> </limitInput>
        </el-form-item> -->
        <el-form-item label="secret" prop="secretVal">
          <limitInput :inputWidth="402" :inputValue.sync="ruleForm.secretVal" :holder="'请输入secret'" :getByType="'word'" :maxLength="80"> </limitInput>
        </el-form-item>
        <el-form-item label="关联商户" prop="enterpriseId">
          <el-select class="w-402" v-model="ruleForm.enterpriseId" placeholder="全部品牌">
            <el-option class="mult-line-select" v-for="item in brandOptions" :key="item.enterpriseId" :label="item.enterpriseName" :value="item.enterpriseId">
              <p class="line-20">{{ item.enterpriseName }}</p>
              <div class="line-20 font-12 color-909399">{{ item.brandName || '--' }}</div>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toCancel">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import singleUpload from '@/components/single-upload.vue';
import limitInput from '@/components/limit-input.vue';
import limitTextarea from '@/components/limit-textarea.vue';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    editRow: {
      type: Object,
      default() {
        return {};
      }
    },
    categoryId: {
      type: Object,
      default() {
        return '';
      }
    }
  },
  components: {
    limitInput,
    limitTextarea,
    singleUpload
  },
  data() {
    return {
      editPersion: localStorage.getItem('userName'),
      dialogVisible: true,
      ruleForm: {
        secretName: '',
        secretVal: '',
        enterpriseId: '',
        secretId: '',
        enterpriseName: ''
      },
      rules: {
        secretName: [{ required: true, message: '请输入小程序名称', trigger: 'blur' }],
        secretVal: [{ required: true, message: '请输入secret', trigger: 'blur' }],
        enterpriseId: [{ required: true, message: '请选择关联商户', trigger: 'change' }]
      },
      brandOptions: [] //品牌
    };
  },
  methods: {
    /**
     * 获取品牌
     */
    getBrandData() {
      const that = this;
      postRequest('/haoban-manage3-web/wx-enterprise-list', { filteRight: 1 })
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!!resData.result && !!resData.result.length) {
              that.brandOptions = resData.result;
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    toCancel() {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
    },
    submitForm: _debounce(function(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = {
        secretId: that.ruleForm.secretId,
        secretName: that.ruleForm.secretName,
        enterpriseId: that.ruleForm.enterpriseId,
        memberSecret: that.ruleForm.secretVal
      };
      postRequest('/haoban-manage3-web/wx-enterprise-member-secret-set', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
          } else {
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    editRow(newData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.$nextTick(() => {
          that.ruleForm = JSON.parse(JSON.stringify(newData));
        });
      }
    }
  },
  mounted() {
    const that = this;
    that.getBrandData();
    if (Object.keys(that.editRow).length) {
      that.$nextTick(() => {
        that.ruleForm = JSON.parse(JSON.stringify(that.editRow));
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.w-402 {
  width: 402px;
}
.m-b-20 {
  margin-bottom: 20px;
}
.p-l-18 {
  padding-left: 18px;
}
.material-content {
  position: relative;
  .material-body {
    /*  resize: none;
    width: 440px;
    height: 273px;
    border-radius: 2px;
    border: 1px solid rgba(196, 198, 207, 1); */
    .el-textarea {
      >>> .el-textarea__inner {
        height: 273px;
      }
    }
  }
  .emoji-img {
    position: absolute;
    left: 10px;
    bottom: 10px;
  }
}
.el-textarea {
  >>> .el-textarea__inner {
    height: 273px;
  }
}
</style>
