<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-26 13:57:06
 -->
<!--
  <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="appName">
          <limitInput :inputWidth="402" :inputValue.sync="ruleForm.appName" :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="secret">
          <limitInput :inputWidth="402" :inputValue.sync="ruleForm.secret" :holder="'请输入secret'" :getByType="'word'" :maxLength="80"> </limitInput>
        </el-form-item>
        <el-form-item label="关联商户" prop="brand">
          <el-select class="w-402" v-model="ruleForm.brand" placeholder="全部品牌" @change="getTableList">
            <!-- <el-option label="全部品牌" value=""></el-option> -->
            <el-option v-for="item in brandOptions" :key="item.enterpriseId" :label="item.enterpriseName" :value="item.enterpriseId"> </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';
import { emojiArr } from '@/utils/emoji';
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: {
        materialId: '',
        agentid: '',
        appName: '',
        secret: '',
        brand: ''
      },
      rules: {
        appName: [{ required: true, message: '请输入小程序名称', trigger: 'blur' }],
        agentid: [{ required: true, message: '请输入agentid', trigger: 'blur' }],
        secret: [{ required: true, message: '请输入secret', trigger: 'blur' }],
        brand: [{ required: true, message: '请选择关联商户', trigger: 'change' }]
      },
      emojiList: emojiArr,
      brandOptions: [] //品牌
    };
  },
  methods: {
    /**
     * 获取品牌
     */
    getBrandData() {
      const that = this;
      postRequest('/haoban-manage3-web/wx-enterprise-list', {})
        .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 = {
        categoryId: that.categoryId,
        materialType: 3,
        link: that.ruleForm.link,
        materialId: that.ruleForm.materialId,
        materialTitle: that.ruleForm.materialTitle,
        imgUrl: that.ruleForm.imgUrl,
        secret: that.ruleForm.secret
      };
      postRequest(that.ruleForm.materialId ? '/haoban-manage3-web/material-edit' : '/haoban-manage3-web/material-add', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
          } else {
            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.ruleForm = JSON.parse(JSON.stringify(newData));
      }
    }
  },
  mounted() {
    const that = this;
    if (Object.keys(that.editRow).length) {
      that.ruleForm = JSON.parse(JSON.stringify(that.editRow));
    }
  }
};
</script>
<style lang="less" 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 {
      /deep/ .el-textarea__inner {
        height: 273px;
      }
    }
  }
  .emoji-img {
    position: absolute;
    left: 10px;
    bottom: 10px;
  }
}
.el-textarea {
  /deep/ .el-textarea__inner {
    height: 273px;
  }
}
</style>
