<template>
  <div class="app-container">
    <el-card style="width: 44%">
      <span>项目名称:</span>
      <el-select v-model="projectValue" placeholder="项目名称" size="small" style="width: 180px" filterable clearable>
        <el-option
          v-for="item in project"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button
        type="primary"
        size="small"
        plain
        icon="el-icon-search"
        style="margin-left:10px"
        @click="searchTable1()">查询</el-button>
      <el-button
        type="primary"
        size="small"
        plain
        icon="el-icon-download"
        style="margin-left:10px"
        @click="exportExcel()">导出项目采购明细</el-button>
    </el-card>
    <el-card style="width: 54%">
      <el-table v-loading="" :data="tableData2" :summary-method="getSummaries1" show-summary border style="width: 100%;max-height: 700px;" height="700px">
        <el-table-column label="零件名称" align="center" width="150">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="图号或型号" align="center" width="150">
          <template slot-scope="scope">
            {{ scope.row.图号或型号 }}
          </template>
        </el-table-column>
        <el-table-column label="材料或规格" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.材料或规格 }}
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.数量 }}
          </template>
        </el-table-column>
        <el-table-column label="单价(元)" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.单价.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="小计(元)" align="center" width="120" prop="金额">
          <template slot-scope="scope">
            {{ scope.row.金额.toFixed(2) }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchProjectTotal2 } from '@/api/WorkshopProcurement/ProjectTotalSearch'
import { mapGetters } from 'vuex'

export default {
  name: '', // 项目总价查询
  data() {
    return {
      projectValue: '',
      project: [],
      tableData: [],
      tableData2: []
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.project.push({
            label: response.data[i].项目名称,
            value: response.data[i].项目流水号
          })
        }
      })
    },
    exportExcel() {
      var param = []
      param[0] = ['项目流水号', this.projectValue]
      var Data = this.CreateData('2001', '报表_项目采购明细（制造）', param)
      this.exportExcel_NPOI(Data)
    },
    searchTable1() {
      if (this.projectValue) {
        searchProjectTotal2(this.projectValue).then(response => {
          this.tableData2 = response.data
        })
      } else {
        this.$message.error('请选择项目')
      }
    },
    getSummaries1(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (index === 5) {
          const values = data.map(item => Number(item['金额']))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return Number(prev) + Number(curr)
              } else {
                return Number(prev)
              }
            }, 0)
            sums[index] += ' 元(含税)'
          } else {
            sums[index] = 'N/A'
          }
        }
      })
      return sums
    }
  }
}
</script>

<style scoped>
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
  .el-tag{
    margin: 0
  }
  .el-date-editor{
    width:300px
  }
</style>

