<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-16 15:23:15
 -->
<!--
  <add-template :brandId="brandId"  @closeTemplate="closeTemplate" @submitTemplate="submitTemplate"></add-template>
  import addTemplate from '@/components/set/add-template.vue';
 -->
<template>
  <el-dialog :title="!!editRow.templateId ? '编辑模板' : '新建模板'" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
    <div class="">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="模板名称" prop="templateName">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.templateName" :holder="'请输入模板名称'" :getByType="'word'" :maxLength="20"> </limitInput>
        </el-form-item>
        <el-form-item label="会员分组" prop="memberTagGroupIds">
           <el-select v-model="ruleForm.memberTagGroupIds" placeholder="请选择">
            <el-option
              v-for="item in groupListData"
              :key="item.memberTagGroupId"
              :label="item.groupName"
              :value="item.memberTagGroupId">
            </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 limitInput from '@/components/limit-input.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
  },
  data() {
    return {
      wxEnterpriseRelatedId: sessionStorage.getItem('userInfos') ? JSON.parse(sessionStorage.getItem('userInfos')).wxEnterpriseId : '',

      dialogVisible: true,
      groupListData: [],
      ruleForm: {
        templateId: '',
        templateName: '',
        memberTagGroupIds: ''
      },
      rules: {
        templateName: [{ required: true, message: '请输入模板名称', trigger: 'blur' }],
        memberTagGroupIds: [
            { type: 'array', required: true, message: '请至少选择一个会员分组', trigger: 'change' }
          ],
      },

    };
  },
  methods: {

    toCancel() {
      const that = this;
      that.$emit('closeTemplate');
      that.$refs['ruleForm'].resetFields();
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeTemplate');
      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 = {
        templateId: that.ruleForm.templateId,
        templateName: that.ruleForm.templateName,
        memberTagGroupIds: that.ruleForm.memberTagGroupIds.join(','),
        editPerson: that.editPerson
      };
      postRequest(that.ruleForm.templateId ? '/haoban-app-customer-web/memberGroup/save-group-template' : '/haoban-app-customer-web/memberGroup/save-group-template', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitTemplate');
          } else {
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取会员分组列表
     */
    getGroupList(brandId) {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId,
        pageNum: 1,
        pageSize: 500
      };
      postRequest('/haoban-app-customer-web/memberGroup/member-tag-group-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {

            that.groupListData = resData.result.list || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(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="scss" scoped>
.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>
