<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <span>合同号:</span>
        <el-select v-model="contractNumValue" placeholder="合同号" size="small" filterable clearable>
          <el-option
            v-for="item in contractNum"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <div style="float: left">{{ item.label }}</div>
            <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
          </el-option>
        </el-select>
        <span>供应商:</span>
        <el-input v-model="supplierName" placeholder="供应商" size="small" clearable style="width: 200px"/>
        <el-button type="success" size="small" icon="el-icon-search" style="margin-left:10px" @click="total=0;pageCurrent=1;pageSize=10;searchTable()">查询</el-button>
      </el-row>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        size="small"
        highlight-current-row
        min-height="540px"
        height="540px"
        border>
        <el-table-column align="center" label="采购合同编号">
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="供应商">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件号">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件名称" width="130px">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="数量" width="70px">
          <template slot-scope="scope">
            {{ scope.row.数量 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="交检日期 " width="180px">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.交检日期"
              size="small"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 135px"
              @change="update(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="检验数量" width="80px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.检验数量" size="mini" style="width: 50px" align="center" @change="update(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="废品数量" width="80px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.废品数量" size="mini" style="width: 50px" align="center" @change="update(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="检查日期" width="180px">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.检查日期"
              size="small"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 135px"
              @change="update(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="报废原因">
          <template slot-scope="scope">
            <el-input v-model="scope.row.报废原因" size="mini" align="center" @change="update(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注">
          <template slot-scope="scope">
            <el-input v-model="scope.row.备注" size="mini" align="center" @change="update(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="120px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.row)">完成</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 50]"
          :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 { initPayState, searchParts, update, deleteData, editData } from '@/api/WorkshopProcurement/PurchasingQualityInspection'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      check1: 0,
      check2: 0,
      loading: false,
      contractNumValue: '',
      contractNum: [],
      supplierName: '',
      tableData: [],
      pageSize: 10,
      pageCurrent: 1,
      total: 0
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initPayState().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNum.push({
            label: response.data[i].采购合同编号,
            name: response.data[i].采购合同名称,
            value: response.data[i].采购合同流水号
          })
        }
      })
    },
    searchTable() {
      this.loading = true
      this.contractNumValue ? this.check1 = 1 : this.check1 = 0
      this.supplierName ? this.check2 = 1 : this.check2 = 0
      searchParts(this.pageCurrent, this.pageSize, this.check1, this.contractNumValue, this.check2, this.supplierName).then(response => {
        this.loading = false
        console.log(response.data.rows)
        for (let i = 0; i < response.data.rows.length; i++) {
          if (response.data.rows[i].物料型号 === null || response.data.rows[i].物料型号 === '') {
            response.data.rows[i].物料型号 = response.data.rows[i].零件图号
          }
        }
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    update(row) {
      update(row.id, row.交检日期, row.检验数量, row.废品数量, row.检查日期, row.报废原因, row.备注).then(response => {
      })
    },
    handleEdit(row) {
      this.$confirm('是否确认无误?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (row.交检日期 !== '' && row.检查日期 !== '') {
          editData(row.id).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('通过成功')
              this.searchTable()
            } else { this.$message.error('通过失败') }
          })
        } else {
          this.$message.warning('请填写信息')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    handleDelete(row) {
      this.deleteRow(deleteData, row.id, function() { this.searchTable() })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>

</style>
