<template>
  <!-- 权限管理确认 -->
	<div style="padding:20px">
    <el-form :inline="true">
			<el-form-item label="名称">
				<el-input v-model="queryParams.taskName" size="small" placeholder="请输入任务名称"></el-input>
			</el-form-item>
			<el-form-item label="处理人">
				<el-input v-model="queryParams.handlePersonName" size="small" placeholder="请输入处理人名称"></el-input>
			</el-form-item>
			<el-form-item label="类型">
				<el-select clearable v-model="queryParams.type" size="small" placeholder="请选择任务类型">
					<el-option v-for="(item,key) in reportType" :key="key" :label="item" :value="key"></el-option>
					<!-- <el-option label="协同任务" value="1"></el-option>
					<el-option label="对比任务" value="2"></el-option>
					<el-option label="初筛任务" value="3"></el-option> -->
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="" size="small" @click="getList">查询</el-button>
			</el-form-item>
		</el-form>
		<!-- 序号/任务名称/创建人/创建时间/状态/完成时间/操作 -->
		<el-table
      border
      style="width: 100%;"
			:data="tableData"
      v-loading="loading"
      header-row-class-name="custom-table-header"
			:default-sort="{ prop: 'date', order: 'descending' }"
      >
			<el-table-column prop="id" type="index" label="序号" width="100" align="center"></el-table-column>
			  <el-table-column prop="taskName" label="任务名称" min-width="230" align="center"></el-table-column>
			  <el-table-column prop="handlePersonName" label="处理人" width="150" align="center"></el-table-column>
			  <el-table-column prop="createName" label="发起人" width="150" align="center"></el-table-column>
			  <el-table-column prop="createTime" label="创建时间" sortable width="200" align="center"></el-table-column>
			  <el-table-column prop="endTime" label="期限" sortable width="200" align="center"></el-table-column>
			  <el-table-column prop="remark" label="备注" sortable width="200" align="center"></el-table-column>
			  <el-table-column prop="type" label="类型" sortable width="150" align="center">
          <template slot-scope="scope">
            <div>
              {{reportType[scope.row.type]}}
            </div>
          </template>
        </el-table-column>
			  <el-table-column prop="taskStatus" label="状态" sortable width="150" align="center">
          <template slot-scope="scope">
            <div>
              {{status[scope.row.taskStatus]}}
            </div>
          </template>
        </el-table-column>
			  <el-table-column prop="finishTime" label="实际完成时间" width="200" sortable align="center" v-if="queryParams.taskStatus == 1"></el-table-column>
			  <el-table-column label="操作" sortable align="center" fixed="right" width="220">
          <template slot-scope="scope">
            <div>
              <!-- <el-button type="primary" size="small" @click="edit(scope.row)" v-if="scope.row.taskStatus == 0">编辑</el-button> -->
    
              <el-dropdown split-button type="primary" size="small" @click="edit(scope.row)" @command="taskWorkRemakBtn($event, scope.row)" trigger="click" v-if="scope.row.taskStatus != 1 " style="margin-right: 10px;">
                <p>编辑</p>
                <el-dropdown-menu slot="dropdown">
                  <!-- <el-dropdown-item command="1" @click="taskWorkRemakBtn(scope.row)" v-if="scope.row.type == 1">确认结果</el-dropdown-item> -->
                  <el-dropdown-item command="1" v-if="scope.row.type == 1 && scope.row.taskStatus==2">确认结果</el-dropdown-item>
                  <el-dropdown-item command="2" v-if="scope.row.type!=0">确认完成</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-button type="primary" size="small" @click="edit(scope.row)" v-else>查看</el-button>
              <!-- <el-button type="primary" size="small" @click="taskWorkRemakBtn(scope.row)" v-if="scope.row.type == 1">结果</el-button> -->
              <!-- <el-button size="small">取消</el-button> -->
            </div>
          </template>
        </el-table-column>
		</el-table>
		<div style="text-align: center;">
      <el-pagination background layout="total, prev, pager, next, jumper" :current-page.sync="queryParams.current"
        :page-size.sync="queryParams.size" @current-change="handleCurrentChange" :total="total">
      </el-pagination>
    </div>

    <el-dialog title="编辑任务" :visible.sync="showDialog" width="600px" :close-on-click-modal="false"  @close="handleClose">
      <el-form :model="form" ref="reportForm" label-width="120px" :rules="taskRules">
            <el-form-item label="任务名称">
              <el-input v-model="form.taskName" :disabled="true" autocomplete="off" placeholder="请输入任务名称"></el-input>
            </el-form-item>
            <el-form-item label="处理人">
              <el-select style="width:100%;" ref="select1" v-model="form.handlePersonId" clearable
               filterable >
               <el-option v-for="item in personnelList" :key="item.id" :label="item.personnelName" :value="item.id"></el-option>
            </el-select>
              <!-- <el-input v-model="form.handlePersonName" :disabled="true" autocomplete="off" placeholder="请输入处理人"></el-input> -->
            </el-form-item>
            <el-form-item label="发起人">
              <el-input v-model="form.createName" :disabled="true" autocomplete="off" placeholder="请输入发起人"></el-input>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-input v-model="form.createTime" :disabled="true" autocomplete="off" placeholder="请输入创建时间"></el-input>
            </el-form-item>
            <el-form-item label="期限" prop="endTime">
              <el-date-picker v-model="form.endTime" value-format="yyyy-MM-dd HH:mm:ss"  type="datetime" placeholder="选择日期时间" style="width:100%"> </el-date-picker>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer" v-if="form.taskStatus==0">
              <el-button @click="handleClose">取 消</el-button>
              <el-button type="primary" @click="submit" >确认</el-button>
        </span>
    </el-dialog>
	</div>
