<!--
 * @Descripttion:素材库-分配企业
 * @version: 1.0.0
 * @Author: 阿羽
 * @Date: 2021-04-12 15:57:24
 * @LastEditors: 阿羽
 * @LastEditTime: 2021-04-12 15:57:24
 -->
<!--
  <distribution-enterprise :id="enterpriseId" v-if="showDistribution"  @closeText="closeText" @submitText="submitText"></distribution-enterprise>
<script>
import distributionEnterprise from '@/components/set/distribution-enterprise.vue';
export default {
data() {
    return {
     enterpriseId:'',
     showDistribution: false
    };
},
 methods: {
     closeText() {
      const that = this;
      that.showDistribution = false;
      that.editRow = {};
    },
    submitText() {
      const that = this;
      that.showDistribution = false;
      that.editRow = {};
      that.$emit('refreshTable');
    },
 }
 -->
<template>
  <el-dialog title="分配企业" :visible.sync="dialogVisible" width="490px" class="distribution-enterprise">
    <div>
      <el-form :model="form" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item>
          <p class="check-all">
            <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选</el-checkbox>
          </p>
          <el-form-item prop="checkedList">
            <el-checkbox-group v-model="form.checkedList" name="checkedList" @change="handleCheckedListChange">
              <el-checkbox v-for="(opt, index) in options" name="checkedList" :label="opt.id" :key="index">
                <p class="name">{{ opt.name }}</p>
                <p class="subname">{{ opt.subname }}</p>
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </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 { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
const options = [
  { id: 1, name: '上海', subname: 'xiaosh' },
  { id: 2, name: '北京', subname: 'xiaosh' },
  { id: 3, name: '广州', subname: 'xiaosh' },
  { id: 4, name: '北京', subname: 'xiaosh' }
];
export default {
  props: {
    id: {
      type: String,
      default: ''
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: true,
      ruleForm: {
        enterpriseId: ''
      },
      rules: {
        checkedList: [{ type: 'array', required: true, message: '请选择企业', tigger: 'change' }]
      },
      checkAll: false,
      form: { checkedList: [] },
      options,
      isIndeterminate: false
    };
  },
  methods: {
    toCancel() {
      this.dialogVisible = false;
    },
    submitForm: _debounce(function(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      showMsg.showmsg('操作成功', 'success');
      this.$emit('submitText');
      return;
      const that = this;
      const data = { ...that.ruleForm };
      postRequest('/haoban-manage3-web/', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$emit('submitText');
          } else {
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    handleCheckAllChange(val) {
      if (val) {
        this.options.map(item => {
          this.form.checkedList.push(item.id);
        });
      } else {
        this.form.checkedList = [];
      }
      this.isIndeterminate = false;
    },
    handleCheckedListChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.options.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.options.length;
    },
    getOptions() {
      this.$refs['ruleForm'].resetFields();
      // console.log(this.id);
    }
  },
  watch: {
    show(newV, oldV) {
      // console.log(newV);
      this.dialogVisible = true;
    }
  },
  mounted() {
    const that = this;
    if (that.id) {
      that.$nextTick(() => {
        this.getOptions();
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.distribution-enterprise {
  >>> .el-dialog {
    height: 429px;
  }
  >>> .el-dialog__header {
    padding: 16px 0 10px 20px;
    .el-dialog__title {
      display: block;
      line-height: 25px;
    }
  }
  >>> .el-dialog__body {
    padding: 12px 16px 0 17px;
    .check-all {
      width: 100%;
      height: 40px;
      box-sizing: border-box;
      background: #f5f7fa;
      padding-left: 28px;
      margin-bottom: 18px;
    }
    .el-checkbox-group {
      height: 248px;
      box-sizing: content-box;
      overflow: scroll;
      padding-left: 28px;
      .el-checkbox {
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;
        margin: 0 0 20px;
        .name {
          font-size: 14px;
          color: #303133;
          line-height: 14px;
          margin-bottom: 6px;
        }
        .subname {
          font-size: 14px;
          color: #909399;
          line-height: 20px;
        }
      }
    }
  }
  >>> .el-dialog__footer {
    padding-top: 16px;
    padding-bottom: 20px;
  }
}
</style>
<style lang="scss"></style>
