<template>
  <div class="app-container">
    <el-card>
      <div>
        <el-row>
          <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" style="margin:0px 5px;width:150px" 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-select v-model="groupNumberValue" placeholder="组号" style="margin:0px 5px;width:100px" size="small" clearable>
            <el-option
              v-for="item in groupNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <el-input v-model="PrNum" size="small" placeholder="零件号" clearable style="width: 250px; margin:0px 5px;width:150px"/>
          <el-select v-model="personValue" placeholder="工艺员" size="small" style="margin:0px 5px;width:150px" clearable >
            <el-option
              v-for="item in person"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <el-date-picker
            v-model="finishTime"
            size="small"
            type="daterange"
            align="center"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            range-separator="～"
            style="width:250px;margin-left: 5px"
            start-placeholder="开始日期"
            end-placeholder="结束日期"/>
          <el-button type="primary" size="small" style="margin: 0px 5px" icon="el-icon-search" plain @click="pageCurrent=1;pageSize=300;total=0;searchData()">查询</el-button>
          <el-tooltip effect="dark" content="将零件打回，重新编制工艺定额" placement="top">
            <el-button v-show="true" :disabled="jinyong1" type="danger" plain style="margin-left: 0px;margin-top: 0px" icon="el-icon-back" size="small" @click="returnEdit">打回重新编制</el-button>
          </el-tooltip>
          <el-button :disabled="jinyong" type="primary" size="small" plain style="float: right; margin: 0px 20px 0px 0px" @click="tiaozheng()">工艺员调整</el-button>
          <el-select v-model="PersonValue1" style="width:100px;float: right;" placeholder="人员" size="small" filterable clearable>
            <el-option
              v-for="item in Person1"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-row>
      </div>
    </el-card>
    <el-row>
      <el-col style="width: 700px">
        <el-card>
          <el-table v-loading="loading" :data="tableData" size="mini" height="740" style="width: 750px" border stripe highlight-current-row @selection-change="handleSelectionChange" @row-click="click">
            <el-table-column type="selection" align="center" width="50"/>
            <el-table-column label="进程" prop="进程" align="center" width="95px">
              <template slot-scope="scope">
                {{ scope.row.进程 }}
              </template>
            </el-table-column>
            <el-table-column label="机床号" prop="机床图号" align="center" width="95px">
              <template slot-scope="scope">
                {{ scope.row.机床图号 }}
              </template>
            </el-table-column>
            <el-table-column label="组号" prop="组号" align="center" width="50px">
              <template slot-scope="scope">
                {{ scope.row.组号 }}
              </template>
            </el-table-column>
            <el-table-column label="零件图号" prop="零件图号" show-overflow-tooltip align="center" width="175px">
              <template slot-scope="scope">
                {{ scope.row.零件图号 }}
              </template>
            </el-table-column>
            <el-table-column label="零件名称" prop="零件名称" show-overflow-tooltip align="center" width="130px">
              <template slot-scope="scope">
                {{ scope.row.零件名称 }}
              </template>
            </el-table-column>
            <el-table-column label="数量" align="center" width="52px">
              <template slot-scope="scope">
                {{ scope.row.批次数量 }}
              </template>
            </el-table-column>
            <el-table-column label="工艺员" prop="工艺员" align="center" width="90px">
              <template slot-scope="scope">
                {{ scope.row.工艺员 }}
              </template>
            </el-table-column>
            <el-table-column label="编制时间" prop="工艺完成" align="center" width="85px">
              <template slot-scope="scope">
                {{ scope.row.工艺完成 }}
              </template>
            </el-table-column>
            <el-table-column label="分配时间" prop="分配时间" align="center" width="85px">
              <template slot-scope="scope">
                {{ scope.row.工艺分配时间 ? scope.row.工艺分配时间.split( ' ')[0] : '' }}
              </template>
            </el-table-column>
            <el-table-column label="下达时间" prop="下达时间" align="center" width="85px">
              <template slot-scope="scope">
                {{ scope.row.操作时间 .split( ' ')[0] }}
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              :current-page="pageCurrent"
              :page-sizes="[5, 10, 20, 300]"
              :page-size="pageSize"
              :pager-count="5"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </el-card>
      </el-col>
      <el-col style="width: 680px">
        <el-card v-if="tableData2.length">
          <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">2{{ 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].机加工开始时间.split(' ')[0] : '' }}</td>
              <td class="tg-c3ow">计划结束</td>
              <td class="tg-c3ow" colspan="2">{{ tableData2[0] ? tableData2[0].机加工结束时间.split(' ')[0] : '' }}</td>
            </tr>
          </table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

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

export default {
  data() {
    return {
      finishTime: '',
      partNumValue: [],
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNumber: [],
      personValue: '',
      person: [],
      pageCurrent: 1,
      pageSize: 300,
      total: 0,
      tableData: [],
      loading: false,
      PrNum: '',
      tableData2: [],
      tableData3: [],
      dialogFormVisiblePeople: false,
      title: '',
      机床号: '',
      组号: '',
      零件图号: '',
      零件名称: '',
      投产数量: '',
      材料: '',
      原材料规格: '',
      工艺员: '',
      定额员: '',
      日期: '',
      PersonValue1: '',
      Person1: [],
      jinyong: false,
      jinyong1: false
    }
  },
  created() {
    this.fetchData()
    // this.searchData()
  },
  methods: {
    tiaozheng() {
      if (this.partNumValue.length !== 0) {
        if (this.PersonValue1) {
          tiaozheng(this.PersonValue1, this.partNumValue).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('调整成功')
              this.searchData()
            } else {
              this.$message.warning('调整失败')
            }
          })
        } else {
          this.$message.warning('请选择人员')
        }
      } else {
        this.$message.warning('请选择物料')
      }
    },
    returnEdit() { // 在制零件工艺修改
      if (this.partNumValue.length === 0) {
        this.$message.error('请选择零件')
      } else {
        this.$confirm(`此操作会将零件打回至未编制状态, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          returnEdit(this.partNumValue).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('打回成功')
              this.partNumValue = []
              this.searchData()
              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].人员编号
          })
          this.Person1.push({
            value: response.data[i].人员编号,
            label: 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, check5
      this.machineNumberValue ? check1 = 1 : check1 = 0
      this.groupNumberValue ? check2 = 1 : check2 = 0
      this.PrNum ? check3 = 1 : check3 = 0
      this.personValue ? check4 = 1 : check4 = 0
      this.finishTime ? check5 = 1 : (check5 = 0, this.finishTime = '')
      search(this.pageCurrent, this.pageSize, check1, this.machineNumberValue, check2, this.groupNumberValue, check3, this.PrNum, check4, this.personValue, check5, this.finishTime[0], this.finishTime[1]).then(response => {
        this.tableData = response.data.result
        this.tableData.map(v => {
          this.$set(v)
          if (v.工艺员 === '超级管理员') {
            v.工艺员 = ''
          }
          return v
        })
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    click(row) {
      this.零件图号 = row.零件图号
      searchtable1(row.工艺计划流水号).then(response => {
        this.tableData2 = response.data
        this.dialogFormVisiblePeople = true
        this.title = row.零件名称 + '工艺流程单'
      })
    },
    handleSelectionChange(val) {
      this.jinyong = false
      this.jinyong1 = false
      this.partNumValue = []
      for (let i = 0; i < val.length; i++) {
        this.partNumValue.push(val[i].工艺计划流水号)
        if (val[i].进程 !== '编制中') {
          this.jinyong = true
        }
        if (val[i].进程 === '编制中' || val[i].进程 === '未分配') {
          this.jinyong1 = true
        }
      }
    },
    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>
</style>
