<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>
      <span>物料名称:</span>
      <el-input v-model="materialName" placeholder="物料名称" size="small" clearable/>
      <span>采购员:</span>
      <el-select v-model="personValue" placeholder="采购员" size="small" clearable style="width: 100px">
        <el-option
          v-for="item in person"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>采购情况:</span>
      <el-select v-model="purchaseStateValue" placeholder="采购情况" size="small" style="width: 100px">
        <el-option
          v-for="item in purchaseState"
          :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="pageCurrent1=1;pageSize1=20;total1=0;searchTable1()">查询</el-button>
    </el-card>

    <el-card>
      <el-table v-loading="" :data="tableData1" size="mini" style="max-height: 600px" height="600px" border stripe>
        <el-table-column align="center" label="采购情况" min-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="物料名称" min-width="150px">
          <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="物料规格" min-width="150px">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件数量" min-width="80px">
          <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="计划到货时间" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.物料计划到货时间.split(' ')[0] }}
          </template>
        </el-table-column>
        <el-table-column :filters="person" :filter-method="filterHandler" align="center" label="采购员" min-width="100px" prop="buyer">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>
  </div>
</template>

<script>
import { initProject, initBuyer, searchTable1 } from '@/api/PurchasingCenter/ContractStateSearch'
import { mapGetters } from 'vuex'

export default {
  name: '', // 采购任务分配
  data() {
    return {
      projectValue: '',
      project: [],
      materialName: '',
      personValue: '',
      person: [],
      purchaseStateValue: 0,
      purchaseState: [
        {
          value: 0,
          label: '全部'
        },
        {
          value: 1,
          label: '未分配'
        },
        {
          value: 2,
          label: '未询价'
        },
        {
          value: 3,
          label: '已询价'
        },
        {
          value: 4,
          label: '已采购'
        }
      ],
      check1: 0,
      check2: 0,
      check3: 0,
      tableData1: [],
      pageCurrent1: 1,
      pageSize1: 20,
      total1: 0
    }
  },
  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].项目流水号
          })
        }
      })
      initBuyer().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.person.push({
            text: response.data[i].姓名,
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    searchTable1() { // 查询
      this.projectValue ? this.check1 = 1 : this.check1 = 0
      this.materialName ? this.check2 = 1 : this.check2 = 0
      this.personValue ? this.check3 = 1 : this.check3 = 0
      searchTable1(this.pageCurrent1, this.pageSize1, this.check1, this.projectValue, this.check2, this.materialName, this.check3, this.personValue, this.purchaseStateValue).then(response => {
        console.log(response.data)
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    filterHandler(value, row) {
      console.log(row, value)
      return parseInt(row['人员编号']) === parseInt(value)
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.pageCurrent1 = 1
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    }
  }
}
</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
  }
</style>
