<template>
  <div>
    <el-form
      ref="nextNode"
      :model="nextNode"
      class="demo-dynamic"
      label-width="120px"
      :label-position="formLabelPosition"
    >
      <el-form-item prop="nextCurrentOrgObj" label="本单位">
        <SelectUser
          :nextUser="nextCurrentOrgObjSelect"
          multiple
          mix
          :showTooltip="showTooltip"
          :disableds="currentOrgDisabledObjId"
          @change="disposeAppUnit($event, 'nextCurrentOrgObjSelect')"
          :types="currentOrgSelectorTabs"
          :params="currentOrgSelectorParams"
        />
      </el-form-item>
      <el-form-item v-if="showOther" prop="nextOtherOrgObj" label="外单位">
        <SelectUser
          :nextUser="nextOtherOrgObjSelect"
          multiple
          mix
          :showTooltip="showTooltip"
          :disableds="otherOrgDisabledObjId"
          :where="{ filid: 'all',roleid :selectorParams.roleid }"

          @change="disposeAppUnit($event, 'nextOtherOrgObjSelect')"
          :types="foreignOrgSelectorTabs"
          :params="foreignOrgSelectorParams"
        />
      </el-form-item>
    </el-form>
    <div style="text-align: right">
      <el-button type="primary"  @click="subMit"> 确定 </el-button>
      <el-button @click="quit" > 取消 </el-button>
    </div>
  </div>
</template>

