<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-23 17:07:33
 -->
<!--
  <set-visiable :brandId="brandId"  @closeStore="closeStore" @submitStore="submitStore"></set-visiable>
  import setVisiable from '@/components/app/customer/set-visiable.vue';
 -->
<template>
  <el-dialog :title="!!editRow.templateId ? '可见范围' : '可见范围'" :visible.sync="dialogVisible" width="873px" :before-close="handleClose">
    <div class="">
      <div class="el-form-item el-form-item--large">
        <label for="name" class="el-form-item__label" style="width: 100px; cursor: pointer;">
          选择范围
        </label>
        <div class="el-form-item__content" style="margin-left: 100px;">
          <el-radio v-model="formData.sendRange" label="0" @change="changeRange">所有门店</el-radio>
          <el-radio class="m-l-40 m-r-8" v-model="formData.sendRange" label="1" @change="changeRange">部分分组</el-radio>
          <div v-if="formData.sendRange == 1" class="w-240 inline-block item-cell-select">
            <gic-select-group :brandId="brandId" class="m-l-10" :width="213" :selectData="formData.selectList" @checkGroupIds="checkGroupIds"> </gic-select-group>
          </div>
          <el-radio class="m-l-40 m-r-8" v-model="formData.sendRange" label="2" @change="changeRange">部分门店</el-radio>
          <div v-if="formData.sendRange == 2" class="w-240 inline-block  item-cell-select">
            <gic-select-store :brandId="brandId" :selectData="formData.selectList" @returnStores="returnStores"> </gic-select-store>
          </div>
          <div v-if="(formData.sendRange == '1' || formData.sendRange == '2') && !formData.selectList.length" class="el-form-item__error">请选择发送范围</div>
        </div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toCancel">取消</el-button>
      <el-button type="primary" :loading="loadingBtn" @click="submitForm('ruleForm')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import gicSelectStore from '@/components/common/gic-select-store.vue';
import gicSelectGroup from '@/components/common/gic-select-group.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: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    editRow: {
      type: Object,
      default() {
        return {};
      }
    },
    categoryId: {
      type: Object,
      default() {
        return '';
      }
    }
  },
  components: {
    gicSelectGroup,
    gicSelectStore
  },
  data() {
    return {
      editPersion: localStorage.getItem('userInfos') && JSON.parse(localStorage.getItem('userInfos')).staffDTO ? JSON.parse(localStorage.getItem('userInfos')).staffDTO.staffName : '',
      wxEnterpriseRelatedId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      loading: false,
      dialogVisible: true,
      formData: {
        sendRange: '0',
        selectList: []
      },
      loadingBtn: false
    };
  },
  methods: {
    /**
     * 改变选择范围
     */
    changeRange(e) {
      const that = this;
      that.formData.selectList = [];
    },
    /**
     * 选择门店
     */
    returnStores: function(nodes) {
      const that = this;
      that.formData.selectList = JSON.parse(JSON.stringify(nodes));
    },
    /**
     * 选择门店分组
     */
    checkGroupIds: function(nodes) {
      const that = this;
      that.formData.selectList = JSON.parse(JSON.stringify(nodes));
    },
    toCancel() {
      const that = this;
      that.$emit('closeStore');
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeStore');
    },
    submitForm: _debounce(function(formName) {
      const that = this;
      if ((that.formData.sendRange == '1' || that.formData.sendRange == '2') && !that.formData.selectList.length) {
        return false;
      }
      that.loadingBtn = true;
      that.postSave();
    }, 300),
    postSave() {
      const that = this;
      const data = {
        enterpriseId: that.brandId,
        templateId: that.$route.query.templateId,
        selectType: that.formData.sendRange,
        groupIds: that.formData.sendRange == 1 ? that.formData.selectList.map(ele => ele.storeGroupId).join(',') : '',
        storeIds: that.formData.sendRange == 2 ? that.formData.selectList.join(',') : ''
      };
      postRequest('/haoban-app-customer-web/memberGroup/save-template-store', data)
        .then(res => {
          let resData = res.data;
          that.loadingBtn = false;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$emit('submitStore');
          } else {
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.loadingBtn = false;
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      that.loading = true;
      let storeGroups = [];
      let filterStoreIds = [];
      let clerkIds = [];
      that.conditionObj.storeGroup.forEach(ele => {
        if (ele.type == 1) {
          clerkIds.push(ele.employeeClerkId);
        }
        if (ele.type == 2) {
          storeGroups.push(ele.groupId);
        }
        if (ele.type == 3) {
          filterStoreIds.push(ele.storeId);
        }
      });
      if (!that.conditionObj.dateRange) {
        that.conditionObj.dateRange = [];
      }
      let para = {
        search: that.searchInput || '', // 搜索字段
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize, // 一页显示个数
        gicEnterpriseId: that.brandId
        // taskType: 1 //0不良评价，1话务任务
      };
      postRequest('/haoban-app-customer-web/memberGroup/search-store', para)
        .then(res => {
          let resData = res.data;
          that.loading = false;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.list || [];
            that.total = resData.result.pageInfo.total;
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.loading = false;
          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>
.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>
