<template>
  <div class="app-container">

    <el-card>
      <span>合同号:</span>
      <el-input v-model="contractNum" placeholder="合同号" size="small" clearable style="width:200px"/>
      <span>外协厂家:</span>
      <el-select v-model="outHelpValue" size="small" filterable remote clearable placeholder="外协厂家">
        <el-option
          v-for="item in outHelp"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>零件号:</span>
      <el-input v-model="partNum" placeholder="零件号" size="small" clearable style="width:200px"/>
      <el-button type="success" size="small" icon="el-icon-search" style="margin-left: 10px;" @click="PageCurrent=1;PageSize=10;searchTable()">查询</el-button>
    </el-card>

    <el-card>
      <el-table v-loading="loading0" :data="tableData" border style="width: 100%;" height="650" stripe>
        <el-table-column label="合同号" width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.表单号 }}
          </template>
        </el-table-column>
        <el-table-column label="厂家" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.外协厂家简称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件号" width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" width="140" align="center">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="数量" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.批次数量 }}
          </template>
        </el-table-column>
        <el-table-column label="保存" width="100" align="center">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-cjn-09" type="primary" @click="saveRow(scope.row)">保存</el-button>
          </template>
        </el-table-column>
        <el-table-column label="废品" width="80" align="center">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].废品数量" size="mini" disabled/>
          </template>
        </el-table-column>
        <el-table-column label="1检" align="center">
          <el-table-column label="交检日期" width="160" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :readonly="readonly1[scope.$index]"
                v-model="tableData[scope.$index].交检日期1"
                size="mini"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
                placeholder="外协科长填选"/>
            </template>
          </el-table-column>
          <el-table-column label="交检数量" width="80" align="center">
            <template slot-scope="scope">
              <el-input v-model="tableData[scope.$index].交检数量1" :readonly="readonly1[scope.$index]" size="mini"/>
            </template>
          </el-table-column>
          <el-table-column label="检查日期" width="160" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="disabled2[scope.$index]"
                :readonly="true"
                v-model="tableData[scope.$index].检查日期1"
                size="mini"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
                placeholder="检查员填选"/>
            </template>
          </el-table-column>
          <el-table-column label="合格数量" width="80" align="center">
            <template slot-scope="scope">
              <el-input v-model="tableData[scope.$index].合格数量1" :disabled="disabled2[scope.$index]" :readonly="true" size="mini"/>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="2检" align="center">
          <el-table-column label="交检日期" width="160" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="disabled3[scope.$index]"
                :readonly="readonly2[scope.$index]"
                v-model="tableData[scope.$index].交检日期2"
                size="mini"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
                placeholder="外协科长填选"/>
            </template>
          </el-table-column>
          <el-table-column label="交检数量" width="80" align="center">
            <template slot-scope="scope">
              <el-input v-model="tableData[scope.$index].交检数量2" :disabled="disabled3[scope.$index]" :readonly="readonly2[scope.$index]" size="mini"/>
            </template>
          </el-table-column>
          <el-table-column label="检查日期" width="160" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="disabled4[scope.$index]"
                :readonly="true"
                v-model="tableData[scope.$index].检查日期2"
                size="mini"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
                placeholder="检查员填选"/>
            </template>
          </el-table-column>
          <el-table-column label="合格数量" width="80" align="center">
            <template slot-scope="scope">
              <el-input v-model="tableData[scope.$index].合格数量2" :disabled="disabled4[scope.$index]" :readonly="true" size="mini"/>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="3检" align="center">
          <el-table-column label="交检日期" width="160" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="disabled5[scope.$index]"
                v-model="tableData[scope.$index].交检日期3"
                size="mini"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
                placeholder="外协科长填选"/>
            </template>
          </el-table-column>
          <el-table-column label="交检数量" width="80" align="center">
            <template slot-scope="scope">
              <el-input v-model="tableData[scope.$index].交检数量3" :disabled="disabled5[scope.$index]" size="mini"/>
            </template>
          </el-table-column>
          <el-table-column label="检查日期" width="160" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="disabled6[scope.$index]"
                :readonly="true"
                v-model="tableData[scope.$index].检查日期3"
                size="mini"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
                placeholder="检查员填选"/>
            </template>
          </el-table-column>
          <el-table-column label="合格数量" width="80" align="center">
            <template slot-scope="scope">
              <el-input v-model="tableData[scope.$index].合格数量3" :disabled="disabled6[scope.$index]" :readonly="true" size="mini"/>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>

      <div class="block" style="margin-top: 10px;">
        <el-pagination
          v-if="!loading0"
          :current-page="PageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :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 { initOutHelp, searchTable, saveRow } from '@/api/Outsourcing/OutsourcingInspectionEntry'