<script>
import SelectUser from '../selectUser.vue';
import util from 'eoss-ui/src/utils/util';
import {
  toFreeTaskCircularRead,
  freeTaskCircularRead
} from 'eoss-ui/src/config/api';
export default {
  components: { SelectUser },
  props: {
    formLabelPosition: {
      type: String,
      default: 'left'
    },
    showTooltip: { type: Boolean, default: true },
    selectorParams: { type: Object, default: () => {return {}} },
    params: {
      type: Object,
      default: () => {}
    },
    appId: {
      type: String,
      default: ''
    },
    showOther: { type: Boolean, default: true }
  },
  data() {
    return {
      currentOrgDisabledObjId: [],
      otherOrgDisabledObjId: [],
      nextCurrentOrgObjSelect: [],
      currentNodeIsCircularReadNode: false,
      simpleTips: true,
      subProcessColumns: [],
      nextOtherOrgObjSelect: [],
      nextNode: {},
      currentOrgSelectorParams: {
        filid: JSON.parse(util.getStorage('mainConfig')).userModel.orgId,only_filid:true
      },
      currentOrgSelectorTabs: ['employee', 'persongroup'],
      foreignOrgSelectorTabs: ['enterprise', 'filgroup'],
      foreignOrgSelectorParams: {enterprise:{filid: 'all', roleid: '', nofilid:sessionStorage.getItem('orgId')},filgroup:{filid: 'all', roleid: '', nofilid: sessionStorage.getItem('orgId')}  },
      loading: null
    };
  },
  mounted() {
    console.log('selectorParams', this.selectorParams);
    this.foreignOrgSelectorParams.enterprise.roleid = this.foreignOrgSelectorParams.filgroup.roleid = this.currentOrgSelectorParams.roleid =
      this.selectorParams.roleid;
    this.getFreeTaskCircularRead();
  },
  methods: {
    quit() {
      this.$emit('cancel');
      this.$parent.$emit('cancel');
    },
    subMit() {
      if (!this.nextNode.nextCurrentOrgObj && !this.nextNode.nextOtherOrgObj)
        return this.$message.warning('请选择下步办理对象!');
      let param = {
        url: freeTaskCircularRead,
        data: {
          ...this.params,
          appId: this.appId || this.params.appId,
          nextCurrentOrgObj: this.nextNode.nextCurrentOrgObj,
          nextOtherOrgObj: this.nextNode.nextOtherOrgObj || ''
        },
        method: 'post'
      };
      this.loading = util.loading(this.$loading, '加载中...');
      util.ajax(param).then((res) => {
        this.loading.close();
        if (res.rCode == 0 || res.status == 'success') {
          if (!this.simpleTips) {
            this.$alert('提交成功', '', {
              confirmButtonText: '确定',
              type: 'success',
              callback: (action) => {
                this.$parent.$emit('success');
              }
            });
          } else {
            this.$parent.$emit('success');
          }
        } else {
          this.$message.error(
            res.message || res.msg || '系统错误，请联系管理员！'
          );
        }
      });
    },
    getFreeTaskCircularRead() {
      let param = {
        url: toFreeTaskCircularRead,
        params: {...this.params, appId: this.appId || this.params.appId }
      };
      this.loading = util.loading(this.$loading, '加载中...');
      util.ajax(param).then((res) => {
        this.loading.close();
        if (res.rCode == 0 || res.status == 'success') {
          this.simpleTips = res.results.simpleTips;
          this.currentOrgDisabledObjId =
            res.results.disabledCurrentOrgUserId.split(',');
          this.otherOrgDisabledObjId =
            res.results.disabledForeignOrgId.split(',');
        }
      });
    },
    getsubProcessColumns() {
      let data = [];
      let obj = { name: '[部门员工]-' };
      if (this.nextCurrentOrgObjSelect.length > 0) {
        this.nextCurrentOrgObjSelect.map((x) => {
          if (x.stype == 'employee') {
            obj.name = !obj.value
              ? obj.name + x.showname
              : obj.name + ',' + x.showname;
            obj.value = obj.value
              ? obj.value + ',' + x.showid
              : 'employee-' + x.showid;
          } else {
            data.push({
              name: '[部门]' + x.showname,
              value: 'department-' + x.showid
            });
          }
        });
      }
      obj.value && data.push(obj);
      if (this.nextOtherOrgObjSelect.length > 0) {
        this.nextOtherOrgObjSelect.map((x) => {
          data.push({
            name: '[企业]' + x.showname,
            value: 'enterprise-' + x.showid
          });
        });
      }
      this.subProcessColumns = data;
      // return data;
    },
    // 单位选择完回调
    disposeAppUnit(res, type) {
      let label = type.substring(0, type.lastIndexOf('Select'));
      this.nextNode[label + 'Name'] = '';
      res.options && (this[type] = res.options);
      let obj = {};
      // if (typeof res.mixList == 'array')
      res.mixList && (this.nextNode[label] = res.mixList.join('|'));
      res.options.map((item) => {
        if (obj[item.stype]) {
          obj[item.stype].push(item.showname);
        } else {
          obj[item.stype] = [item.showname];
        }
      });
      for (let key in obj) {
        if (
          key === 'enterprise' &&
          this.nextNode[label + 'Name'].indexOf('[企业]') == -1
        ) {
          this.nextNode[label + 'Name'] = this.nextNode[label + 'Name']
            ? this.nextNode[label + 'Name'] +
              '|' +
              '[企业]-' +
              obj[key].join(',')
            : '' + '[企业]-' + obj[key].join(',');
        }
        if (
          key === 'department' &&
          this.nextNode[label + 'Name'].indexOf('[部门]') == -1
        ) {
          this.nextNode[label + 'Name'] = this.nextNode[label + 'Name']
            ? this.nextNode[label + 'Name'] +
              '|' +
              '[部门]-' +
              obj[key].join(',')
            : '' + '[部门]-' + obj[key].join(',');
        }
        if (
          key === 'employee' &&
          this.nextNode[label + 'Name'].indexOf('[部门员工]') == -1
        ) {
          this.nextNode[label + 'Name'] = this.nextNode[label + 'Name']
            ? this.nextNode[label + 'Name'] +
              '|' +
              '[部门员工]-' +
              obj[key].join(',')
            : '' + '[部门员工]-' + obj[key].join(',');
        }
      }
      // this.nextNode[label + 'Name'] = obj.enterprise?;
      if (!this.currentNodeIsCircularReadNode) {
        this.nextNode[label + 'SerialId'] = res.nextUser.join(',');
        this.nextNode[label + 'Json'] = JSON.stringify(res.options);
      }
      this.getsubProcessColumns();
    }
  }
};
</script>

<style></style>
