<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <el-row>
          <el-col style="width: 280px">
            <span>机床号：</span>
            <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="machineChange">
              <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-col>
          <el-col style="width: 270px">
            <span>组号：</span>
            <el-select v-model="groupNumberValue" placeholder="组号" size="small" clearable>
              <el-option
                v-for="item in groupNumber"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-col>
          <el-col style="width: 280px">
            <span>零件号：</span>
            <el-input v-model="PrNum" size="small" placeholder="零件号" clearable style="width: 200px"/>
          </el-col>
          <el-col style="width: 290px">
            <span>工艺员：</span>
            <el-select v-model="personValue" placeholder="工艺员" size="small" clearable style="width: 100px">
              <el-option
                v-for="item in person"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
            <el-button type="success" size="small" style="margin-left: 10px" icon="el-icon-search" @click="pageCurrent=1;pageSize=10;total=0;searchData()">查询</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="max-height: 550px"
        height="550"
        border
        highlight-current-row
        @row-click="click">
        <el-table-column label="零件图号" align="center">
          <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-column label="零件投产数量" align="center">
          <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="[5, 10, 20]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-card v-if="tableData2.length">
      <div slot="header">
        <el-button v-show="true" type="danger" icon="el-icon-edit" size="small" @click="returnEdit">在制零件工艺调整</el-button>
      </div>
      <table class="tg">
        <tr>
          <td class="tg-x61c" colspan="4" rowspan="2">江苏高精</td>
          <td class="tg-c3ow" colspan="2" rowspan="2">零件条码</td>
          <td class="tg-c3ow" colspan="3">零件图号</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].零件图号 : '' }}</td>
          <td class="tg-c3ow">数量</td>
        </tr>
        <tr>
          <td class="tg-c3ow" colspan="3">零件名称</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].零件名称 : '' }}</td>
          <td class="tg-c3ow" rowspan="3">{{ tableData2[0] ? tableData2[0].投产数量 : '' }}</td>
        </tr>
        <tr>
          <td class="tg-c3ow" colspan="2">项目名称</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].项目名称 : '' }}</td>
          <td class="tg-c3ow" colspan="2" rowspan="2">{{ tableData2[0] ? 'P' + tableData2[0].工艺计划流水号 : '' }}</td>
          <td class="tg-c3ow" colspan="3">机床</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].机床图号 : '' }}</td>
        </tr>
        <tr>
          <td class="tg-c3ow">材质</td>
          <td class="tg-c3ow">{{ tableData2[0] ? tableData2[0].材料 : '' }}</td>
          <td class="tg-c3ow">规格</td>
          <td class="tg-c3ow">{{ tableData2[0] ? tableData2[0].原材料规格 : '' }}</td>
          <td class="tg-c3ow" colspan="3">组号</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].组号 : '' }}</td>
        </tr>
        <tr>
          <td class="tg-c3ow">序次</td>
          <td class="tg-c3ow">工艺名称</td>
          <td class="tg-c3ow" colspan="3">工艺要求</td>
          <td class="tg-c3ow">难度系数</td>
          <td class="tg-c3ow">工作者</td>
          <td class="tg-c3ow">单件</td>
          <td class="tg-c3ow">准结</td>
          <td class="tg-c3ow">检查结果</td>
          <td class="tg-c3ow">计划完成日期</td>
          <td class="tg-c3ow">工序条码</td>
        </tr>
        <tr v-for="(value,key) in tableData2" :id="key" :key="key">
          <td class="tg-c3ow">{{ key + 1 }}</td>
          <td class="tg-c3ow">{{ value.工艺名称 }}</td>
          <td class="tg-c3ow" colspan="3">{{ value.工艺要求 }}</td>
          <td class="tg-c3ow">{{ value.工艺难度等级 }}</td>
          <td class="tg-c3ow"/>
          <td class="tg-c3ow">{{ value.单件定额工时 }}</td>
          <td class="tg-c3ow">{{ value.准结定额工时 }}</td>
          <td class="tg-c3ow"/>
          <td class="tg-c3ow">{{ value.计划日期 }}</td>
          <td class="tg-c3ow">{{ value.工序流水号 }}</td>
        </tr>
        <tr>
          <td class="tg-c3ow">工艺员</td>
          <td class="tg-c3ow">{{ tableData2[0] ? tableData2[0].工艺员 : '' }}</td>
          <td class="tg-c3ow">定额员</td>
          <td class="tg-c3ow">{{ tableData2[0] ? tableData2[0].定额员 : '' }}</td>
          <td class="tg-c3ow">工艺完成</td>
          <td class="tg-c3ow">{{ tableData2[0] ? tableData2[0].工艺实际完成时间.split(' ')[0] : '' }}</td>
          <td class="tg-c3ow">加工开始</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].机加工开始时间 : '' }}</td>
          <td class="tg-c3ow">加工结束</td>
          <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].机加工结束时间 : '' }}</td>
        </tr>
      </table>
    </el-card>
  </div>
</template>

<script>
import { initMachineProject, searchgroup, initPerson, search, searchtable1,
  returnEdit } from '@/api/ManufacturingCenter/PartWorkCraftSelect_rdlc'

export default {
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNumber: [],
      personValue: '',
      person: [],
      pageCurrent: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      loading: false,
      PrNum: '',
      tableData2: [],
      tableData3: [],
      机床号: '',
      组号: '',
      零件图号: '',
      零件名称: '',
      投产数量: '',
      材料: '',
      原材料规格: '',
      工艺员: '',
      定额员: '',
      日期: ''
    }
  },
  created() {
    this.fetchData()
    this.searchData()
  },
  methods: {
    returnEdit() { // 在制零件工艺修改
      if (this.partNumValue === '' || this.partNumValue === null) {
        this.$message.error('请选择零件')
      } else {
        this.$confirm(`此操作会将 ${this.零件图号} 打回至未编制状态, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          returnEdit(this.partNumValue).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('打回成功')
              this.partNumValue = ''
              this.tableData = []
              this.tableData2 = []
            } else {
              this.$message.error('打回失败')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
      }
    },
    fetchData() {
      initMachineProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
      initPerson().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.person.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    machineChange() {
      this.groupNumberValue = ''
      this.groupNumber = []
      searchgroup(this.machineNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groupNumber.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    searchData() {
      this.loading = true
      let check1, check2, check3, check4
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      this.PrNum ? check3 = 1 : check3 = 0
      this.personValue ? check4 = 1 : check4 = 0
      search(this.pageCurrent, this.pageSize, check1, this.machineNumberValue, check2, this.groupNumberValue, check3, this.PrNum, check4, this.personValue).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    click(row) {
      this.partNumValue = row.工艺计划流水号
      this.零件图号 = row.零件图号
      searchtable1(row.工艺计划流水号).then(response => {
        this.tableData2 = response.data
      })
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchData()
    }
  }
}
</script>
<style>
  td{
    width: 100px;
    height: 30px;
    text-align:center;
  }
  .tg  {border-collapse:collapse;border-spacing:0;margin: 0 auto}
  .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-c3ow{border-color:inherit;text-align:center;vertical-align:top}
  .tg .tg-x61c{font-weight:bold;font-size:28px;border-color:inherit;text-align:center;vertical-align:top}
</style>
<style scoped>
  span{
    margin: 10px 0 10px 10px;
  }
  .el-card{
    margin-bottom: 15px;
  }
    .el-col{
        margin-bottom: 10px;
    }
</style>
