<template>
  <div class="app-container">
    <el-card style="width: 65%">
      <div style="margin-top: 7px; margin-bottom: 7px">
        <span>外协厂家</span>
        <el-input v-model="supplierNameValue" size="small" placeholder="外协厂家或零件图号" style="width: 180px" clearable/>
        <span>外协日期</span>
        <el-date-picker v-model="timer" type="daterange" value-format="yyyy-MM-dd" size="small" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"/>
        <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>
        <el-button icon="el-icon-search" type="success" size="small" @click="pageCurrent = 1;pageSize = 50;getTableData()">查询</el-button>
        <el-button type="warning" class="el-icon-download" size="small" style="margin: 10px 0 0 10px" @click="exportExcel">导出</el-button>
      </div>
    </el-card>
    <el-card style="width: 70%">
      <el-table
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        stripe
        size="mini"
        style="width: 100%"
        height="760">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column
          label="外协状态"
          align="center"
          width="70px">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.到货状态)!==1" type="warning" size="mini">未到</el-tag>
            <el-tag v-if="Number(scope.row.到货状态)===1" type="success" size="mini">已到</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="项目名称"
          align="center"
          width="140px">
          <template slot-scope="scope">{{ scope.row.项目名称 }}</template>
        </el-table-column>
        <el-table-column
          label="零件图号"
          align="center"
          width="160px">
          <template slot-scope="scope">{{ scope.row.零件图号 }}</template>
        </el-table-column>
        <el-table-column
          label="零件名称"
          align="center"
          width="120px">
          <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" width="100">
          <template slot-scope="scope">
            {{ scope.row.外协工序 }}
          </template>
        </el-table-column>
        <el-table-column
          label="外协厂家"
          align="center"
          width="160px">
          <template slot-scope="scope">{{ scope.row.外协厂家名称 }}</template>
        </el-table-column>
        <el-table-column
          label="外协单号"
          align="center"
          width="200">
          <template slot-scope="scope">{{ scope.row.表单号 }}</template>
        </el-table-column>
        <el-table-column
          label="外协日期"
          align="center"
          width="100">
          <template slot-scope="scope">{{ scope.row.任务下达日期.substr(0,10) }}</template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 50, 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 { searchTable, getprocedure } from '@/api/Outsourcing/OutsourcingPartsQuery'

export default {
  name: 'Index',
  data() {
    return {
      timer: '',
      supplierNameValue: '',
      loading: false,
      total: 0,
      MaterialsValue: '',
      Materials: [{
        value: 0,
        label: '未到'
      }, {
        value: 1,
        label: '已到'
      }],
      tableData: [],
      procedure: [],
      pageCurrent: 1,
      pageSize: 50,
      tableData1: []
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    exportExcel() {
      this.tableData1 = []
      let check, check1, check2
      this.supplierNameValue ? check = 1 : check = 0
      if (this.MaterialsValue !== '' && this.MaterialsValue !== null) {
        check1 = 1
      } else {
        check1 = 0
      }
      if (this.timer === null) {
        this.timer = ''
      }
      if (this.timer[1] === undefined || this.timer[0] === undefined || this.timer[1] === null || this.timer[0] === null) {
        check2 = 0
      } else {
        check2 = 1
      }
      searchTable(check, this.supplierNameValue, check1, this.MaterialsValue, check2, this.timer[0], this.timer[1], 100000000, this.pageCurrent).then(response => {
        for (let i = 0; i < response.data.rows.length; i++) {
          this.tableData1.push({
            到货状态: response.data.rows[i].到货状态,
            项目名称: response.data.rows[i].项目名称,
            零件图号: response.data.rows[i].零件图号,
            零件名称: response.data.rows[i].零件名称,
            批次数量: response.data.rows[i].批次数量,
            外协厂家名称: response.data.rows[i].外协厂家名称,
            表单号: response.data.rows[i].表单号,
            工艺计划流水号: response.data.rows[i].工艺计划流水号,
            单件是否外协: response.data.rows[i].单件是否外协,
            任务下达日期: response.data.rows[i].任务下达日期,
            外协工序: ''
          })
        }
        for (let i = 0; i < this.tableData1.length; i++) {
          getprocedure(this.tableData1[i].工艺计划流水号, this.tableData1[i].单件是否外协).then(response => {
            this.procedure[i] = ''
            for (let j = 0; j < response.data.length; j++) {
              this.procedure[i] = this.procedure[i] + ' ' + response.data[j].工艺名称简称
            }
            this.tableData1[i].外协工序 = this.procedure[i]
          })
        }
      }).then(() => {
        setTimeout(this.exportExcel1, 500)
      })
    },
    exportExcel1() {
      console.log(this.tableData1, 232332)
          import('@/vendor/Export2Excel').then(excel => {
            const tHeader = ['外协状态', '项目名称', '零件图号', '零件名称', '数量', '外协工序', '外协厂家', '外协单号', '外协日期']
            const filterVal = ['到货状态', '项目名称', '零件图号', '零件名称', '批次数量', '外协工序', '外协厂家名称', '表单号', '任务下达日期']
            const list = this.tableData1
            const data = this.formatJson(filterVal, list)
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: '外协零件表',
              autoWidth: true,
              bookType: 'xlsx'
            })
          })
    },
    getTableData() {
      // console.log(this.timer[0], 1)
      // console.log(this.timer[1], 2)
      this.tableData = []
      this.loading = true
      let check, check1, check2
      this.supplierNameValue ? check = 1 : check = 0
      if (this.MaterialsValue !== '' && this.MaterialsValue !== null) {
        check1 = 1
      } else {
        check1 = 0
      }
      // this.timer === null ? (check2 = 0, this.timer = '') : check2 = 1
      if (this.timer === null) {
        this.timer = ''
        console.log(this.timer, 123)
      }
      if (this.timer[1] === undefined || this.timer[0] === undefined || this.timer[1] === null || this.timer[0] === null) {
        check2 = 0
      } else {
        check2 = 1
      }
      console.log(check2, 4564688)
      searchTable(check, this.supplierNameValue, check1, this.MaterialsValue, check2, this.timer[0], this.timer[1], this.pageSize, this.pageCurrent).then(response => {
        for (let i = 0; i < response.data.rows.length; i++) {
          this.tableData.push({
            到货状态: response.data.rows[i].到货状态,
            项目名称: response.data.rows[i].项目名称,
            零件图号: response.data.rows[i].零件图号,
            零件名称: response.data.rows[i].零件名称,
            批次数量: response.data.rows[i].批次数量,
            外协厂家名称: response.data.rows[i].外协厂家名称,
            表单号: response.data.rows[i].表单号,
            工艺计划流水号: response.data.rows[i].工艺计划流水号,
            单件是否外协: response.data.rows[i].单件是否外协,
            任务下达日期: response.data.rows[i].任务下达日期,
            外协工序: ''
          })
        }
        this.getProcedure(this.tableData)
        this.total = response.data.total
        this.loading = false
      })
    },
    getProcedure(row) {
      for (let i = 0; i < row.length; i++) {
        getprocedure(row[i].工艺计划流水号, row[i].单件是否外协).then(response => {
          this.procedure[i] = ''
          for (let j = 0; j < response.data.length; j++) {
            this.procedure[i] = this.procedure[i] + ' ' + response.data[j].工艺名称简称
          }
          this.tableData[i].外协工序 = this.procedure[i]
        })
      }
    },
    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>
