import userDialog from "../../../views/user/user/dialog.vue";
import wfTaskUserRangeDialog from '../wfTaskUserRangeDialog.vue'

let moudule = {};
moudule = {
  props: ['visiable', 'multi', 'rows', 'param', 'current_prefix', 'wfInfo'],
  components: {userDialog,wfTaskUserRangeDialog},
  created() {

  },
  mounted() {
    this.initTableM1();
  },
  data() {
    var that = this;
    return {
      showDialog: true,
      falseValue: false,
      selectMulti: true,
      vxeOption: {},
      showUserDialog: false,
      userRows: [],
      operateIndex: -1,

      rangeTableDatas:[],
      userRangeRows: [],
      showWfTaskUserRangeDialog:false
    };
  },
  methods: {
    openShowWfTaskUserRangeDialog(row,rowIndex, rangeTableDatas){
      this.userRangeRows = row.wfTaskCandidateDTOs.map(item => {
        return {
          userId: item.id,
          nickName: item.name
        }
      });
      this.rangeTableDatas = rangeTableDatas;
      this.operateIndex = rowIndex;
      this.showWfTaskUserRangeDialog = true;
    },
    confirmWfTaskUserRangeDialog(rows){
      let $grid = this.$refs["table-m1"];
      let item = $grid.getTableData().fullData[this.operateIndex];
      let wfTaskCandidateDTOs = item.wfTaskCandidateDTOs;
      let item1s = [];
      let item2s = []
      rows.forEach(row => {
        let userId = row.userId;
        let wfTaskCandidateDTO = wfTaskCandidateDTOs.find(item => item.id == userId)
        if (wfTaskCandidateDTO) {
          if (!wfTaskCandidateDTO.userFlag) wfTaskCandidateDTO.userFlag = 6
          item1s.push({...wfTaskCandidateDTO});
        } else {
          item2s.push({
            id: userId,
            name: row.nickName,
            userFlag: 6
          })
        }
      })
      item.wfTaskCandidateDTOs = item1s.concat(item2s)

      this.saveItem(item);
    },
    initTableM1() {
      let that = this;
      let tableOption = {
        vue: that,
        tableRef: 'table-m1',
        tableName: 'setCandidateDialog_dialog-m1',
        path: this.current_prefix + '/wf/getAllNodes',
        param: () => {
          return {
            stringOne: this.wfInfo.uuid
          }
        },
        config: {
          /*checkboxConfig: {
            checkStrictly: true,
            showHeader: this.selectMulti,
          },*/
          proxyConfig: {
            props: {
              result: "objx", // 配置响应结果列表字段
              total: "objx.length", // 配置响应结果总页数字段
            },
          },
        },
        columns: [
          {type: 'checkbox', width: 50, resizable: false, fixed: 'left'},
          {title: this.$t2('节点名称', 'components.wf.nodeName'), field: 'nodeName', width: 250, fixed: 'left'},
          {
            field: 'wfTaskCandidates',
            title: this.$t2('候选人', 'components.wf.candidate'),
            width: 450,
            slots: {
              default: 'wfTaskCandidates'
            }
          },
          {
            width: 150,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: 'operate'
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        that.vxeOption = opts;
      });
    },
    getUsernames(row) {
      return row.wfTaskCandidateDTOs.map(item => item.name).join(",")
    },
    checkWfTaskUserRange(row, rowIndex, callback){
      //是否使用用户范围
      let wfInfo = this.wfInfo;
      this.$http({
        url: this.current_prefix + '/wf_task_user_range/list',
        method: `post`,
        data: {
          modelId: wfInfo.modelId,
          taskDefinitionKey: row.nodeId
        },
        isLoading: true,
        errorMsg: false,
        error:(error)=>{
          if(error?.response?.status == 404){
            //不使用用户范围
            callback && callback();
          }else{
            this.$errorMsg({
              content: error.message,
              type: 'error',
            })
          }
        },
        success: res => {
          let rows = res.objx || [];
          if(!rows.length){
            //不使用用户范围
            callback && callback();
          }else{
            //使用用户范围
            this.openShowWfTaskUserRangeDialog(row, rowIndex, rows)
          }
        }
      });
    },

    openUserDialig(row, rowIndex) {
      //是否使用用户范围
      this.checkWfTaskUserRange(row, rowIndex, ()=>{
        //不使用用户范围
        this.userRows = row.wfTaskCandidateDTOs.map(item => {
          return {
            id: item.id,
            nickName: item.name
          }
        });
        this.operateIndex = rowIndex;
        this.showUserDialog = true;
      })
    },
    userConfirm(rows) {
      let $grid = this.$refs["table-m1"];
      let item = $grid.getTableData().fullData[this.operateIndex];
      let wfTaskCandidateDTOs = item.wfTaskCandidateDTOs;
      let item1s = [];
      let item2s = []
      rows.forEach(row => {
        let userId = row.id + "";
        let wfTaskCandidateDTO = wfTaskCandidateDTOs.find(item => item.id == userId)
        if (wfTaskCandidateDTO) {
          if (!wfTaskCandidateDTO.userFlag) wfTaskCandidateDTO.userFlag = 6
          item1s.push({...wfTaskCandidateDTO});
        } else {
          item2s.push({
            id: userId,
            name: row.nickName,
            userFlag: 6
          })
        }
      })
      item.wfTaskCandidateDTOs = item1s.concat(item2s)

      this.saveItem(item);
    },
    saveItem(row) {
      this.$http({
        url: this.current_prefix + '/wf/setTaskUser',
        method: `post`,
        data: row,
        isLoading: true,
        success: res => {
          this.$message({
            message: res.content,
            type: 'success',
            duration: 500,
            onClose: t => {
              this.searchEvent();
            }
          });
        }
      });
    },
    closeWin() {
      this.dialogClose();
      this.$emit('close')
    }
  }
};
export default moudule;
