<template>
  <div class="app-container">
    <el-card>
      <span>项目名称:</span>
      <el-select v-model="projectValue" placeholder="项目名称" size="small" filterable clearable>
        <el-option
          v-for="item in project"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left:10px"
        @click="total1=0;pageCurrent1=1;pageSize1=10;searchTable1()">查询</el-button>
    </el-card>
    <el-card>
      <el-table v-loading="" :data="tableData2" :summary-method="getSummaries1" show-summary border style="width: 100%;max-height: 500px;" height="300px" size="mini">
        <el-table-column label="项目名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="材料" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.材料 }}
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.数量 }}
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.单价 }}
          </template>
        </el-table-column>
        <el-table-column label="小计(元)" align="center" min-width="100" prop="金额">
          <template slot-scope="scope">
            {{ scope.row.金额 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <el-table v-loading="" :data="tableData" :summary-method="getSummaries" show-summary border style="width: 100%;max-height: 500px;" height="300px" size="mini">
        <el-table-column label="项目名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="物料名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="物料规格" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column label="物料型号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.数量 }}
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.单价 }}
          </template>
        </el-table-column>
        <el-table-column label="小计(元)" align="center" min-width="100" prop="金额">
          <template slot-scope="scope">
            {{ scope.row.金额 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchProjectTotal1, 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].项目流水号
          })
        }
      })
    },
    searchTable1() {
      if (this.projectValue) {
        searchProjectTotal1(this.projectValue).then(response => {
          this.tableData = response.data
        })
        searchProjectTotal2(this.projectValue).then(response => {
          this.tableData2 = response.data
        })
      } else {
        this.$message.error('请选择项目')
      }
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 4) {
          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'
          }
        } else if (index === 6) {
          sums[index] = parseInt(parseInt(sums[5]) * 1.16 * 100) / 100 + ' 元(含税)'
        }
      })
      return sums
    },
    getSummaries1(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 4) {
          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'
          }
        } else if (index === 6) {
          sums[index] = parseInt(parseInt(sums[5]) * 1.16 * 100) / 100 + ' 元(含税)'
        }
      })
      return sums
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .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>
