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

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

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

      rangeTableDatas: [],
      userRangeRows: [],
      showWfTaskUserRangeDialog:false,

      tableData: [],
      showModifyButton:false,
      showPreWfNodesButton:false

    };
  },
  methods: {
    initWfParam15(){
      //初始化是否开启获取上一次候选人功能
      return this.$http({
        url: USER_PREFIX + "/wf_diy_attribute/getValue",
        method: `post`,
        data: {attributeKey: "param15"},
        success: (res) => {
          this.showPreWfNodesButton = res?.objx === true;
        },
      });
    },
    handleModifyButtonState(){
      let result = false;
      this.tableData.forEach(item => {
        if (item.modifyCandidates && !item.wfTaskCandidateDTOs?.length) {
          result = true;
        }
      })
      this.showModifyButton = result
    },
    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.handleModifyButtonState();
    },
    initTableM1() {
      this.tableData = this.$baseLodash.cloneDeep(this.rows || []);
      this.handleModifyButtonState();
      let that = this;
      let tableOption = {
        vue: that,
        tableRef: 'table-m1',
        tableName: 'setCandidateDialog2_dialog-m1',
        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){
      //是否使用用户范围
      this.$http({
        url: this.current_prefix + '/wf_task_user_range/list',
        method: `post`,
        data: {
          modelId: this.modelId,
          taskDefinitionKey: row.nodeId
        },
        isLoading: true,
        success: res => {
          let rows = res.objx || [];
          if(!rows.length){
            //不使用用户范围
            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.handleModifyButtonState();
      // 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();
            }
          });
        }
      });
    },
    confirmDialog(){
      this.$emit("confirm",this.tableData)
      this.closeWin()
    },
    closeWin() {
      this.dialogClose();
      this.$emit('close')
    },
    fillCandidates() {
      this.$baseConfirm(this.$t1('您确定要获取上次候选人吗？')).then(() => {
        this.$http({
          url: this.current_prefix + '/wf/getPreWfNodes',
          method: `post`,
          data: {
            objTypeCode: this.objTypeCode,
            modelId: this.modelId,
            userFlag: 5
          },
          isLoading: true,
          success: res => {
            let rows = res.objx || []
            if(rows.length){
              let dataMap = {};
              rows.forEach(row=>{
                let items = row.wfTaskCandidateDTOs || [];
                items.forEach(item=>{
                  item.userFlag = 5
                })
                dataMap[row.nodeId] = row.wfTaskCandidateDTOs || [];
              })
              this.tableData.forEach(item => {
                if (item.modifyCandidates && !item.wfTaskCandidateDTOs?.length) {
                  item.wfTaskCandidateDTOs = dataMap[item.nodeId] || [];
                }
              })
              this.handleModifyButtonState();
            }
          }
        });

      });

    },
  }
};
export default moudule;
