<template>
  <div class="app-container">
    <el-card style="width: 76%">
      <div style="margin-top: 7px; margin-bottom: 7px">
        <span>供应商</span>
        <el-select v-model="supplierNameValue" style="width:200px" placeholder="供应商" size="small" filterable clearable>
          <el-option
            v-for="item in supplierNames"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>零件图号</span>
        <el-input v-model="partNumber" size="small" placeholder="零件图号" style="width: 160px" clearable/>
        <span>备料状态</span>
        <el-select v-model="MaterialsValue" placeholder="请选择备料状态" filterable size="small" style="width: 100px" 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: 300px;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="success" size="small" @click="pageCurrent = 1;pageSize = 100;getTableData()">查询</el-button>
        <el-button type="primary" icon="el-icon-download" size="small" @click="exportExcel()">导出</el-button>
      </div>
    </el-card>
    <el-card style="width: 76%">
      <el-table
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        stripe
        size="mini"
        style="width: 100%"
        height="760"
        @sort-change="sortChange">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column
          label="备料状态"
          align="center"
          width="80px">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.是否到货)===0" type="warning" size="mini">未到</el-tag>
            <el-tag v-if="Number(scope.row.是否到货)===1" type="primary" size="mini">已到</el-tag>
            <el-tag v-if="Number(scope.row.是否到货)===2" type="success" size="mini">已结</el-tag>
          </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.供应商名称 }}</template>
        </el-table-column>
        <el-table-column
          label="物料编码"
          align="center"
          min-width="180px">
          <template slot-scope="scope">{{ 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="70">
          <template slot-scope="scope">
            {{ scope.row.采购数量 }}
          </template>
        </el-table-column>
        <el-table-column label="备料时间" align="center" prop="下单日期" sortable="custom" width="100">
          <template slot-scope="scope">
            {{ scope.row.下单日期 ? scope.row.下单日期.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column
          label="到货件数"
          align="center"
          width="70">
          <template slot-scope="scope">{{ scope.row.到货数量 }}</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, searchTablesheet } 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: '已结'
      }],
      loading: false,
      total: 0,
      tableData: [],
      pageCurrent: 1,
      pageSize: 100
    }
  },
  created() {
    this.fetchData()
    // this.getTableData()
  },
  methods: {
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    exportExcel() {
      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
      }
      searchTablesheet(check, this.supplierNameValue, check1, this.partNumber, check2, this.MaterialsValue, check3, this.dateRange[0], this.dateRange[1], this.ordername, this.order).then(res => {
        console.log(res.data, 11)
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['备料状态', '供应商', '物料编码', '物料名称', '材料', '备料件数', '备料时间', '到货件数', '到货时间', '结算时间']
          const filterVal = ['备料状态', '供应商名称', '物料编码', '零件名称', '材料', '采购数量', '下单日期', '到货数量', '日期', '开票时间']
          const list = res.data
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '外购备料',
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      })
    },
    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) {
      console.log(column + '-' + column.prop + '-' + column.order, 11111)
      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 {
        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 => {
        console.log(response)
        this.tableData = response.data.rows
        this.total = response.data.total
        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>
