<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.createName" 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-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="任务名称" width="230" align="center"></el-table-column>
			  <el-table-column prop="reportName" label="所属报告" width="200" align="center">
				<template slot-scope="scope">
					<div>
						<el-link type="primary" @click="checkReport(scope.row)">{{ scope.row.reportName }}</el-link>
					</div>
				</template>
			  </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="180" 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="120" align="center">
				<template slot-scope="scope">
					<div>
					{{reportType[scope.row.type]}}
					</div>
				</template>
			  </el-table-column>
			  <el-table-column prop="taskStatus" label="状态" sortable width="120" align="center">
				<template slot-scope="scope">
					<div>
					{{status[scope.row.taskStatus]}}
					</div>
				</template>
			  </el-table-column>
			  <el-table-column label="任务进度" align="center" min-width="200px" show-overflow-tooltip v-if="queryParams.taskStatus != 1">
              <template slot-scope="scope">
                <!-- <span>
                  {{ scope.row.progress }}
                </span> -->
                <el-progress :text-inside="true" :stroke-width="20" :percentage="scope.row.progress" :color="customColors"></el-progress>
              </template>
            </el-table-column>
			  <el-table-column prop="finishTime" label="实际完成时间" width="180" sortable  align="center" v-if="queryParams.taskStatus == 1"></el-table-column>
			  <el-table-column prop="result" label="结果" sortable  align="center" v-if="queryParams.taskStatus == 1">
				<template slot-scope="scope">
					<div v-if="scope.row.type==0">
						<span v-if="btnObj.filter(item=>{return item.value == scope.row.result}).length>0">{{btnObj.filter(item=>{return item.value == scope.row.result})[0].label}}</span>
					</div>
					<div v-else>
						{{status[scope.row.taskStatus]}}
					</div>
				</template>
			  </el-table-column>
			  <!-- <el-table-column prop="remark" label="理由" sortable  align="center" v-if="queryParams.taskStatus == 1"></el-table-column> -->
			  <el-table-column prop="operation" label="操作" align="center" fixed="right" v-if="queryParams.taskStatus == 0" width="180px" >
				  <template slot-scope="scope">	
						  <el-button size="mini" @click="handleEdit(scope.row)">处理</el-button>
            <el-popconfirm 
              style="margin-left: 10px;"
              v-if="scope.row.type!=0"
                  title="这一条任务确定完成吗？" 
                  confirm-button-text='确定'
                  cancel-button-text='取消'
                  @confirm="handleFinsh(scope.row)">
                  <!--  @click="handleFinsh(scope.row)" -->
              <el-button size="mini" slot="reference"  type="primary">确认完成</el-button>
            </el-popconfirm>
				  </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-descriptions class="margin-top" :column="1" :size="size">

			<el-descriptions-item label="标的专利号">{{form.signPatentNo}}</el-descriptions-item>
			<el-descriptions-item label="报告名称">{{form.name}}</el-descriptions-item>
			<el-descriptions-item label="承担部门">{{form.departmentName}}</el-descriptions-item>
			<el-descriptions-item label="负责人">{{form.personName}}</el-descriptions-item>
			<el-descriptions-item label="委托方">{{form.clientName}}</el-descriptions-item>
			<el-descriptions-item label="产品/技术">{{form.proTec}}</el-descriptions-item>
			<el-descriptions-item label="应用场景">
        <div style="word-wrap:keep-all;">
          <span v-for="item in form.scenarioNames" >
            {{item}}&nbsp;&nbsp;&nbsp;
          </span>
        </div>
        
      </el-descriptions-item>
			<el-descriptions-item label="关联报告">{{form.associateReportName}}</el-descriptions-item>
			<el-descriptions-item label="卷号">{{form.volumeNumber}}</el-descriptions-item>
			<el-descriptions-item label="备注">{{form.remark}}</el-descriptions-item>
  		</el-descriptions>
        <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose">取 消</el-button>
			  <el-dropdown split-button type="primary" @click="submit">
				{{btn.label}}
				<el-dropdown-menu slot="dropdown">
					<el-dropdown-item v-for="item in btnObj" :key="item.label" @click.native="onChangeFamily(item)">{{ item.label }}</el-dropdown-item>
				</el-dropdown-menu>
			  </el-dropdown>
              <!-- <el-button type="primary" @click="submit" >审核通过</el-button> -->
        </span>
    </el-dialog>
	  </div>
</template>
  
