<template>
  <div class="app-container">
    <el-card style="margin-bottom: 20px">
      <span>项目名称:</span>
      <el-select v-model="projectValue" filterable clearable size="small" placeholder="项目名称" @change="searchMachine">
        <el-option
          v-for="item in project"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <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"/>
      </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('cjn')">导出</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>
      <table id="cjn" cellspacing="0px" border align="center" width="100%" style="">
        <thead id="20">
          <tr>
            <td colspan="1" style="text-align: center;font-size: 30px;font-weight: bold;width: 10.25%;">GAOJING</td>
            <td colspan="15" style="text-align:center;font-size: 30px;font-weight: bold">专机装配工艺过程卡</td>
          </tr>
          <tr class="tbodyHead">
            <td colspan="1" style="font-weight: bold" width="6.25%" align="center">机床编号:</td>
            <td colspan="4" width="15%" align="center">{{ MachineNum }}</td>
            <td colspan="1" style="font-weight: bold" width="6.25%" align="center">机床名称:</td>
            <td colspan="5" width="35%" align="center">{{ MachineName }}</td>
            <td colspan="1" style="font-weight: bold" width="6.25%" align="center">客户名称:</td>
            <td colspan="4" width="31.25%" align="center">{{ CustomerName }}</td>
          </tr>
          <tr id="tableHead">
            <th class="tg-0pky">序号</th>
            <th class="tg-0lax" colspan="4">工序名称</th>
            <th class="tg-0lax" colspan="6">工艺内容及装配具体要求</th>
            <th class="tg-0lax">质检</th>
            <th class="tg-0lax">工时/时</th>
            <th class="tg-0lax">完成情况/装配工</th>
            <th class="tg-0lax">检验情况/检验员</th>
            <th class="tg-0lax" width="100px">备注</th>
          </tr>
        </thead>
        <tbody v-for="(item, index) in dataAll" :key="index">
          <tr>
            <td colspan="16" style="font-weight: bold">{{ item.table1 }}</td>
          </tr>
          <tr v-for="(list, index) in item.table2" :key="index">
            <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"/>
          </tr>
        </tbody>
      </table>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchMachine, searchDetail, searchProcess, searchTable } from '@/api/Assembly/PrintingAssemblyProcessCard'
import { getExplorer, method5, Cleanup } from '@/vendor/exportExcel'

export default {
  data() {
    return {
      dataAll: [],
      tableData: [],
      radio: 0,
      check: 0,
      check1: 0,
      loading: false,
      projectValue: '',
      project: [],
      machineNumberValue: '',
      machineNumber: [],
      MachineNum: '',
      MachineName: '',
      CustomerName: '',
      pageCurrent: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'project')
    },
    searchMachine() { // 根据条件查机床编号
      this.machineNumberValue = ''
      this.machineNumber = []
      searchMachine(this.projectValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    searchTable() { // 主表查询
      this.loading = true
      this.projectValue ? this.check = 1 : this.check = 0
      this.machineNumberValue ? this.check1 = 1 : this.check1 = 0
      searchTable(this.check, this.projectValue, 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 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(tableid) {
      if (this.dataAll.length === 0) {
        this.$message.warning('暂无数据')
      } else {
        getExplorer()
        method5(tableid)
        Cleanup()
      }
    },
    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;
  }
</style>
<style>
  .tg  {border-collapse:collapse;border-spacing:0;}
  .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
  .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
  .tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
  .tg .tg-0lax{text-align:left;vertical-align:top}
</style>