</template>

<script>
export default {
  props:['activeName','reportType','status'],
  data() {
    const isTime = (rule, value, callback) => {
      if (this.formEndTime) {
         this.changeEndTime = Date.parse(new Date)
      }
      let b = Date.parse(value)
      console.log(this.changeEndTime,b);
      if (value) {
        if (b<this.changeEndTime) {
          callback(new Error('禁止选择现在及以前时间，请重新选择'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      tableData:[],
      form:[],
      formEndTime: true,
      changeEndTimes:null,
      queryParams: {
        size: 10,
        current: 1,
        taskStatus:this.activeName=='first'?0:1,
        roleInTask:0,
        taskName:'',
        createName:'',
        type:null
      },
      total: 0,
      personnelList:[],
      showDialog:false,
      loading: false,
      taskWorkRemak: 0,
      // pickerOptions: {
      //   disabledDate (time) {
      //       //disabledDate 文档上：设置禁用状态，参数为当前日期，要求返回 Boolean
      //       return time.getTime() < Date.now()//选当前时间之后的时间
      //     }
      // },
      taskRules:{
        endTime: [
            {  required: false, validator:isTime, trigger: 'change' }
          ],
      },
    };
  },
  watch:{
    activeName(val){
      this.queryParams.taskStatus = this.activeName=='first'?0:1
      this.getList()
    }
  },
  mounted() {
    this.getList()
    this.getAllPersonnelList()
  },
  methods: {
    
     //获取全部人员
     getAllPersonnelList(){
      this.$api.getAllPersonnel().then(response=>{
        // console.log(response)
        this.personnelList = response.data
      })
    },
    //查询任务
    getList(){
      this.loading=true
      this.$api.QueryPageList(this.queryParams).then(response=>{
        this.tableData = response.data.records
        this.total = response.data.total
        this.loading=false
      })
    },
    //分页
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    //编辑报告
    edit(row) {
      console.log(row,11);
      this.showDialog=true
      this.form =JSON.parse(JSON.stringify(row))
    },
    // 查看(确认)结果
    taskWorkRemakBtn(ev,row) {
      console.log(ev, row);
      if (ev == 1) {
        if (row.reportType==4) {
          let queryParams = {
            taskId: row.id,
            reportId: row.reportId,
            type:row.type,
            reportType:row.reportType,
            current: 1,
            size: 10,
            taskStatus: "3",
            filedOptions: [],
          }
          this.$api.reTaskPatents(queryParams).then(res => {
          console.log(res);
          if (res.code == 200) {
            var b = {
                taskId:row.id,
                type:row.type,
                reportId:row.reportId,
                patentNo:res.data.records[0].patentNo,
                reportType : row.reportType,
                reportStatus: "",
            }
            this.$s.setSession('params',b)
            let a = this.$router.resolve({
              path: '/FTOContrast/' + res.data.records[0].id,
              query: {
                patentNo:res.data.records[0].patentNo,
                reportId: row.reportId,
                taskWorkRemak:0,
              }
            })
            window.open(a.href, "_blank")
          }
        })
         
        } else {
          let a = this.$router.resolve({
            path: '/Incomplete',
            query: {
              taskId: row.id,
              reportId: row.reportId,
              type: row.type,
              taskWorkRemak:0,
              reportType: row.reportType,
              signPatentNo:row.signPatentNo
              // coordination: this.coordination
            }
          // path: '/Incomplete'+`taskId=470&reportId=350&type=3&reportType=${}`,
          // path: '/customer_review',
          })
          window.open(a.href, "_blank")
        }
        
      } else {
        this.$confirm('确认任务完成, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$api.taskFinish({id:row.id}).then((res) => {
                this.$message.success("任务已完成")
                this.getList()
              })
            }).catch(() => {
              this.$message.info("已取消")         
        });
        
      }
       
    },
    submit() {
      this.formEndTime=false
      this.$refs.reportForm.validate(valid => {
        if (valid) {
           this.$api.UpdateTask(this.form).then(response=>{
            if(response.code == 200){
              this.$message.success('任务修改成功')
              this.handleClose()
              this.getList()
            }
          })
        }
      })
     
    },
    //关闭弹窗
	  handleClose(){
      this.showDialog = false
      this.formEndTime=true
	  }
  },
  
};
</script>

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