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

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

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

      rangeTableDatas:[],
      userRangeRows: [],
      showWfTaskUserRangeDialog:false,
      currentRow:null
    };
  },
  methods: {
    init(){
      this.currentRow = this.$baseLodash.cloneDeep(this.checkRow);
    },
    openShowWfTaskUserRangeDialog(row, rangeTableDatas){
      /* this.userRangeRows = row.wfTaskCandidateDTOs.map(item => {
        return {
          userId: item.id,
          nickName: item.name
        }
      }); */
      this.rangeTableDatas = rangeTableDatas;
      this.showWfTaskUserRangeDialog = true;
    },
    confirmWfTaskUserRangeDialog(rows){
      let wfInfo = this.wfInfo;
      let item2s = []
      rows.forEach(row => {
        let userId = row.userId;
        item2s.push({
          id: userId,
          name: row.nickName,
          userFlag: 6
        })
      })

      let formData = {
        uuid: wfInfo.uuid,
        taskId:this.currentRow.taskId,
        nodeId:this.currentRow.taskDefinitionKey,
        wfTaskCandidateDTOs:item2s
      }

      this.saveItem(formData);
    },
    checkWfTaskUserRange(row, callback){
      //是否使用用户范围
      let wfInfo = this.wfInfo;
      this.$http({
        url: this.current_prefix + '/wf_task_user_range/list',
        method: `post`,
        data: {
          modelId: wfInfo.modelId,
          taskDefinitionKey: row.taskDefinitionKey
        },
        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, rows)
          }
        }
      });
    },

    openUserDialig() {
      let row = this.currentRow;
      //是否使用用户范围
      this.checkWfTaskUserRange(row, ()=>{
        //不使用用户范围
        /* this.userRows = row.wfTaskCandidateDTOs.map(item => {
          return {
            id: item.id,
            nickName: item.name
          }
        }); */
        this.showUserDialog = true;
      })
    },
    userConfirm(rows) {
      let wfInfo = this.wfInfo;
      let item2s = []
      rows.forEach(row => {
        let userId = row.id + "";
        item2s.push({
          id: userId,
          name: row.nickName,
          userFlag: 6
        })
      })

      let formData = {
        uuid: wfInfo.uuid,
        taskId:this.currentRow.taskId,
        nodeId:this.currentRow.taskDefinitionKey,
        wfTaskCandidateDTOs:item2s
      }

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