<template>
  <div class="app-container">
    <el-card>
      <!--<el-button :disabled="isShow" type="info" size="small" style="margin: 0px 180px;float: right" @click="EditData">补录完成日期</el-button>-->
      <el-button :disabled="isDisabled" type="info" size="small" style="margin: 0px 180px;float: right" @click="EditConfirmNum">修改完成数量</el-button>
      <el-select v-model="Machine" filterable clearable size="small" style="margin:0px 10px 0px 10px;width: 185px;" placeholder="机床号" @change="getGroup">
        <el-option
          v-for="item in Machines"
          :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.value2 }}</div>
        </el-option>
      </el-select>
      <el-select v-model="Group" size="small" clearable placeholder="组号" style="margin:0px 10px;width: 100px;" @change="getTableData">
        <el-option
          v-for="item in Groups"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-input v-model="number" placeholder="零件号" clearable size="small" style="width: 170px;margin:0px 10px;"/>
      <el-button type="success" class="el-icon-search" size="small" style="margin: 0px 10px;" @click="pageCurrent = 1;pageSize = 100;getTableData()">查询</el-button>
      <el-button type="warning" class="el-icon-download" size="small" style="margin: 10px 0 0 10px" @click="exportExcel">导出</el-button>
    </el-card>
    <el-row>
      <el-col style="width: 37%">
        <el-card>
          <el-table
            :data="tableData1"
            :row-class-name="tableRowClassName1"
            loading="loading"
            class="table"
            style="max-height: 830px"
            highlight-current-row
            empty-text="   "
            border
            @row-click="getRow">
            <el-table-column type="index" label="序号" width="50"/>
            <el-table-column align="center" label="工艺名" show-overflow-tooltip min-width="70">
              <template slot-scope="scope">
                {{ scope.row.工艺名称 }}
              </template>
            </el-table-column>
            <!--<el-table-column align="center" label="已完数量" show-overflow-tooltip min-width="70">-->
            <!--<template slot-scope="scope">-->
            <!--{{ scope.row.完成数量 }}-->
            <!--</template>-->
            <!--</el-table-column>-->
            <el-table-column align="center" label="质检" width="60">
              <template slot-scope="scope">
                {{ scope.row.序间质检 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="不合格数" width="80">
              <template slot-scope="scope">
                {{ scope.row.不合格数量 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作者" show-overflow-tooltip width="70">
              <template slot-scope="scope">
                {{ scope.row.操作者 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="完成日期" width="95">
              <template slot-scope="scope">
                {{ scope.row.完成日期 }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col style="width: 63%">
        <el-card>
          <el-table
            :data="tableData"
            :row-class-name="tableRowClassName"
            loading="loading"
            class="table"
            highlight-current-row
            border
            height="630"
            @row-click="getCurrentRow">
            <el-table-column type="index" align="center" label="跟单号" width="140">
              <template slot-scope="scope">
                <el-radio :label="scope.row.跟单号" v-model="radio"/>
              </template>
            </el-table-column>
            <el-table-column align="center" label="零件图号" show-overflow-tooltip width="200">
              <template slot-scope="scope">
                {{ scope.row.零件图号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="零件名称" show-overflow-tooltip width="150">
              <template slot-scope="scope">
                {{ scope.row.零件名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="加工数" width="70">
              <template slot-scope="scope">
                {{ scope.row.加工数 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="计划数" width="70">
              <template slot-scope="scope">
                {{ scope.row.投产数量 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="加工完成" width="100">
              <template slot-scope="scope">
                {{ scope.row.完成日期 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="计划完成" width="100">
              <template slot-scope="scope">
                {{ scope.row.计划结束时间 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="用户名称" show-overflow-tooltip min-width="110">
              <template slot-scope="scope">
                {{ scope.row.客户名称 }}
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              :current-page="pageCurrent"
              :page-sizes="[100, 200, 300, 400]"
              :page-size="pageSize"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="90px" class="demo-ruleForm">
        <el-row width="200px">
          <el-form-item label="操作员" prop="人员" >
            <el-input v-model="form.人员" placeholder="操作员" readonly size="small" style="width: 80%" @focus="dialogTableVisible" />
          </el-form-item>
          <el-form-item label="人员编号" prop="人员编号" >
            <el-input v-model="form.人员编号" placeholder="人员编号" disabled size="small" style="width: 80%" />
          </el-form-item>
          <el-form-item label="完成日期" prop="完成时间">
            <el-date-picker
              v-model="form.完成时间"
              style="width: 80%"
              size="small"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="选择日期"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')" >确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible1" title="人员信息" center width="750px">
      <div style="height: 500px">
        <div style="float: left;margin-top: 10px;margin-left: 20px;overflow-y: scroll;height: 400px">
          <el-tree
            :data="data2"
            node-key="id"
            style="width: 150px;float: left"
            height="400"
            accordion
            default-expand-all="true"
            highlight-current="true"
            @node-click="handleNodeClick"/>
        </div>
        <el-table :data="List1" highlight-current-row height="400" style="width: 400px;float: left;margin-left: 30px" @row-click="handleCurrentChange2">
          <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-column label="工作岗位" align="center">
            <template slot-scope="scope">
              {{ scope.row.岗位名称 }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-button style="margin-left: 260px" @click="dialogFormVisible1 = false">取消</el-button>
      <el-button type="primary" style="margin-top: 20px" @click="getName">确认</el-button>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="修改完成数量" center style="min-height: 300px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules" label-position="left" label-width="90px" class="demo-ruleForm">
        <el-row width="200px">
          <el-form-item label="操作者：" prop="操作者" >
            <el-select v-model="form2.操作者" filterable style="width: 200px" placeholder="无" @change="czzChange">
              <el-option
                v-for="item in 操作者"
                :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.value2 }}</div>
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row width="200px">
          <el-form-item label="数量：" prop="数量" >
            <el-input-number :min="0" :max="max" v-model="form2.数量" placeholder="数量" style="width: 200px"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2=false">取消</el-button>
        <el-button type="primary" @click="submit2('form')" >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMachines, getGroups, getTable, gettabledata1, getTree, fn, getTable8, submitEdit, getExport, searchWorkTime, submit2 } from '@/api/ManufacturingCenter/ProcessingStatusNew'
import Cookie from 'js-cookie'

export default {
  data() {
    return {
      Machine: '', // 机床号
      Machines: [],
      Group: '', // 组号
      Groups: [],
      number: '', // 零件图号
      tableData: [], // 工序表
      loading: '',
      checkbox_Machine: '', // 机床
      checkbox_Group: '', // 分组
      checkbox_number: '',
      pageCurrent: 1,
      pageSize: 100,
      total: 0,
      tableData1: [], // 工序表
      radio: '',
      completeTime: '', // 完成时间
      row1: [],
      isShow: true,
      processSerialNumber: '', // 工序流水号
      startTime: '', // 开始时间
      endTime: '',
      dateOfProduction: '', // 排产时间
      title: '',
      dialogFormVisible: false,
      form: {
        人员: '',
        人员编号: '',
        完成时间: ''
      },
      rules: {
        人员: [{ required: true, message: '请选择操作员', trigger: 'change' }],
        人员编号: [{ required: true, message: '人员编号', trigger: 'change' }],
        完成时间: [{ required: true, message: '请选择实际完成时间', trigger: 'change' }]
      },
      dialogFormVisible1: false,
      data2: [],
      List1: [],
      ssks: '',
      Number1: '', // 人员编号
      row2: [],
      khbmbh: '',
      isDisabled: true,
      dialogFormVisible2: false,
      form2: {
        操作者: '',
        数量: ''
      },
      操作者: [],
      max: 0
    }
  },
  created() {
    this.getMachine()
    this.getTable9()
  },
  methods: {
    EditConfirmNum() {
      this.form2.数量 = 0
      this.操作者 = []
      searchWorkTime(this.processSerialNumber).then(response => {
        if (response.data.length !== 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.操作者.push({
              label: response.data[i].姓名,
              value: response.data[i].人员编号,
              value2: '完成数量:' + response.data[i].完成数量,
              value3: response.data[i].批次数量
            })
          }
          if (this.操作者.length !== 0) {
            this.form2.操作者 = parseInt(this.操作者[0].value)
            this.max = parseInt(this.操作者[0].value3) - 1
          }
          this.dialogFormVisible2 = true
        } else {
          this.$message.error('该工序暂无完成信息')
        }
      })
    },
    czzChange() {
      for (let i = 0; i < this.操作者.length; i++) {
        if (this.操作者[i].value === this.form2.操作者) {
          this.form2.数量 = 0
          this.max = parseInt(this.操作者[i].value3)
        }
      }
    },
    submit2() {
      submit2(this.processSerialNumber, this.form2.数量, this.form2.操作者).then(response => {
        this.getCurrentRow2(this.row2)
        this.dialogFormVisible2 = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    exportExcel() {
      if (!this.Machine) {
        this.$message.warning('请选择机床')
        return
      }
      getExport(this.Machine).then(res => {
        console.log(res.data)
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['机床', '组号', '图号及规格', '制品名称', '计划数', '材料', '当前工序名']
          const filterVal = ['机床图号', '组号', '零件图号', '零件名称', '投产数量', '材料', '工艺名称']
          const list = res.data
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '加工进度表',
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      })
    },
    getMachine() {
      if (Cookie.get('machineValue') !== undefined) {
        this.Machine = ''
        this.Machines = []
        this.Group = ''
        this.Groups = []
        getMachines().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号,
              value2: response.data[i].项目名称
            })
          }
          if (Cookie.get('group') !== undefined) {
            this.Machine = parseInt(Cookie.get('machineValue'))
            this.getGroup()
            this.getTableData()
            this.Group = parseInt(Cookie.get('group'))
            // this.getTableData()
            Cookie.remove('machineValue')
            Cookie.remove('group')
          } else {
            // this.getTableData()
            Cookie.remove('machineValue')
            Cookie.remove('group')
          }
        })
      } else {
        this.Machine = ''
        this.Machines = []
        this.Group = ''
        this.Groups = []
        getMachines().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号,
              value2: response.data[i].项目名称
            })
          }
        })
        this.getTableData()
      }
    }, // 获取组号
    getGroup() {
      this.Group = ''
      this.Groups = []
      getGroups(this.Machine).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Groups.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
        this.Group = response.data[0].组件流水号
      })
      this.getTableData()
    }, // 按条件查询
    getTableData() {
      this.tableData = []
      this.tableData1 = []
      this.isDisabled = true
      this.Loading = true
      if (this.Machine === '') {
        this.checkbox_Machine = 0
      } else {
        this.checkbox_Machine = 1
      }
      if (this.Group === '') {
        this.checkbox_Group = 0
      } else {
        this.checkbox_Group = 1
      }
      if (this.number === '') {
        this.checkbox_number = 0
      } else {
        this.checkbox_number = 1
      }
      getTable(this.checkbox_Machine, this.Machine, this.checkbox_Group, this.Group, this.checkbox_number, this.number, this.startTime, this.endTime, this.pageCurrent, this.pageSize).then(response => {
        if (response.data.rows.length !== 0) {
          for (let i = 0; i < response.data.rows.length; i++) {
            this.tableData.push({
              工艺计划流水号: response.data.rows[i].工艺计划流水号,
              考核状态: response.data.rows[i].考核状态,
              序号: response.data.rows[i].序号,
              跟单号: response.data.rows[i].跟单号,
              零件图号: response.data.rows[i].零件图号,
              零件名称: response.data.rows[i].零件名称,
              加工数: response.data.rows[i].完成数量, // MES里采集  现在没有
              投产数量: response.data.rows[i].批次数量,
              完成日期: response.data.rows[i].机加工实际完成时间 ? response.data.rows[i].机加工实际完成时间.split(' ')[0] : '', // 排产时间
              计划结束时间: response.data.rows[i].机加工结束时间 ? response.data.rows[i].机加工结束时间.split(' ')[0] : '',
              客户名称: response.data.rows[i].客户名称 // 客户名
            })
          }
        }
        this.total = response.data.total
        this.loading = false
      })
    },
    // 获取工序表
    getCurrentRow(row) {
      this.isDisabled = true
      this.completeTime = ''
      this.dateOfProduction = ''
      this.processSerialNumber = ''
      this.radio = true
      this.tableData1 = []
      this.row2 = row.工艺计划流水号
      gettabledata1(this.row2).then(response => {
        console.log(response.data)
        if (response.data.length !== 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.tableData1.push({
              工艺名称: response.data[i].工艺名称,
              序间质检: response.data[i].质检结果,
              排产日期: response.data[i].排产时间 ? response.data[i].排产时间.split(' ')[0] : '',
              完成日期: response.data[i].工序完成日期 ? response.data[i].工序完成日期.split(' ')[0] : '',
              操作者: response.data[i].操作者,
              不合格数量: response.data[i].不合格数量,
              质检数量: response.data[i].数量,
              工序流水号: response.data[i].工序流水号,
              工序状态: response.data[i].工序状态,
              完成数量: response.data[i].完成数量
            })
          }
        }
      })
    },
    getCurrentRow2(row) {
      this.isDisabled = true
      this.completeTime = ''
      this.dateOfProduction = ''
      this.processSerialNumber = ''
      this.radio = true
      this.tableData1 = []
      gettabledata1(this.row2).then(response => {
        if (response.data.length !== 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.tableData1.push({
              工艺名称: response.data[i].工艺名称,
              序间质检: response.data[i].质检结果,
              排产日期: response.data[i].排产时间 ? response.data[i].排产时间.split(' ')[0] : '',
              完成日期: response.data[i].工序完成日期 ? response.data[i].工序完成日期.split(' ')[0] : '',
              操作者: response.data[i].操作者,
              不合格数量: response.data[i].不合格数量,
              质检数量: response.data[i].数量,
              工序流水号: response.data[i].工序流水号,
              工序状态: response.data[i].工序状态,
              完成数量: response.data[i].完成数量
            })
          }
        }
      })
    },
    // 补加完成日期
    EditData() {
      this.dialogFormVisible = true
      this.form.人员 = ''
      this.form.人员编号 = ''
      this.form.完成时间 = ''
    },
    getRow(row) {
      this.completeTime = row.完成日期
      this.dateOfProduction = row.排产日期
      this.processSerialNumber = row.工序流水号
      if (this.processSerialNumber) { this.isDisabled = false } else { this.isDisabled = true }
    },
    dialogTableVisible() {
      this.dialogFormVisible1 = true
      this.fetchData3()
    },
    fetchData3() {
      getTree().then(response => { // 获取人员信息树
        const data = response.data
        this.data2 = fn(data, 0)
      })
    },
    handleNodeClick(data) { // 树节点点击
      this.ssks = data.label
      getTable8(data.id).then(response => {
        this.List1 = response.data
      })
    },
    getTable9() {
      this.khbmbh = 12 // 考核部门编号
      getTable8(this.khbmbh).then(response => {
        this.List1 = response.data
      })
    },
    handleCurrentChange2(row) { // 获取当前行人员信息
      this.ssks = row.节点名称
      this.Number1 = row.人员编号
      this.Name = row.姓名
    },
    getName() { // 提交人员
      this.dialogFormVisible1 = false
      this.form.人员 = this.Name
      this.form.人员编号 = this.Number1
    },
    submit() {
      submitEdit(this.Number1, this.form.完成时间, this.processSerialNumber).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('补录成功')
        } else {
          this.$message.error('补录失败')
        }
      })
      this.dialogFormVisible = false
      this.getCurrentRow(this.row2)
    },
    // 完成日期变色
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex
      if (parseInt(row.考核状态) > 6) {
        return 'CompleteColor'
      }
    }, // 工序状态变色
    tableRowClassName1({ row, rowIndex }) {
      row.index = rowIndex
      if (parseInt(row.工序状态) > 3) {
        return 'CompleteColor'
      }
    }, // 重置表单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.getTableData()
    }
  }
}
</script>

<style>
  .CompleteColor{
    background: #9bd7fc !important;
  }
</style>
