<template>
  <div class="app-container">
    <el-card>
      <span>机床编号:</span>
      <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="machineChange">
        <el-option
          v-for="item in machineNumber"
          :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-select v-model="groupNumberValue" placeholder="组号" size="small" clearable>
        <el-option
          v-for="item in groupNumber"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button type="success" size="small" icon="el-icon-search" style="margin-left: 15px" @click="searchTable()">查询</el-button>
    </el-card>

    <el-tabs v-model="PartType" type="border-card" @tab-click="searchTable()">
      <el-tab-pane label="标准件和外购件" name="标准件和外购件">
        <el-table :data="tableData1" size="mini" style="max-height: 600px" height="600px" border stripe>
          <el-table-column align="center" label="采购情况" width="80px">
            <template slot-scope="scope">
              <el-tag v-if="!scope.row.人员编号" type="primary" size="small">未分配</el-tag>
              <el-tag v-if="parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!==''" type="warning" size="small">未询价</el-tag>
              <el-tag v-if="parseInt(scope.row.询价状态编号)===3&&parseInt(scope.row.采购状态编号)!==3" type="success" size="small">已询价</el-tag>
              <el-tag v-if="parseInt(scope.row.采购状态编号)===3" type="info" size="small">已采购</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="机床图号" width="100px">
            <template slot-scope="scope">
              {{ scope.row.机床图号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="组号" width="80px">
            <template slot-scope="scope">
              {{ scope.row.组号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="名称" width="100px">
            <template slot-scope="scope">
              {{ scope.row.物料名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="型号" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.物料型号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="规格" min-width="150px">
            <template slot-scope="scope">
              {{ scope.row.物料规格 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="技术下达数量" width="100px">
            <template slot-scope="scope">
              {{ scope.row.零件数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购数量" width="100px">
            <template slot-scope="scope">
              {{ scope.row.数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="到货数量" width="100px">
            <template slot-scope="scope">
              {{ scope.row.已到货数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供应商" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.供应商名称 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购合同编号" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.采购合同编号 || '—' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :current-page="pageCurrent1"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize1"
            :total="total1"
            style="margin-top:10px;display:inline-block;width:50%"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange1"
            @current-change="handleCurrentChange1"/>
        </div>
      </el-tab-pane>
      <el-tab-pane label="基本件" name="基本件">
        <el-table :data="tableData2" size="mini" style="max-height: 600px" height="600px" border stripe>
          <el-table-column align="center" label="采购情况" width="80px">
            <template slot-scope="scope">
              <el-tag v-if="!scope.row.人员编号" type="primary" size="small">未分配</el-tag>
              <el-tag v-if="parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!==''" type="warning" size="small">未询价</el-tag>
              <el-tag v-if="parseInt(scope.row.询价状态编号)===3&&parseInt(scope.row.采购状态编号)!==3" type="success" size="small">已询价</el-tag>
              <el-tag v-if="parseInt(scope.row.采购状态编号)===3" type="info" size="small">已采购</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="机床图号" width="100px">
            <template slot-scope="scope">
              {{ scope.row.机床图号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="组号" width="80px">
            <template slot-scope="scope">
              {{ scope.row.组号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="名称" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.零件名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="图号" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.零件图号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="规格" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.规格 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="材料" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.材料 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="技术下达数量" width="100px">
            <template slot-scope="scope">
              {{ scope.row.零件数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购数量" width="100px">
            <template slot-scope="scope">
              {{ scope.row.数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="到货数量" width="100px">
            <template slot-scope="scope">
              {{ scope.row.已到货数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供应商" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.供应商名称 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购合同编号" min-width="100px">
            <template slot-scope="scope">
              {{ scope.row.采购合同编号 || '—' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :current-page="pageCurrent2"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize2"
            :total="total2"
            style="margin-top:10px;display:inline-block;width:50%"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"/>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { initMachineProject, searchgroup, searchTable1, searchTable2 } from '@/api/PurchasingCenter/PurchaseSituationSearch'
import { mapGetters } from 'vuex'
export default {
  name: '', // 采购件采购查询
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNumber: [],
      PartType: '标准件和外购件',
      tableData1: [],
      tableData2: [],
      tableData3: [],
      pageCurrent1: 1,
      pageSize1: 20,
      total1: 0,
      pageCurrent2: 1,
      pageSize2: 20,
      total2: 0
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
    this.searchTable()
  },
  methods: {
    fetchData() {
      initMachineProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    machineChange() {
      this.groupNumberValue = ''
      this.groupNumber = []
      searchgroup(this.machineNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groupNumber.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    searchTable() {
      if (this.PartType === '标准件和外购件') {
        this.pageCurrent1 = 1
        this.pageSize1 = 20
        this.total1 = 0
        this.searchTable1()
      } else if (this.PartType === '基本件') {
        this.pageCurrent2 = 1
        this.pageSize2 = 20
        this.total2 = 0
        this.searchTable2()
      }
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.pageCurrent1 = 1
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    handleSizeChange2(val) {
      this.pageSize2 = val
      this.pageCurrent2 = 1
      this.searchTable2()
    },
    handleCurrentChange2(val) {
      this.pageCurrent2 = val
      this.searchTable2()
    },
    searchTable1() { // 查询标准件和外购件
      let check1, check2
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      searchTable1(check1, this.machineNumberValue, check2, this.groupNumberValue, this.pageCurrent1, this.pageSize1).then(response => {
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    searchTable2() { // 查询基本件
      let check1, check2
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      searchTable2(check1, this.machineNumberValue, check2, this.groupNumberValue, this.pageCurrent2, this.pageSize2).then(response => {
        this.tableData2 = response.data.rows
        this.total2 = response.data.total
      })
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .el-tag{
    margin: 0
  }
</style>
