<template>
  <div>
    <div style="width: 1900px;overflow:auto">
      <div style="white-space: nowrap">
        <el-card>
          <el-row>
            <el-select v-model="machineNameValue" placeholder="机床图号" style="width: 150px" size="small" filterable clearable>
              <el-option
                v-for="item in machineName"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
            <el-input v-model="partName" placeholder="零件图号名称" size="small" clearable style="width:150px"/>
            <el-date-picker
              v-model="outTime"
              type="daterange"
              size="small"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              range-separator="~"
              style="width:240px;margin-left: 10px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"/>
            <!--<span>处理结果:</span>-->
            <el-select v-model="ProcessingResultValue" filterable placeholder="处理结果" size="small" style="width: 130px;margin-left: 10px" clearable>
              <el-option
                v-for="item in ProcessingResult"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
            <el-button type="primary" icon="el-icon-search" size="small" plain style="margin-left: 10px" @click="total=0;pageSize = 20;pageCurrent = 1;searchTable()">查询</el-button>
            <!--<el-button type="primary" icon="el-icon-search" size="small" plain style="margin-left: 10px" @click="total=0;pageSize = 20;pageCurrent = 1;loginsession()">session</el-button>-->
            <el-button :disabled="handleEdit_disable || procedureGroup.length === 0" type="distribution" icon="el-icon-top" size="small" plain style="margin-left: 10px" @click="submit()">提交补投</el-button>
          </el-row>
        </el-card>
      </div>
    </div>
    <el-card>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        height="590"
        style="width: 1390px; margin: 3px 0"
        size="mini"
        border
        stripe
        highlight-current-row
        element-loading-text="拼命加载中"
        @selection-change="handleSelectionChange">
        <el-table-column :selectable="selectable" type="selection" align="center" width="45"/>
        <el-table-column label="进程" align="center" width="80px">
          <template slot-scope="scope">
            {{ scope.row.进程 }}
          </template>
        </el-table-column>
        <el-table-column label="机床图号" prop="机床图号" align="center" width="140px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="机床名称" prop="机床名称" align="center" width="140px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column label="组号" prop="组号" align="center" width="100px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.组号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" prop="零件图号" align="center" width="173px" show-overflow-tooltip>
          <!--<el-table-column label="零件图号" prop="零件图号" align="center" width="180" show-overflow-tooltip>-->
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" width="129" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="工序名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.工艺名称 }}
          </template>
        </el-table-column>
        <el-table-column label="加工数" align="center">
          <template slot-scope="scope">
            {{ scope.row.投产数量 }}
          </template>
        </el-table-column>
        <el-table-column label="质检数量" align="center">
          <template slot-scope="scope">
            {{ scope.row.数量 }}
          </template>
        </el-table-column>
        <el-table-column label="不合格数" align="center" width="90px">
          <template slot-scope="scope">
            {{ scope.row.不合格数量 }}
          </template>
        </el-table-column>
        <el-table-column label="不合格原因" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.不合格原因文本 }}
          </template>
        </el-table-column>
        <el-table-column label="处理结果" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.不合格处理文本 }}
          </template>
        </el-table-column>
        <el-table-column label="操作人员" align="center">
          <template slot-scope="scope">
            {{ scope.row.操作工 === '超级管理员' ? '' : scope.row.操作工 }}
          </template>
        </el-table-column>
        <el-table-column label="报废工时(分)" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row.报废工时 }}
          </template>
        </el-table-column>
        <el-table-column label="提交补投" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.报废时间 ? scope.row.报废时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="提交人" align="center" width="90px">
          <template slot-scope="scope">
            {{ scope.row.报废人 }}
          </template>
        </el-table-column>
        <el-table-column label="补投数" align="center" width="90px">
          <template slot-scope="scope">
            {{ scope.row.补投数量 !== 0 || scope.row.补投数量 ? scope.row.补投数量 : '' }}
          </template>
        </el-table-column>
        <el-table-column label="补投时间" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.补投时间 ? scope.row.补投时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="补投人" align="center" width="90px">
          <template slot-scope="scope">
            {{ scope.row.补投人 }}
          </template>
        </el-table-column>
        <el-table-column label="质检备注" align="center" width="120px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="质检人员" align="center">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="质检时间" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.操作时间 ? scope.row.操作时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
  </div>
</template>

