<template>
  <el-dialog
    :title='$t("common.view")'
    width="80%"
    :append-to-body="true"
    :visible.sync="isVisible"
    class="tenant-view"
  >
    <div class="boxDiv">
    	<!-- 任务信息 -->
    	<el-row>
    		<p style="margin-top:0;text-align:center;font-size:18px;">{{$t("runCenter.form.viewSched.name")}}
				<span style="float:right;margin-right:20px;font-size:14px;">{{$t("runCenter.form.viewSched.bomNo")+"："}}{{tenant.no}}</span>
			</p>
    		<table class="comtable" border="0" cellspacing="0" cellpadding="6">
    			<tr>
    				<td class="blodTd" align="center" width="120">{{$t("runCenter.form.viewSched.procedureNo")+"："}}</td>
    				<td width="200">{{tenant.taskNo}}</td>
    				<td class="blodTd" align="center" width="120">{{$t("runCenter.form.viewSched.status")+"："}}</td>
    				<td width="120">
                         <el-tag v-if="tenant.status == 0" type="info">{{$t("runCenter.common.schedulStatus.noStart")}}</el-tag>
							<el-tag v-if="tenant.status == 1" type="">{{$t("runCenter.common.schedulStatus.run")}}</el-tag>
							<el-tag v-if="tenant.status == 2" type="success">{{$t("runCenter.common.schedulStatus.finished")}}</el-tag>
					</td>
    			</tr>
    			<tr>
					<td class="blodTd" align="center">{{$t("runCenter.form.viewSched.procedureDesc")+"："}}</td>
    				<td>{{tenant.taskName}}</td>
					<td class="blodTd" align="center">{{$t("runCenter.form.viewSched.content")+"："}}</td>
    				<td width="200">{{tenant.content}}</td>
    			</tr>
				<tr>
                    <td class="blodTd" align="center">{{$t("runCenter.form.viewSched.prepareWorkHours")+"："}}</td>
    				<td>{{tenant.time}}分钟</td>
    				<td class="blodTd" align="center">{{$t("runCenter.form.viewSched.procedurePosition")+"："}}</td>
    				<td>{{tenant.currentStationName}}</td>
				</tr>
    			<tr>
    				<td class="blodTd" align="center">{{$t("runCenter.form.viewSched.startTime")+"："}}</td>
    				<td>{{tenant.startTime}}</td>
    				<td class="blodTd" align="center">{{$t("runCenter.form.viewSched.endTime")+"："}}</td>
    				<td>{{tenant.endTime}}</td>
    			</tr>
    		</table>
    	</el-row>

    	<!-- 拆除零件 -->
    	<el-row>
    		<p class="titleP" style="margin-top:15px;color:#000;font-size:18px;" >ST1 拆除零件 </p>
    		<!-- Table数据 -->
			<el-table
		      ref="table"
		      v-loading="loading"
		      :data="tableData.records"
		      border
		      fit
		      style="width: 100%;"
		    >
        <!-- 材料编码 -->
        <el-table-column prop="meterialCode" :label='$t("prepare.table.tools.no")' align="center" ></el-table-column>
        <!-- 材料牌号 -->
        <el-table-column prop="tradeMark" :label='$t("prepare.table.tools.type")' align="center" ></el-table-column>
        <!-- 材料名称 -->
        <el-table-column prop="equipmentName" :label='$t("prepare.table.tools.name")' align="center"></el-table-column>

			  <el-table-column prop="name" :label='$t("prepare.table.tools.name")' align="center">
				   <template slot-scope="{ row }">
                       <el-button  size="small"  type="primary" @click.native="printCode(row)" round >打印零件编码</el-button>
				   </template>
			  </el-table-column>

			</el-table>
    	</el-row>
    	<!-- 拆除夹具 -->
    	<el-row v-show="false">
    		<p class="titleP" style="margin-top:15px;color:#000;font-size:18px;">ST2 拆除夹具   <el-button size="small" type="primary" @click.native="release" round>拆除夹具</el-button></p>
    		<el-tag size="medium" style="margin:10px;font-size:15px;"> 人工拆除夹具并放入夹具库</el-tag>
    	</el-row>

    	<div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="confirmDisabled" @click="submitForm">{{ $t('machining.buttons.finished') }}</el-button>
	      <el-button type="primary" :disabled="confirmDisabled" @click="storageForm">{{ $t('machining.buttons.inFinished') }}</el-button>
	    </div>
    </div>
  </el-dialog>
</template>
<script>
	// 列表查询的共通参数
	import { initQueryParams } from '@/utils/commons'
  import taskMgrApi from "@/api/runManageCenter/taskMgr"
  import taskNodMgrApi from "@/api/machiningClient/machiningClient"
  import machiningClientApi from "@/api/machiningClient/machiningClient"
