<template>
  <div class="app-container">
    <el-card style="width: 100%">
      <div style="margin-top: 7px; margin-bottom: 7px">
        <el-select v-model="supplierNameValue" style="width:220px" placeholder="供应商" size="small" filterable clearable>
          <el-option
            v-for="item in supplierNames"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-input v-model="partNumber" size="small" placeholder="零件图号" style="width: 180px" clearable/>
        <el-select v-model="MaterialsValue" placeholder="请选择备料状态" filterable size="small" style="width: 140px;margin-left: 10px" clearable>
          <el-option
            v-for="item in Materials"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>备料时间段:</span>
        <el-date-picker
          v-model="dateRange"
          :picker-options="pickerOptions"
          size="small"
          type="daterange"
          align="center"
          style="width: 240px;margin: 3px 0"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="～"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
        <el-button icon="el-icon-search" type="primary" plain size="small" style="margin-left: 10px" @click="pageCurrent = 1;pageSize = 100;getTableData()">查询</el-button>
        <el-button v-positive="'loading'" :disabled="false" type="success" plain icon="el-icon-download" size="small" @click="exportExcel()">导出</el-button>
      </div>
    </el-card>
    <el-card style="width: 100%">
      <el-table v-loading="loading" :data="tableData" highlight-current-row border stripe size="mini" style="width: 100%" height="750" @sort-change="sortChange">
        <el-table-column label="序号" align="center" type="index" width="50"/>
        <el-table-column label="进程" align="center" prop="进程" sortable="custom" width="80px">
          <template slot-scope="scope">
            {{ scope.row.备料状态 }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" min-width="210px" width="300" prop="供应商名称" sortable="custom" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.备料状态==='自家' ? '自家' : scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="物料编码" align="center" width="180px">
          <template slot-scope="scope">
            {{ scope.row.物料编码 ? scope.row.物料编码 : scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="物料名称" align="center" prop="零件名称" sortable="custom" width="120px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="材料" width="90" prop="材料" sortable="custom" align="center">
          <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.到货数量 }}
          </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" prop="下单日期" sortable="custom" width="120">
          <template slot-scope="scope">
            {{ scope.row.下单日期 ? scope.row.下单日期.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="到货时间" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.日期 ? scope.row.日期.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="结算时间" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.开票时间 ? scope.row.开票时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChanges"
          @current-change="handleCurrentChanges"/>
      </div>
    </el-card>
  </div>
</template>

<script>
import { searchSupplier, searchTable, HandleNumber } from '@/api/WorkshopProcurement/OutsourcingMaterialsSearch'
export default {
  name: 'Index',
  data() {
    return {
      ordername: '',
      order: '',
      dateRange: '',
      expands: [],
      pickerOptions: {
        shortcuts: [{
          text: '上季度',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 30 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 366 / 2)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去一年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 365)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      supplierNameValue: '',
      supplierNames: [],
      partNumber: '',
      MaterialsValue: '',
      Materials: [{
        value: 0,
        label: '已结'
      }, {
        value: 1,
        label: '已到'
      }, {
        value: 2,
        label: '未到'
      }, {
        value: 3,
        label: '未采'
      }, {
        value: 4,
        label: '自家'
      }, {
        value: 5,
        label: '未分配'
      }],
      loading: false,
      total: 0,
      tableData: [],
      pageCurrent: 1,
      pageSize: 100
    }
  },
  created() {
    this.fetchData()
    this.HandleNumber()
  },
  methods: {
    HandleNumber() {
      HandleNumber()
    },
    formatJson(column, tableData) {
      return tableData.map(v => column.map(j => {
        return v[j]
      }))
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        let check, check1, check2, check3
        this.dateRange ? check3 = 1 : (check3 = 0, this.dateRange = '')
        this.supplierNameValue ? check = 1 : check = 0
        this.partNumber ? check1 = 1 : check1 = 0
        if (this.MaterialsValue !== '' && this.MaterialsValue !== null) {
          check2 = 1
        } else {
          check2 = 0
        }
        var param = []
        param[0] = ['供应商流水号_check', check]
        param[1] = ['供应商流水号', this.supplierNameValue]
        param[2] = ['零件图号_check', check1]
        param[3] = ['零件图号', this.partNumber]
        param[4] = ['备料状态_check', check2]
        param[5] = ['备料状态', this.MaterialsValue]
        param[6] = ['下单时间_check', check3]
        if (check3 === 0) {
          param[7] = ['开始时间', '']
          param[8] = ['结束时间', '']
        } else {
          param[7] = ['开始时间', this.dateRange[0]]
          param[8] = ['结束时间', this.dateRange[1]]
        }
        param[9] = ['排序名称', this.ordername]
        param[10] = ['排序方式', this.order]
        var Data = this.CreateData('2001', '报表_备料管理_外购备料_综合查询数据', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    fetchData() {
      searchSupplier(0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '').then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.supplierNames.push({
            label: response.data[i].供应商名称,
            value: response.data[i].供应商流水号
          })
        }
      })
    },
    // 按供应商名称，下单日期，零件名称，材料
    sortChange(column) {
      if (column.prop === '供应商名称') {
        this.ordername = 1
      } else if (column.prop === '零件名称') {
        this.ordername = 2
      } else if (column.prop === '材料') {
        this.ordername = 3
      } else if (column.prop === '下单日期') {
        this.ordername = 4
      } else if (column.prop === '进程') {
        this.ordername = 5
      } else {
        this.ordername = ''
      }
      if (column.order === 'ascending') {
        this.order = 1
      } else if (column.order === 'descending') { this.order = 2 } else {
        this.order = ''
      }
      this.getTableData()
    },
    getTableData() {
      this.loading = true
      let check, check1, check2, check3
      this.dateRange ? check3 = 1 : (check3 = 0, this.dateRange = '')
      this.supplierNameValue ? check = 1 : check = 0
      this.partNumber ? check1 = 1 : check1 = 0
      if (this.MaterialsValue !== '' && this.MaterialsValue !== null) {
        check2 = 1
      } else {
        check2 = 0
      }
      searchTable(check, this.supplierNameValue, check1, this.partNumber, check2, this.MaterialsValue, check3, this.dateRange[0], this.dateRange[1], this.ordername, this.order, this.pageSize, this.pageCurrent).then(response => {
        this.tableData = response.data.result
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.getTableData()
    }
  }
}
</script>

<style scoped>
  span{
    margin: 0px;
  }
  .wrapper{
    width: 225px;
    height: 50px;
    overflow: hidden;
    border: 1px solid #b8c7d9;
    float: left;
    margin: 0px;
  }
  .content{
    position: relative;
    padding: 8px 8px 0 8px;
  }
  .action{
    position: absolute;
    top: 4px;
    right: 8px;
  }
  .delete{
    padding: 0 5px;
    border-radius: 3px;
    color: #0f84b0;
    font-size: 12px;
  }
  .delete:hover{
    background: -webkit-linear-gradient(top,#0ba9e3,#0099d3);
    color: #fff;
  }
  a{
    text-decoration: none;
  }
  .icon{
    width: 30px;
    height: 30px;
    display: inline-block;
    margin-right: 7px;
    margin-top: 2px;
    font-size: 26px;
  }
  .info{
    position: absolute;
    left: 44px;
    line-height: 30px;
    display: inline-block;
    color: #999;
  }
  .el-card {
    padding: 0px !important;
    margin: 0px !important;
  }
</style>
