<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <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>组&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</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>
        <span>名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称:</span>
        <el-input v-model="Name" placeholder="名称" size="small" clearable style="width:200px"/>
      </el-row>
      <el-row style="margin-top:10px">
        <span>规&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格:</span>
        <el-input v-model="spec" placeholder="规格" size="small" clearable style="width:200px"/>
        <span>型&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</span>
        <el-input v-model="model" placeholder="型号" size="small" clearable style="width:200px"/>
        <el-button type="success" size="small" icon="el-icon-search" style="margin-left: 15px" @click="searchTable()">查询</el-button>
        <el-tooltip :open-delay="1000" effect="dark" content="将全部采购件导出到表格" placement="top">
          <el-button type="warning" size="small" icon="el-icon-download" style="margin-left: 15px" @click="exportDetail()">导出</el-button>
        </el-tooltip>
      </el-row>
    </el-card>

    <el-tabs v-model="PartType" type="border-card" @tab-click="searchTable()">
      <el-tab-pane label="标准件" name="标准件">
        <el-table :data="tableData0" 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&&parseInt(scope.row.采购状态编号)===3" type="info" size="small">已采购</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="机床图号" width="100px" show-overflow-tooltip>
            <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" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.物料名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="型号" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.物料型号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="规格" min-width="200px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.物料规格 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供货商" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.供货商 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="备注" min-width="100px" show-overflow-tooltip>
            <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">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.数量 || scope.row.离线采购数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="到货数量" width="100px">
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.已到货数量 || scope.row.离线到货数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供应商" min-width="150px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.供应商名称 || scope.row.离线合同供应商名称 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="合同编号" min-width="100px">
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.采购合同编号 || 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>
        <div class="block">
          <el-pagination
            :current-page="pageCurrent0"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize0"
            :total="total0"
            style="margin-top:10px;display:inline-block;width:50%"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange0"
            @current-change="handleCurrentChange0"/>
        </div>
      </el-tab-pane>
      <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&&parseInt(scope.row.采购状态编号)===3" type="info" size="small">已采购</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="机床图号" width="100px" show-overflow-tooltip>
            <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" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.物料名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="型号" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.物料型号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="规格" min-width="200px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.物料规格 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供货商" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.供货商 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="备注" min-width="100px" show-overflow-tooltip>
            <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">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.数量 || scope.row.离线采购数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="到货数量" width="100px">
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.已到货数量 || scope.row.离线到货数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供应商" min-width="150px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.供应商名称 || scope.row.离线合同供应商名称 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="合同编号" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.采购合同编号 || 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>
        <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&&parseInt(scope.row.采购状态编号)===3" type="info" size="small">已采购</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="机床图号" width="100px" show-overflow-tooltip>
            <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" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.零件名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="图号" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.零件图号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="规格" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.规格 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="材料" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.材料 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供货商" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.供货商 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="备注" min-width="100px" show-overflow-tooltip>
            <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">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="到货数量" width="100px">
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.已到货数量 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="供应商" min-width="100px" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : scope.row.供应商名称 || '—' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购合同编号" min-width="100px">
            <template slot-scope="scope">
              {{ parseInt(scope.row.询价状态编号)===1&&scope.row.人员编号!=='' ? '—' : 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>
        <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, searchTable1Export, searchTable2Export } from '@/api/PurchasingCenter/PurchaseSituationSearch'
import { mapGetters } from 'vuex'
export default {
  name: '', // 采购件采购查询
  data() {
    return {
      Name: '',
      spec: '',
      model: '',
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNumber: [],
      PartType: '标准件',
      tableData0: [],
      tableData1: [],
      tableData2: [],
      tableData3: [],
      pageCurrent0: 1,
      pageSize0: 20,
      total0: 0,
      pageCurrent1: 1,
      pageSize1: 20,
      total1: 0,
      pageCurrent2: 1,
      pageSize2: 20,
      total2: 0
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
    this.searchTable()
  },
  methods: {
    // 导出采购物料执行情况
    exportDetail() {
      let check1, check2
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      searchTable1Export(check1, this.machineNumberValue, check2, this.groupNumberValue, 1).then(res1 => { // 标准件
        searchTable1Export(check1, this.machineNumberValue, check2, this.groupNumberValue, 2).then(res2 => { // 外购件
          searchTable2Export(check1, this.machineNumberValue, check2, this.groupNumberValue).then(res3 => { // 基本件
            import('./Export2Excel').then(excel => {
              const dataGroup = []
              const filterVal1 = ['机床图号', '组号', '物料名称', '物料型号', '物料规格', '零件数量', ['数量', '离线采购数量'], ['已到货数量', '离线到货数量'], ['供应商名称', '离线合同供应商名称'], ['采购合同编号', '离线合同编号'], '姓名', '供货商', '备注'] // tableData0里的属性名
              const filterVal2 = ['机床图号', '组号', '物料名称', '物料型号', '物料规格', '零件数量', ['数量', '离线采购数量'], ['已到货数量', '离线到货数量'], ['供应商名称', '离线合同供应商名称'], ['采购合同编号', '离线合同编号'], '姓名', '供货商', '备注'] // tableData1里的属性名
              const filterVal3 = ['机床图号', '组号', '零件名称', '零件图号', '规格', '材料', '零件数量', '数量', '已到货数量', '供应商名称', '采购合同编号', '姓名', '供货商', '备注'] // tableData2里的属性名
              const data1 = res1.data.map(v => filterVal1.map(j => {
                if (typeof (j) === 'string') {
                  return v[j] || '—'
                } else {
                  return v[j[0]] || v[j[1]] || '—'
                }
              }))
              data1.map(v => {
                v.push('') // 物料状态
                if (v[10] === '—') {
                  v[13] = '未分配'
                } else if (v[6] === '—') {
                  v[13] = '未采购'
                } else if (v[6] !== '—' && v[6] !== v[7]) {
                  v[13] = '未全部到货'
                } else if (v[6] !== '—' && v[6] === v[7]) {
                  v[13] = '全部到货'
                }
              })
              dataGroup.push(data1)
              const data2 = res2.data.map(v => filterVal2.map(j => {
                if (typeof (j) === 'string') {
                  return v[j] || '—'
                } else {
                  return v[j[0]] || v[j[1]] || '—'
                }
              }))
              data2.map(v => {
                v.push('') // 物料状态
                if (v[10] === '—') {
                  v[13] = '未分配'
                } else if (v[6] === '—') {
                  v[13] = '未采购'
                } else if (v[6] !== '—' && v[6] !== v[7]) {
                  v[13] = '未全部到货'
                } else if (v[6] !== '—' && v[6] === v[7]) {
                  v[13] = '全部到货'
                }
              })
              dataGroup.push(data2)
              const data3 = res3.data.map(v => filterVal3.map(j => {
                if (typeof (j) === 'string') {
                  return v[j] || '—'
                } else {
                  return v[j[0]] || v[j[1]] || '—'
                }
              }))
              data3.map(v => {
                v.push('') // 物料状态
                if (v[11] === '—') {
                  v[14] = '未分配'
                } else if (v[7] === '—') {
                  v[14] = '未采购'
                } else if (v[7] !== '—' && v[7] !== v[8]) {
                  v[14] = '未全部到货'
                } else if (v[7] !== '—' && v[7] === v[8]) {
                  v[14] = '全部到货'
                }
              })
              dataGroup.push(data3)
              const headerGroup = [['机床号', '组号', '名称', '图号或型号', '规格', '技术下达数量', '合同采购数量', '到货数量', '供应商', '合同编号', '采购员', '供货商', '备注', '物料状态'],
                ['机床号', '组号', '名称', '图号或型号', '规格', '技术下达数量', '合同采购数量', '到货数量', '供应商', '合同编号', '采购员', '供货商', '备注', '物料状态'],
                ['机床号', '组号', '名称', '图号或型号', '规格', '材料', '技术下达数量', '合同采购数量', '到货数量', '供应商', '合同编号', '采购员', '供货商', '备注', '物料状态']]
              const sheetGroup = ['标准件', '外购件', '基本件']
              excel.export_json_to_excel({
                headerGroup: headerGroup,
                dataGroup: dataGroup,
                sheetGroup: sheetGroup,
                filename: '采购物料执行情况',
                autoWidth: true,
                bookType: 'xlsx'
              })
            })
          })
        })
      })
    },
    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.searchTable0()
      } else if (this.PartType === '外购件') {
        this.searchTable1()
      } else if (this.PartType === '基本件') {
        this.searchTable2()
      }
    },
    handleSizeChange0(val) {
      this.pageSize0 = val
      this.pageCurrent0 = 1
      this.searchTable0()
    },
    handleCurrentChange0(val) {
      this.pageCurrent0 = val
      this.searchTable0()
    },
    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()
    },
    searchTable0() { // 查询标准件
      let check1, check2, check3, check4, check5
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      this.Name ? check3 = 1 : check3 = 0
      this.spec ? check4 = 1 : check4 = 0
      this.model ? check5 = 1 : check5 = 0
      searchTable1(check1, this.machineNumberValue, check2, this.groupNumberValue, this.pageCurrent0, this.pageSize0, 1, check3, this.Name, check4, this.spec, check5, this.model).then(response => {
        console.log(this.tableData0)
        this.tableData0 = response.data.rows
        this.total0 = response.data.total
      })
    },
    searchTable1() { // 查询外购件
      let check1, check2, check3, check4, check5
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      this.Name ? check3 = 1 : check3 = 0
      this.spec ? check4 = 1 : check4 = 0
      this.model ? check5 = 1 : check5 = 0
      searchTable1(check1, this.machineNumberValue, check2, this.groupNumberValue, this.pageCurrent1, this.pageSize1, 2, check3, this.Name, check4, this.spec, check5, this.model).then(response => {
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    searchTable2() { // 查询基本件
      let check1, check2, check3, check4, check5
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      this.Name ? check3 = 1 : check3 = 0
      this.spec ? check4 = 1 : check4 = 0
      this.model ? check5 = 1 : check5 = 0
      searchTable2(check1, this.machineNumberValue, check2, this.groupNumberValue, this.pageCurrent2, this.pageSize2, check3, this.Name, check4, this.spec, check5, this.model).then(response => {
        this.tableData2 = response.data.rows
        this.total2 = response.data.total
      })
    }
  }
}
</script>

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