<template>
  <!-- 审核任务弹窗 -->
  <div>
    <el-dialog title="添加审核任务" :visible.sync="showTask" width="500px" @close="handleCloseTask"
      :close-on-click-modal="false">
      <el-form :model="form" :rules="TaskRules" ref="form" label-width="120px">
        <el-form-item label="任务名称：" prop="name">
          <el-input v-model="form.name" type="text" placeholder="输入任务名称" />
        </el-form-item>
        <el-form-item label="审核人：" prop="handler">
          <el-select style="width:100%;" ref="select1" v-model="form.handler" clearable filterable remote
            :remote-method="remoteMethodPerson" v-SelectLazyLoading="lazyLoadingPerson">
            <el-option v-for="item in personnelList.data" :key="item.id" :label="item.name" :value="item.id"
              placeholder="请选择审核人"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="截止日期：" prop="deadLineTime">
          <el-date-picker style="width:100%" v-model="form.deadLineTime" value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核备注：" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="输入备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseTask">取 消</el-button>
        <el-button type="primary" @click="submitTask">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    const isTime = (rule, value, callback) => {
      if (this.isEndTime) {
        this.isEndTimes = Date.parse(new Date)
      }

      let b = Date.parse(value)
      if (value) {
        if (b < this.isEndTimes) {
          callback(new Error('禁止选择现在及以前时间，请重新选择'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请选择时间'))
      }
    }
    return {
      isEndTime: true,
      isEndTimes: null,
      // 审核任务表单数据
      form: {},
      showTask: false,
      // 任务表单校验
      TaskRules: {
        name: [{ required: true, message: '请输入任务名称', trigger: 'blur' },],
        handler: [{ required: true, message: '请选择审核人', trigger: 'change' },],
        deadLineTime: [{ required: true, validator: isTime, trigger: 'change' }],
      },
      //人员列表懒加载
      personnelList: {
        queryParams: {
          current: 1,
          size: 10
        },
        loading: false,
        data: [],
      },
      // 父组件form表单数据
      parentForm: {},
    }
  },
  computed: {},
  mounted() {

  },
  methods: {
    open(data, type) {
      this.showTask = true
      // 判断文件是否都上传完毕
      if (data.systemFileList && data.systemFileList.length > 0) {
        data.fileGuids = []
        for (let i = 0; i < data.systemFileList.length; i++) {
          if (data.systemFileList[i].guid) {
            data.fileGuids.push(data.systemFileList[i].guid)
          } else {
            this.$message.error('文件未全部上传，请耐心等待')
            return false
          }
        }
      }
      this.parentForm = data
      // 任务类型
      this.form.type = type
      this.getPermissionPersonnel()
    },
    // 确定
    submitTask() {
      this.isEndTime = false
      this.$refs.form.validate((valid) => {
        if (valid) {
          /**类型
           * 0标引任务
           * 1项目开卷审核任务
           * 2检索条件任务
           * 3对比任务
           * 4协同任务
           * 5任务审核任务
           * 6文件分配任务审核
           */
          
          if (['@'].includes(this.form.handler)) {//是邮箱为1
            this.form.handlerType = 1
          } else {//是id为0
            this.form.handlerType = 0
          }
          let params = {
            patentDigProjectDTO: this.parentForm,
            projectTaskDTO: this.form,
          }
          // 区分是哪种审核
          switch (this.form.type) {
            case 1://专利挖掘项目审核
                this.addProjectOpenExamineTask(params)
              break;
            case 6://专利挖掘项目-文件分配任务审核

              break;
            case 7://专利挖掘项目-文件列表审核任务

              break;

            default:
              break;
          }

        }
      })
    },
    // 专利挖掘项目-文件分配任务审核
    task(params) {
      this.$api.task(params).then(res => {
        if (res.code == 200) {
          this.$message.success('已发送审核任务')
          this.$emit('taskForm', '已发送审核任务')
          this.handleCloseTask()
        }
      })
    },
    // 专利挖掘项目审核
    addProjectOpenExamineTask(params) {
      this.$api.addProjectOpenExamineTask(params).then(res => {
        if (res.code == 200) {
          this.$message.success('已发送审核任务')
          this.$emit('taskForm', '已发送审核任务')
          this.handleCloseTask()
        }
      })
    },
    // 取消
    handleCloseTask() {
      this.$refs.form.resetFields()
      this.form = {}
      this.showTask = false
      this.isEndTime = true
    },
    /**
    * 人员
    */
    // 人员列表远程搜索
    remoteMethodPerson(query) {
      this.personnelList.data = []
      this.personnelList.queryParams.current = 1
      this.personnelList.queryParams.name = query
      this.getPermissionPersonnel()
    },
    // 获取所有人员列表懒加载
    lazyLoadingPerson() {
      if (this.personnelList.queryParams.current * this.personnelList.queryParams.size >= this.personnelList.queryParams.total) {
        return false
      }
      this.personnelList.queryParams.current += 1
      this.getPermissionPersonnel()
    },
    //获取所有人员列表（修改不要一次性获取，可以使用懒加载加远程搜索 ）
    async getPermissionPersonnel(type) {
      this.personnelList.loading = true;
      await this.$api.getPermissionPersonnel(this.personnelList.queryParams).then((response) => {
        if (response.code == 200) {
          this.personnelList.loading = false;
          this.personnelList.data.push(...response.data)
          this.personnelList.queryParams.total = response.pageColumn.total
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>