<script>
import { searchMachine, searchProcessingResult, searchtable, editSpare } from '@/api/ManufacturingCenter/ScrappedSupplementaryInvestment'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      machineNameValue: '', // 机床图号
      machineName: [],
      partName: '',
      all: false,
      loading: false,
      handleEdit_disable: false,
      ProcessingResultValue: '',
      ProcessingResult: [],
      procedureGroup: [],
      ScrapQuantity: [],
      ProcessPlanNumber: [],
      Unqualified: [],
      remarkGroup: [],
      people: [],
      Data: [],
      tableData: [], // 表格数据
      total: 0, // 总条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      outTime: '',
      order: '',
      operaPeople: [],
      procedureGroup1: []
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.projectChange()
  },
  methods: {
    projectChange() {
      this.machineName = []
      this.machineNameValue = ''
      searchMachine().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineName.push({
            label: response.data[i].机床图号,
            value: response.data[i].机床流水号
          })
        }
      })
      searchProcessingResult().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.ProcessingResult.push({
            label: response.data[i].不合格处理文本,
            value: response.data[i].不合格处理
          })
        }
      })
    },
    exportTable() {
      let check7, check8
      if (this.partName !== '' && this.partName !== null) {
        this.partNamecheck = true
      } else {
        this.partNamecheck = false
      }
      this.outTime ? check7 = 1 : (check7 = 0, this.outTime = '')
      this.all === false ? check8 = 0 : check8 = 1
      var param = []
      param[0] = ['机床流水号', this.machineNameValue]
      param[1] = ['零件图号_check', this.partNamecheck]
      param[2] = ['零件图号', this.partName]
      param[3] = ['投产时间_check', check7]
      if (check7 === 0) {
        param[4] = ['开始时间', '']
        param[5] = ['结束时间', '']
      } else {
        param[4] = ['开始时间', this.outTime[0]]
        param[5] = ['结束时间', this.outTime[1]]
      }
      param[6] = ['加急_check', check8]
      var Data = this.CreateData('2001', '报表_生产计划查询', param)
      this.exportExcel_NPOI(Data)
    },
    // 查询表格数据
    searchTable() {
      if (this.machineNameValue || this.partName || this.outTime) {
        this.loading = true
        let check, check1
        this.machineNameValue ? check = 1 : check = 0
        this.partName ? check1 = 1 : check1 = 0
        let check7, check10
        this.outTime ? check7 = 1 : (check7 = 0, this.outTime = '')
        this.ProcessingResultValue ? check10 = 1 : check10 = 0
        searchtable(check, this.machineNameValue, check1, this.partName, check7, this.outTime[0], this.outTime[1], check10, this.ProcessingResultValue, this.pageCurrent, this.pageSize).then(response => {
          console.log(response.data, 123)
          this.tableData = response.data.result
          this.loading = false
          this.total = parseInt(response.data.output[0].ItemCount)
          this.loading = false
        })
      } else {
        this.$message.warning('请输入筛选条件')
      }
    },
    submit() {
      this.$confirm('是否将零件提交报废?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.ScrapQuantity = []
        this.ProcessPlanNumber = []
        this.Unqualified = []
        this.remarkGroup = []
        this.people = []
        this.operaPeople = []
        this.procedureGroup1 = []
        for (let i = 0; i < this.procedureGroup.length; i++) {
          this.ScrapQuantity.push(this.procedureGroup[i].不合格数量)
          this.ProcessPlanNumber.push(this.procedureGroup[i].工艺计划流水号)
          this.Unqualified.push(this.procedureGroup[i].不合格原因文本)
          this.remarkGroup.push(this.procedureGroup[i].备注)
          this.people.push(this.procedureGroup[i].人员编号)
          this.operaPeople.push(this.procedureGroup[i].操作人员编号)
          this.procedureGroup1.push(this.procedureGroup[i].工序流水号)
        }
        this.handleEdit_disable = true
        editSpare(this.ProcessPlanNumber, this.ScrapQuantity, this.id, this.Unqualified, this.remarkGroup, this.people, this.operaPeople, this.procedureGroup1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('报废成功')
            this.searchTable()
          } else {
            this.handleEdit_disable = false
            this.$message.success('报废失败')
          }
        })
      }).catch((error) => {
        console.log(error)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectable(row) {
      return (row.报废时间 === null && row.不合格处理文本 === '报废')
    },
    handleSelectionChange(val) {
      this.procedureGroup = val
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}

</script>

<style>
  .el-table .UrgentItem{
    background: #FF9797;
  }
</style>

