<template>
  <div class="app-container">
    <el-card style="margin-bottom: 20px">
      <span>机床编号:</span>
      <el-select v-model="machineNumberValue" filterable clearable placeholder="机床编号" size="small">
        <el-option
          v-for="item in machineNumber"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
        </el-option>
      </el-select>
      <el-button type="success" icon="el-icon-search" style="margin: 10px;" size="small" @click="searchTable">查询</el-button>
      <el-button type="primary" icon="el-icon-download" size="small" @click="exportTable()">导出</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        size="mini"
        highlight-current-row
        min-height="300px"
        height="300px"
        border
        @row-click="searchDetail">
        <el-table-column align="center" label="项目名称">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="机床编号">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="机床名称">
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="客户名称">
          <template slot-scope="scope">
            {{ scope.row.客户名称 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-card>
      <div id="cjn">
        <table cellspacing="0px" border align="center" width="100%" style="">
          <thead id="20">
            <tr class="trHeight2">
              <td colspan="3" style="text-align: center;font-size: 30px;font-weight: bold;width: 10.25%;">GAOJING</td>
              <td colspan="13" style="text-align:center;font-size: 30px;font-weight: bold">专机装配工艺过程卡</td>
              <td colspan="2"><img id="img" alt=""></td>
            </tr>
            <tr class="tbodyHead">
              <td colspan="2" style="font-weight: bold" width="7.25%" align="center">机床编号:</td>
              <td colspan="3" width="16%" align="center">{{ MachineNum }}</td>
              <td colspan="1" style="font-weight: bold" width="8.25%" align="center">机床名称:</td>
              <td colspan="5" width="30%" align="center">{{ MachineName }}</td>
              <td colspan="1" style="font-weight: bold" width="7.25%" align="center">客户名称:</td>
              <td colspan="6" width="31.25%" align="center">{{ CustomerName }}</td>
            </tr>
            <tr id="tableHead" class="trHeight">
              <th class="tg-0pky" width="4%">序号</th>
              <th class="tg-0lax" colspan="4">工序名称</th>
              <th class="tg-0lax" colspan="6">工艺内容及装配具体要求</th>
              <th class="tg-0lax">质检</th>
              <th class="tg-0lax" width="4%">工时/时</th>
              <th class="tg-0lax">完成情况/装配工</th>
              <th class="tg-0lax">检验情况/检验员</th>
              <th class="tg-0lax" width="100px">备注</th>
              <th class="tg-0lax" colspan="2" width="200px">条码</th>
            </tr>
          </thead>
          <tbody v-for="(item, index) in dataAll" :key="index">
            <tr class="trHeight">
              <td colspan="18" style="font-weight: bold">{{ item.table1 }}</td>
            </tr>
            <tr v-for="(list, index) in item.table2" :key="index" class="trHeight1">
              <td align="center"> {{ list.序号 }}</td>
              <td colspan="4">{{ list.工序名称 }}</td>
              <td colspan="6">{{ list.工艺内容及装配具体要求 }}</td>
              <td align="center">{{ list.质检 }}</td>
              <td>{{ list.工时 }}</td>
              <td/>
              <td/>
              <td width="100px"/>
              <td colspan="2" width="200px"><barcode :value="list.装配工序流水号" :options="barcode_option" tag="svg"/></td>
            </tr>
          </tbody>
        </table>
      </div>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchDetail, searchProcess, searchTable } from '@/api/Assembly/PrintingAssemblyProcessCard'
import $ from 'jquery'
import QRCode from 'qrcode'

export default {
  data() {
    return {
      barcode_option: {
        displayValue: false,
        background: '#fff',
        valid: function(valid) {
          console.log(valid)
        },
        width: '2px',
        height: '25px'
      },
      dataAll: [],
      tableData: [],
      radio: 0,
      check: 0,
      check1: 0,
      loading: false,
      machineNumberValue: '',
      machineNumber: [],
      MachineNum: '',
      MachineName: '',
      CustomerName: '',
      pageCurrent: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            value: response.data[i].机床流水号,
            name: response.data[i].项目名称
          })
        }
      })
    },
    searchTable() { // 主表查询
      this.loading = true
      this.machineNumberValue ? this.check1 = 1 : this.check1 = 0
      searchTable(this.check1, this.machineNumberValue, this.pageCurrent, this.pageSize).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    async searchDetail(row) { // 查询工艺
      this.dataAll = []
      this.MachineName = row.机床名称
      this.MachineNum = row.机床图号
      this.CustomerName = row.客户名称
      const opts = {
        errorCorrectionLevel: 'H',
        type: 'image/jpeg',
        rendererOpts: {
          quality: 0.3
        }
      }
      QRCode.toDataURL(row.机床流水号, opts, function(err, url) {
        if (err) throw err
        const img = document.getElementById('img')
        img.src = url
      })
      const response = await searchDetail(row.机床流水号)
      const data = response.data
      for (let i = 0; i < data.length; i++) {
        const response2 = await searchProcess(data[i].组件工艺流水号)
        this.dataAll.push({
          table1: data[i].序号 + '    ' + data[i].组号 + '    ' + data[i].装配任务,
          table2: response2.data
        })
      }
    },
    exportTable() {
      const htmlNode = $('#cjn').html()
      const body = $('body')
      body.children().hide()
      const printNode = $(htmlNode)
      body.append(printNode)
      window.print()
      body.children().not('script').show()
      printNode.remove()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .trHeight{
    height: 65px;
  }
  .trHeight1{
    height: 30px;
  }
  .trHeight2{
    height: 120px;
  }
  .tbodyHead{
    height: 70px;
  }
</style>
<style>
</style>
