<template>
  <div class="app-container receiveCheck">
    <el-card>
      <div slot="header">
        <span>项目名称:</span>
        <el-select v-model="projectValue" style="width:200px" placeholder="请选择" size="small" clearable filterable @change="projectChange">
          <el-option
            v-for="item in project"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>机床图号:</span>
        <el-select v-model="machineValue" style="width:190px" placeholder="请选择" size="small" clearable filterable @change="machineChange">
          <el-option
            v-for="item in machine"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>组号:</span>
        <el-select v-model="groupValue" style="width:140px" placeholder="请选择" size="small" clearable filterable>
          <el-option
            v-for="item in group"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 10px" @click="searchProject()">查询</el-button>
      </div>
      <el-table
        :data="tableData1"
        :summary-method="getSummaries"
        style="width: 100%"
        height="650"
        size="mini"
        show-summary
        stripe
        border>
        <el-table-column align="center" label="序号" type="index"/>
        <el-table-column align="center" label="项目名称">
          <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="80px">
          <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="物料型号">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="小计(元)" prop="小计">
          <template slot-scope="scope">
            {{ scope.row.小计 ? scope.row.小计.toFixed(2) : 0 }}
          </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="领用时间">
          <template slot-scope="scope">
            {{ scope.row.出库时间 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchMachine, searchGroup, searchReceiveCheck } from '@/api/PurchasingCenter/ReceiveCheck'
export default {
  data() {
    return {
      projectValue: '', // 项目名称
      project: [],
      machineValue: '',
      machine: [],
      groupValue: '',
      group: [],
      tableData1: [],
      check1: '',
      pageCurrent: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'project')
    },
    projectChange() {
      this.machine = []
      this.machineValue = ''
      this.group = []
      this.groupValue = ''
      searchMachine(this.projectValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machine.push({
            value: response.data[i].机床流水号,
            label: response.data[i].机床图号
          })
        }
      })
    },
    machineChange() {
      this.group = []
      this.groupValue = ''
      searchGroup(this.machineValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.group.push({
            value: response.data[i].组件流水号,
            label: response.data[i].组号
          })
        }
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(2)
          sums[index] += ' 元'
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    searchProject() { // 查询
      if (!this.projectValue) {
        this.$message.error('请选择项目')
      } else {
        let check2, check3
        this.machineValue ? check2 = 1 : check2 = 0
        this.groupValue ? check3 = 1 : check3 = 0
        searchReceiveCheck(1, this.projectValue, check2, this.machineValue, check3, this.groupValue).then(response => {
          this.tableData1 = response.data
        })
      }
    }
  }
}
</script>

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