export default {
  data() {
    return {
      loading0: false,
      contractNum: '',
      outHelpValue: '',
      outHelp: [],
      partNum: '',
      tableData: [],
      readonly1: [],
      readonly2: [],
      readonly3: [],
      disabled2: [],
      disabled3: [],
      disabled4: [],
      disabled5: [],
      disabled6: [],
      PageCurrent: 1,
      PageSize: 10,
      total: null
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.outHelp = []
      initOutHelp().then(response => { // 初始化外协厂家；*这里模糊查询省一个存储过程
        for (let i = 0; i < response.data.length; i++) {
          this.outHelp.push({
            label: response.data[i].外协厂家名称,
            value: response.data[i].外协厂家流水号
          })
        }
      })
    },
    searchTable() {
      this.loading0 = true
      this.tableData = []
      if (this.contractNum === '') {
        this.check1 = 0
      } else { this.check1 = 1 }
      if (this.outHelpValue === '') {
        this.check2 = 0
      } else { this.check2 = 1 }
      if (this.partNum === '') {
        this.check3 = 0
      } else { this.check3 = 1 }
      searchTable(this.PageCurrent, this.PageSize, this.check2, this.outHelpValue, this.check1, this.contractNum, this.check3, this.partNum).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
        this.judge()
        this.loading0 = false
      })
    },
    // 判断进度
    judge() {
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].合格数量1 === null || this.tableData[i].合格数量1 === '') {
          this.readonly1[i] = false
        } else {
          this.readonly1[i] = true
        }
        if (this.tableData[i].合格数量2 === null || this.tableData[i].合格数量2 === '') {
          this.readonly2[i] = false
        } else {
          this.readonly2[i] = true
        }
        if (this.tableData[i].合格数量3 === null || this.tableData[i].合格数量3 === '') {
          this.readonly3[i] = false
        } else {
          this.readonly3[i] = true
        }
        if (this.tableData[i].交检数量1 === null || this.tableData[i].交检数量1 === '') {
          this.disabled2[i] = true
        } else {
          this.disabled2[i] = false
        }
        if (this.tableData[i].合格数量1 === null || this.tableData[i].合格数量1 === '') {
          this.disabled3[i] = true
        } else {
          this.disabled3[i] = false
        }
        if (this.tableData[i].交检数量2 === null || this.tableData[i].交检数量2 === '') {
          this.disabled4[i] = true
        } else {
          this.disabled4[i] = false
        }
        if (this.tableData[i].合格数量2 === null || this.tableData[i].合格数量2 === '') {
          this.disabled5[i] = true
        } else {
          this.disabled5[i] = false
        }
        if (this.tableData[i].交检数量3 === null || this.tableData[i].交检数量3 === '') {
          this.disabled6[i] = true
        } else {
          this.disabled6[i] = false
        }
      }
    },
    saveRow(row) {
      if (parseInt(row.交检数量1) > row.批次数量 || parseInt(row.交检数量1) + parseInt(row.交检数量2) > row.批次数量 || parseInt(row.交检数量1) + parseInt(row.交检数量2) + parseInt(row.交检数量3) > row.批次数量) {
        this.$message.error('超出零件数量范围')
      } else {
        if (row.交检日期1 === null) { row.交检日期1 = '' }
        if (row.交检数量1 === null) { row.交检数量1 = '' }
        if (row.检查日期1 === null) { row.检查日期1 = '' }
        if (row.合格数量1 === null) { row.合格数量1 = '' }
        if (row.交检日期2 === null) { row.交检日期2 = '' }
        if (row.交检数量2 === null) { row.交检数量2 = '' }
        if (row.检查日期2 === null) { row.检查日期2 = '' }
        if (row.合格数量2 === null) { row.合格数量2 = '' }
        if (row.交检日期3 === null) { row.交检日期3 = '' }
        if (row.交检数量3 === null) { row.交检数量3 = '' }
        if (row.检查日期3 === null) { row.检查日期3 = '' }
        if (row.合格数量3 === null) { row.合格数量3 = '' }
        saveRow(row.外协加工任务单明细流水号, row.交检日期1, row.交检数量1, row.检查日期1, row.合格数量1, row.交检日期2, row.交检数量2, row.检查日期2, row.合格数量2, row.交检日期3, row.交检数量3, row.检查日期3, row.合格数量3, row.废品数量).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('保存成功')
            // this.PageCurrent = 1
            // this.PageSize = 10
            // this.searchTable()
          } else { this.$message.error('保存失败') }
        })
      }
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  .doing-row4{
    background: #f0f9eb;
  }
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:130px
  }
  span{
    margin: 10px 0 10px 10px;
  }
</style>