export default {
  name: 'TaskView',
  components: { },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      screenWidth: 0,
      confirmDisabled: false,
      callRobotFlag: false,	
      unBaitingCount: 0,
      unFeedingCount: 0,
      width: this.initWidth(),
      tenant: {},
      queryParams: initQueryParams({}),
      RFIDParams: {
		    url: "192.168.11.130",
		    port: "102",
		    taskId: "",
  		  taskNodeId: "",
  		  data: {
    		  content: "",
    		  positionIndex: ""
  		  }
	    },
      loading: false,
    	tenantViewVisible: false,
    	tableKey: 0,
	  	tableData: {
	    	total: 0
	  	},
	  	tableData1: [],
	  	tableData2: [],
	  	currentRow: null, // 当前选择的行

	  	spanArr: [],  // 【组序】合并单元格
	    spanArr2: [],  // 【优先级】合并单元格
    }
  },
  // 实例已经在内存中创建好，此时data和methods已将ok,如果要操作data中的数据或是调用methods中的方法，最早只能在created中操作
  created() {
  	// 加载列表数据
  	// this.fetch()
	},
  computed: {
    isVisible: {
      get () {
        return this.dialogVisible
      },
      set () {
        this.close()
      }
    }
  },
  mounted () {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
  },
  methods: {
    confirmBaiting(){
		  let valid = this.tenant.taskNodeId!=''? true: false;
	    if(!valid){
          this.$message({
	          message: "任务ID" + this.$t("rules.require"),
	          type: "warning"
	      })		  
		  return false
	    }
      console.log(this.tenant)
      taskMgrApi.confirmBaiting({ "taskNodeId":this.tenant.taskNodeId}).then((res) => {
          res = res.data         
          if (res.isSuccess) {          
            this.unBaitingCount = res.data.unBaitingCount
            this.unFeedingCount = res.data.unFeedingCount            
          }
        });        
      },  
	release(){
	    // 上下料站拆除夹具
      taskMgrApi.releaseClamp({ "id" : this.tenant.id}).then((res) => {
          res = res.data;
          if (res.isSuccess) {
              // 通知列表
              this.$emit("success");
          }
      });
	},
	printCode(row){
        this.loading = true        
        taskNodMgrApi.printCode({"uniqueCode" : this.tenant.uniqueCode, "taskNodeId" : row.taskNodeId }).then((res) => {
        res = res.data;
        if (res.isSuccess) {
          // 通知列表
          this.$emit("success");
          // // 通知列表-并关闭弹出框
          // this.$emit("close");
        }
      }).finally(()=>{
        this.loading = false
      });
    },
    storageForm(){
      machiningClientApi.checkStockNode({"taskNodeId": this.tenant.taskNodeId}).then((res)=>{
            res = res.data;
            if (res.isSuccess) {                   
              if(res.data){
                  this.submitForm()   
              }else{
                  this.callRobot()
              }
            }
      })
    },
  	submitForm(){
        this.confirmDisabled = true
        taskMgrApi.updateTaskStatus({ "id" : this.tenant.id, "updateType" : "end","processType" : "3","updateUser" : this.$store.state.account.user.id}).then((res) => {
            res = res.data;
            if (res.isSuccess) {
                // 通知列表
                this.$emit("success");
                // 通知列表-并关闭弹出框
                this.$emit("close");
            }
        }).finally(()=>{
        this.confirmDisabled = false
      });
  	},
    callRobot(){
      this.callRobotFlag = false     	
      	machiningClientApi.callRobot({ "taskNodeId" : this.tenant.taskNodeId, "callType":"2"}).then((res) => {
        			res = res.data
        			if (res.isSuccess) {                 
         				 // 通知列表
          				this.$emit("success");
          				// 通知列表-并关闭弹出框
          				this.$emit("close");
       				 }
      			}).finally(()=>{
        			this.callRobotFlag = true
       });		 
		
    },
    initWidth () {
      this.screenWidth = document.body.clientWidth
      if (this.screenWidth < 550) {
        return '95%'
      } else if (this.screenWidth < 990) {
        return '580px'
      } else if (this.screenWidth < 1400) {
        return '600px'
      } else {
        return '650px'
      }
    },
    setTenant (val) {
      this.tenant = { ...val }
      // 添加数据
      this.tableData.records = []
      this.tableData.records.push({
          meterialCode: this.tenant.meterialCode,
          equipmentName: this.tenant.equipmentName,
          tradeMark: this.tenant.tradeMark,
          armorPic: this.tenant.armorPic,
      })

      if(!this.tenant.uniqueCode){
        this.RFIDParams.taskId =  this.tenant.id
        this.RFIDParams.taskNodeId =  this.tenant.taskNodeId
        this.RFIDParams.data.positionIndex = this.tenant.currentStationName.substr(-1)
        let that = this
        machiningClientApi.read(this.RFIDParams).then(res => {         
         if(res.data.result){
           this.$message({
		          	 message: "RFID: "+res.data.result,
		          	 type: "success"
		      })
           that.tenant.uniqueCode = res.data.result
         }
        });
      }

      this.confirmBaiting()
    },
    close () {
      this.$emit('close')
    },
  }
}
</script>
<style lang="scss" scoped>
	.dialog-footer{
		text-align: center;
		padding: 30px;
	}
	.titleP{
		position: relative;
		font-size: 16px;
		padding-left: 15px;
	}
	.titleP:before{
		content: '';
		position: absolute;
		left: 0;
		top: 3px;
		width: 5px;
		height: 80%;
		background: #0F79DC;
	}
	.comtable{
		width: 100%;
		border-left: 1px solid #CCCCCC;
		border-top: 1px solid #CCCCCC;
	}
	.comtable td{
		border-right: 1px solid #CCCCCC;
		border-bottom: 1px solid #CCCCCC;
	}
	.blodTd{
		background: #EEEEEE;
		color: #333333;
	}
</style>
