<template>
  <div>
    <el-card>
      <el-row>
        <span>零件图号:</span>
        <el-input v-model="partName" placeholder="零件图号" size="small" clearable style="width:200px"/>
        <el-button size="small" icon="el-icon-search" type="success" style="margin-left: 10px" @click="pageSize=10; pageCurrent=1;searchTable()">查询</el-button>
        <el-checkbox v-model="all" size="mini">查询全部</el-checkbox>
        <el-button type="primary" size="small" icon="el-icon-download" style="margin-left:10px" @click="exportTable('BL')">导出备料单</el-button>
        <el-button type="primary" size="small" icon="el-icon-success" style="margin-left:10px" @click="edit()">自家备料</el-button>
        <el-button type="primary" size="small" icon="el-icon-success" style="margin-left:10px" @click="edit2()">外购备料</el-button>
      </el-row>
    </el-card>

    <el-card>
      <el-table v-loading="loading" :data="tableData" height="580" style="width: 100%;" border element-loading-text="拼命加载中" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
          align="center"/>
        <el-table-column label="打印状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.备料打印!=='0'" type="success" size="small">已导出</el-tag>
            <el-tag v-else type="warning" size="small">未导出</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" width="240px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.批次数量 }}
          </template>
        </el-table-column>
        <el-table-column label="材料" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.材料 }}
          </template>
        </el-table-column>
        <el-table-column label="重量" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.重量 }}
          </template>
        </el-table-column>
        <el-table-column label="备料" align="center">
          <template slot-scope="scope">
            {{ scope.row.工艺要求 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <div v-show="false" id="BL">
      <table border cellspacing="0px" style="table-layout: fixed; width: 600px;">
        <colgroup>
          <col style="width: 120px">
          <col style="width: 180px">
          <col style="width: 110px">
          <col style="width: 110px">
          <col style="width: 110px">
          <col style="width: 450px">
        </colgroup>
        <tr>
          <th colspan="2" height="60px">GAOJING</th>
          <th colspan="4" height="60px"><span style="font-weight:bold">备料单</span></th>
        </tr>
        <tr>
          <td align="center">零件名称</td>
          <td align="center">零件图号</td>
          <td align="center">材料</td>
          <td align="center">原材料规格</td>
          <td align="center">批次数量</td>
          <td align="center">备料</td>
        </tr>
        <tr v-for="(list, index) in tableData2" :key="index">
          <td align="center">{{ list.零件名称 }}</td>
          <td align="center">{{ list.零件图号 }}</td>
          <td align="center">{{ list.材料 }}</td>
          <td align="center">{{ list.原材料规格 }}</td>
          <td align="center">{{ list.批次数量 }}</td>
          <td>{{ list.工艺要求 }}</td>
        </tr>
      </table>
    </div>

  </div>
</template>

<script>
import { searchtable, searchtable2, edit, edit2 } from '@/api/ManufacturingCenter/CreatePreparationBill'
import { exportExcelMethod } from './exportExcel'
import $ from 'jquery'
export default {
  data() {
    return {
      a: 0,
      tableData2: [],
      partName: '',
      all: false,
      loading: false,
      Data: [],
      tableData: [], // 表格数据
      total: null, // 总条数
      pageSize: 10, // 每页显示条数
      pageCurrent: 1 // 后台获取页
    }
  },
  created() {
    this.searchTable()
  },
  methods: {
    exportTable() {
      if (this.tableData2.length === 0) {
        this.$message.warning('暂无数据')
      } else {
        const htmlNode = $('#BL').html()
        const body = $('body')
        body.children().hide()
        const printNode = $(htmlNode)
        body.append(printNode)
        window.print()
        body.children().not('script').show()
        printNode.remove()
      }
    },
    // 查询表格数据
    searchTable() {
      this.loading = true
      this.tableData = []
      this.Data = []
      if (this.partName !== '' && this.partName !== null) {
        this.partNamecheck = true
      } else {
        this.partNamecheck = false
      }
      searchtable(this.partNamecheck, this.partName, !this.all, 0, this.pageCurrent, this.pageSize).then(response => {
        if (response.data.total === 0) {
          this.loading = false
        } else {
          // this.tableData.push({
          //   重量: response.data.rows[0].重量
          // })
          this.Data = response.data.rows
          this.total = response.data.total
        }
      }).then(() => {
        for (let i = 0; i < this.Data.length; i++) {
          searchtable2(this.Data[i].工艺计划流水号).then(response => {
            this.tableData.push({
              工艺计划流水号: this.Data[i].工艺计划流水号,
              零件名称: this.Data[i].零件名称,
              零件图号: this.Data[i].零件图号,
              备料打印: this.Data[i].备料打印,
              工艺要求: response.data[0].工艺要求,
              材料: this.Data[i].材料,
              重量: this.Data[i].重量,
              批次数量: this.Data[i].批次数量
            })
          })
          this.loading = false
        }
      })
    },
    handleSelectionChange(val) {
      this.tableData2 = []
      this.tableData2 = val
    },
    edit() {
      if (this.tableData2.length === 0) {
        this.$message.warning('暂无数据')
      } else {
        this.a = 0
        for (let i = 0; i < this.tableData2.length; i++) {
          edit(this.tableData2[i].工艺计划流水号).then(response => {
            this.a += parseInt(response.data[0].result)
            if (this.a === this.tableData2.length) {
              this.pageSize = 10
              this.pageCurrent = 1
              this.searchTable()
            }
          })
        }
      }
    },
    edit2() {
      if (this.tableData2.length === 0) {
        this.$message.warning('暂无数据')
      } else {
        this.a = 0
        for (let i = 0; i < this.tableData2.length; i++) {
          edit2(this.tableData2[i].工艺计划流水号).then(response => {
            this.a += parseInt(response.data[0].result)
            if (this.a === this.tableData2.length) {
              this.pageSize = 10
              this.pageCurrent = 1
              this.searchTable()
            }
          })
        }
      }
    },
    downLoad(tableid) {
      if (this.tableData2.length === 0) {
        this.$message.warning('暂无数据')
      } else {
        exportExcelMethod(tableid, '123', 'asd')
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>

</style>
