<template>
  <el-form :model="groupData" :rules="groupDataRules" ref="groupData" label-width="110px" class="group-detail">
    <el-form-item label="审核组名称" prop="auditGroupName">
      <el-input
        placeholder="请输入内容"
        maxlength="20"
        show-word-limit
        style="width: 481px"
        v-model.trim="groupData.auditGroupName"></el-input>
    </el-form-item>
    <el-form-item label="选择管理员" prop="auditPerson">
      <dm-transfer
        v-model="groupData.auditPerson"
        :titles="['全选', '已选']"
        :button-texts="['移除', '转入']"
        :props="{label: 'staffName', key: 'staffId'}"
        :render-content="render"
        :data="adminList">
      </dm-transfer>
    </el-form-item>
    <el-form-item style="margin-top: 50px">
      <el-button type="primary" :loading="loading" @click="onSave">{{ groupData.auditGroupId ? '保存' : '确认新建' }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import getStrLen from '@/common/js/strlen';
import fetch from '@/api/review';
const { auditGroupDetail, editAuditGroup, addAuditGroup, getAdminList } = fetch;

export default {
  name: 'CreateAuditGroup',
  data() {
    return {
      loading: false,
      adminList: [],
      groupData: {
        auditGroupName: '', // 审核组名称
        auditPerson: [], // [{staffId:1111,staffName:"dsds",roleName:"sdd",roleId:sdsd}]
        wxEnterpriseId: '',
        auditGroupId: ''
      },
      groupDataRules: {
        auditGroupName: [ { required: true, message: '请输入审核组名称', trigger: 'blur' } ],
        auditPerson: [ { required: true, message: '请选择管理员', trigger: 'blur' } ]
      },
      render(n, options) {
        const str = `${options.staffName} (${options.roleName})`;
        {
          if (getStrLen.getZhLen(str) <= 10) {
            return <span>{str}</span>;
          } else {
            return <el-tooltip placement="top">
              <p slot="content">{str}</p>
              <span>{str}</span>
            </el-tooltip>;
          }
        }
      }
    };
  },
  created() {
    const { auditGroupId } = this.$route.query;
    const haobanUser = JSON.parse(localStorage.getItem('haoBanUser') || '{}');
    this.groupData.wxEnterpriseId = haobanUser.wxEnterpriseId;
    this.$emit('showTab', '/audit-group-list');
    if (auditGroupId) {
      this.getGroupDetail(auditGroupId);
    }
    this.getAdminList();
  },
  methods: {
    getAdminList() {
      const { wxEnterpriseId } = this.groupData;
      getAdminList({ wxEnterpriseId }).then(res => {
        this.adminList = res.result || [];
      });
    },
    getGroupDetail(auditGroupId) {
      auditGroupDetail({ auditGroupId }).then(res => {
        const { auditGroupName, auditGroupId, auditPerson } = res.result || {};
        Object.assign(this.groupData, { auditGroupName, auditGroupId });
        if (Array.isArray(auditPerson)) {
          this.groupData.auditPerson = auditPerson.map(el => el.staffId);
        }
      });
    },
    // 新建、编辑受审组的保存
    onSave() {
      this.$refs.groupData.validate(vali => {
        if (!vali) return;
        const { auditGroupName, auditPerson, auditGroupId, wxEnterpriseId } = this.groupData;
        const params = {
          auditGroupName,
          wxEnterpriseId,
          auditPerson: []
        };
        params.auditPerson = this.adminList.filter(el => auditPerson.some(item => item == el.staffId));
        params.auditPerson = JSON.stringify(params.auditPerson);
        this.loading = true;
        if (auditGroupId) {
          params.auditGroupId = auditGroupId;
          editAuditGroup(params).then(succ.bind(this, '保存成功！')).finally(() => this.loading = false);
        } else {
          addAuditGroup(params).then(succ.bind(this, '新建成功！')).finally(() => this.loading = false);
        }
        function succ(message) {
          this.$message.success(message);
          this.$router.go(-1);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.group-detail {
  padding: 30px;
  min-height: 100%;
  background-color: #fff;
}
/deep/.el-transfer-panel__body, /deep/.el-transfer-panel__list {
  height: 264px;
}
</style>