<script>
  export default {
	props:['activeName','reportType','status'],
	data() {
	  return {
		customColors: [
        {color: '#f56c6c', percentage: 20},
        {color: '#e6a23c', percentage: 40},
        {color: '#5cb87a', percentage: 60},
        {color: '#1989fa', percentage: 80},
        {color: '#6f7ad3', percentage: 100}
      ],
		tableData:[],
		form:[],
		size:'',
		taskId:'',
		remark:'',
		btn:{
				label:'同意创建',
				value:1
      },
		queryParams: {
		  size: 10,
		  current: 1,
		  taskStatus:this.activeName=='first'?0:1,
		  roleInTask:1,
		  taskName:'',
		  createName:'',
		  type:null
		},
		btnObj:[
			{
				label:'同意创建',
				value:1
			},
			{
				label:'取消创建',
				value:4
			},
			{
				label:'缺少材料',
				value:5
			}
		],
		total: 0,
		loading:false,
      showDialog: false,
    //   finish: {
	// 	'0':'未完成',
	// 	'1':'完成'
	//   },
      coordination:true,
	  };
	},
	watch:{
	  activeName(val){
		// this.queryParams.taskStatus = this.activeName=='first'?0:1
      this.queryParams.taskStatus = this.activeName == 'first' ? 0 : 1
		  this.getList()
	  }
	},
	mounted() {
	  this.getList()
	},
	computed:{
		dictMessage() {
			return this.$store.state.dictMessage.dictMessage
		},
	},
	methods: {
		checkReport(row){
			row.dictMessage = this.dictMessage
			row.type = row.reportType
			row.name = row.taskName
			this.$s.setSession('row', row)
			var router = this.$router.resolve({
				name: 'taskReportDetails' ,
			})
          window.open(router.href, '_blank');
		},
		//查询任务
		async getList(){
			this.tableData = []
			this.loading = true
			var data = await this.$api.QueryPageList(this.queryParams)
			// .then((response)=>{
				// if(response.code == 200){
					// var a = response.data
					if(this.queryParams.taskStatus != 1 && data.data.records.length>0){
						data.data.records.forEach(async(item,index)=>{
						// if(item.type==0){
							await this.$api.taskProgress({ id: item.id }).then(response => {
								if (response.code == 200) {
									if(item.type!=0){
										if(response.data.total !=0){
											item.progress = Number((response.data.finish / response.data.total *100).toFixed(2))
										}else{
											item.progress = 0
										}
										
									}else{
										if(item.taskStatus == 1){
											item.progress = 100
										}else{
											item.progress = 0
										}
									}
									if(index == data.data.records.length-1){
										this.tableData = data.data.records
										this.total = data.data.total
										this.loading=false
									}
								}
								
							})
						// }else{
						// 	setTimeout(() => {
						// 		if(item.taskStatus == 1){
						// 			item.progress = 100
						// 		}else{
						// 			item.progress = 0
						// 		}
						// 	}, 500)
						// }
						})
					}else{
						this.tableData = data.data.records
						this.total = data.data.total
						this.loading=false
					}
					
					// this.$nextTick(()=>{
					// 	setTimeout(()=>{
					// 		this.tableData = response.data.records
					// 		this.total = response.data.total
					// 		this.loading=false
					// 	},800)
					// })
					
					
				// }
			// })
		},
	  
	  //切换按钮
	  onChangeFamily(key) {
		this.btn = key
	  },
	  //完成任务
    handleEdit(row) {
      switch (row.type) {
        case 0://开卷
          this.bookReviewTask(row)
          break;
        case 1://协同
          this.coordinationTask(row)
          break;
        case 2://对比
		case 3://初筛
          this.contrastTask(row)
          break;
      
        default:
          break;
      }
    },
    handleFinsh(row) {
      console.log(row);
      this.$api.taskFinish({ id: row.id }).then((res) => {
        if (row.type==1) {
          this.$message.success("任务完成，已成功发送邮件")
        } else {
          this.$message.success("任务已完成")
        }
        this.getList()
      })
    },
    bookReviewTask(row) {
      this.taskId = row.id
      var queryParams={
        reportId:row.reportId
      }
      this.$api.QueryReport(queryParams).then(response=>{
        console.log(response)
        this.showDialog=true
        this.form = response.data[0]
      })
    },
    coordinationTask(row) {
      console.log(row);
      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)
            var a = this.$router.resolve({
              path: '/FTOContrast/' + res.data.records[0].id,
              query: {
                patentNo:res.data.records[0].patentNo,
                reportId:row.reportId,
              }
            })
            window.open(a.href, "_blank")
          }
        })
         
      } else {
        var a = this.$router.resolve({
          path: '/Incomplete',
          query: {
            taskId: row.id,
            reportId:row.reportId,
            type:row.type,
            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")
      }
    },
    contrastTask(row) {
		// this.$store.commit('SET_ADMIN_SIGNPATENTNO',row.signPatentNo)
		// console.log(row.signPatentNo,this.$store.state.admin)
		// localStorage.signPatentNo = row.signPatentNo
    // console.log(row);
      let a=this.$router.resolve({
        path: '/Incomplete',
        query: {
          // taskStatus: this.queryParams.taskStatus,
          taskId: row.id,
          reportId:row.reportId,
          type:row.type,
		      reportType:row.reportType,
          signPatentNo:row.signPatentNo
        }
      })
      window.open(a.href,"_blank")
    },
	  submit(){
		var a = {
			taskId:this.taskId,
			status:1,
			// remark:this.remark,
			result: this.btn.value,
			reportStatus:this.btn.value
      }
    console.log(a);
      this.$api.AuditTask(a).then(response => {
      console.log(response);
			if(response.code == 200){
				// this.$message.success('审核通过')
				this.showDialog = false
				this.getList()
			}
		})
	  },
	  //分页
	  handleCurrentChange(val) {
		this.queryParams.current = val;
		this.getList();
	  },
	  //关闭弹窗
	  handleClose(){
		this.showDialog = false
	  }
	},
	
  };
  </script>
  
<style lang="scss" scoped>
  </style>