<!--零件报废补投-->
<template>
  <div class="app-container">

    <el-card>
      <div>
        <span>项目名称:</span>
        <el-select v-model="ProjectNameValue" placeholder="请选择项目名称" style="width:200px" clearable size="small" @change="fetchMachineDrawingData" @clear="MachineDrawingValue = '';MachineDrawing = [];Team = [];TeamValue = ''">
          <el-option
            v-for="item in ProjectName"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span style="margin-left: 15px">机床号:</span>
        <el-select v-model="MachineDrawingValue" placeholder="请选择机床图号" style="width:200px" clearable size="small" @change="fetchTeamDrawingData" @clear="Team = [];TeamValue = ''">
          <el-option
            v-for="item in MachineDrawing"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span style="margin-left: 15px">组号:</span>
        <el-select v-model="TeamValue" placeholder="请选择组号" style="width:200px" clearable size="small">
          <el-option
            v-for="item in Team"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span style="margin-left: 15px">零件图号:</span>
        <el-input v-model="PartDrawing" placeholder="请输入零件图号" size="small" style="width: 200px" clearable/>
        <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 20px" @click="currentPage1=1; fetchTableData()">查询</el-button>
      </div>
    </el-card>

    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(226,223,223,1)"
        height="250"
        highlight-current-row
        border
        stripe
        style="width: 100%; text-align: center"
        @current-change="fetchTableData2">
        <el-table-column
          align="center"
          prop="province1"
          label="零件号"
        >
          <template slot-scope="scope">{{ scope.row.零件图号 }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="province2"
          label="零件名称"
        >
          <template slot-scope="scope">{{ scope.row.零件名称 }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="province4"
          label="批次数量"
        >
          <template slot-scope="scope">{{ scope.row.批次数量 }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="province5"
          label="开始时间"
        >
          <template slot-scope="scope">{{ scope.row.机加工开始时间 }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="province6"
          label="结束时间"
        >
          <template slot-scope="scope">{{ scope.row.机加工结束时间 }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="province7"
          label="零件状态"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.批次零件状态 === '2'" type="info" size="small">停产</el-tag>
            <el-tag v-else type="success" size="small">生产</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作"
          width="100px"
        >
          <template slot-scope="scope" >
            <el-button :disabled="scope.row.批次零件状态 === '2'" type="primary" size="small" @click="handleEdit(scope.row)">停产</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="currentPage1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>

    <el-card>
      <el-table
        v-loading="loading2"
        :data="tableData2"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(226,223,223,1)"
        height="250"
        border
        stripe
        style="width: 100%">
        <el-table-column
          width="80px"
          prop="province1"
          label="工序顺序"
        >
          <template slot-scope="scope">{{ scope.row.工序顺序 }}</template>
        </el-table-column>
        <el-table-column
          width="200px"
          prop="province2"
          label="工艺名称"
        >
          <template slot-scope="scope">{{ scope.row.工艺名称 }}</template>
        </el-table-column>
        <el-table-column
          prop="province4"
          label="工艺要求"
        >
          <template slot-scope="scope">{{ scope.row.工艺要求 }}</template>
        </el-table-column>
        <el-table-column
          width="80px"
          prop="province5"
          label="单件定额工时"
        >
          <template slot-scope="scope">{{ scope.row.单件定额工时 }}</template>
        </el-table-column>
        <el-table-column
          width="80px"
          prop="province6"
          label="准结定额工时"
        >
          <template slot-scope="scope">{{ scope.row.准结定额工时 }}</template>
        </el-table-column>
        <el-table-column
          width="100px"
          prop="province7"
          label="工艺是否完成"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.工序状态 === '4'"><div style="width: 12px;height: 12px;border-radius: 50%;background-color: rgb(103, 194, 58);display: inline-block;margin:0px 5px 0 0"/>完成</div>
            <div v-else ><div style="width: 12px;height: 12px;border-radius: 50%;background-color: red;display: inline-block;margin:0px 5px 0 0"/>未完成</div>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="currentPage2"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize2"
          :total="total2"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"/>
      </div>
    </el-card>

    <el-card>
      <div>停产后,将重新补投两种零件，一种是接着加工(执行原来的加工计划)，另一种是从第一序重新加工(重新制定零件的加工计划)，补投的零件号后加B</div>
      <div style="margin-top: 20px; display: block">
        <span>补投接着加工零件数量</span>
        <el-input v-model="inputA" auto-complete="off" style="width: 200px" size="small"/>
        <span>开始工序</span>
        <el-input v-model="inputB" :disabled="true" auto-complete="off" style="width: 200px" size="small"/>
        <el-button type="success" size="small" @click="editA">补投接着生产零件</el-button>
      </div>
      <!--<div style="margin-left: 800px; display: block">>>>><el-button type="success" size="small" style="margin-left: 40px" @click="goToCapacityBalance()">制定计划</el-button></div>-->
      <div style="margin-top: 20px; display: block">
        <span>补投重新加工零件数量</span>
        <el-input v-model="inputC" auto-complete="off" style="width: 200px" size="small" />
        <span>开始工序</span>
        <el-input v-model="inputD" :disabled="true" auto-complete="off" style="width: 200px" size="small"/>
        <el-button type="success" size="small" @click="editB">补投重新生产零件</el-button>
      </div>
    </el-card>

    <el-card v-show="false">
      <el-table
        :data="tableData4"
        height="250"
        highlight-current-row
        border
        stripe
        style="width: 100%">
        <el-table-column
          prop="province1"
          label="零件号"
        >
          <template slot-scope="scope">{{ scope.row.零件图号 }}</template>
        </el-table-column>
        <el-table-column
          prop="province2"
          label="零件名称"
        >
          <template slot-scope="scope">{{ scope.row.零件名称 }}</template>
        </el-table-column>
        <el-table-column
          prop="province4"
          label="重投数量"
        >
          <template slot-scope="scope">{{ scope.row.批次数量 }}</template>
        </el-table-column>
        <el-table-column
          prop="province5"
          label="开始工序"
        >
          <template slot-scope="scope">{{ scope.row.开始工序 }}</template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :visible.sync="dialogeditVisible" title="停产" width="400px" center >
      <el-form ref="form" :model="form" label-position="left">
        <el-form-item label="零件号" label-width="100px" style="width: 299px ; display: inline-block; text-align: left" size="small">
          <el-input v-model="form.input1" :disabled="true" auto-complete="off" style="margin-left: 20px"/>
        </el-form-item>
        <el-form-item label="停产原因" label-width="100px" prop="reason" style="width: 299px ; display: inline-block; text-align: left" size="small">
          <el-input v-model="form2.reason" auto-complete="off" style="margin-left: 20px"/>
        </el-form-item>
        <el-form-item label="停产时间" label-width="120px" prop="time" style="width: 299px ; display: inline-block; text-align: left" size="small">
          <el-date-picker
            v-model="form2.time"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            placeholder="选择日期"/>
        </el-form-item>
        <el-form-item v-show="false" label="零件状态" label-width="123px" style="margin-bottom: 18px; display: inline-block" prop="input2">
          <el-radio-group v-model="radio" fill="#B2DFEE" boolean @change="remark()">
            <el-radio-button label="正常生产"/>
            <el-radio-button label="停止生产"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="false" label="开始时间" label-width="120px" style="margin-bottom: 18px; display: inline-block">
          <el-date-picker
            v-model="input3"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            placeholder="选择日期"/>
        </el-form-item>
        <el-form-item v-show="false" label="结束时间" label-width="120px" style="margin-bottom: 18px; display: inline-block">
          <el-date-picker
            v-model="input4"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <div style="margin-right: 60px;margin-top: 10px">
        <el-button style="margin-left: 35%" @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogeditVisible1" title="停产记录" width="400px" center>
      <el-form ref="form2" :model="form2" label-position="left">
        <el-form-item label="停产原因" label-width="100px" style="width: 299px ; display: inline-block; text-align: left" size="small">
          <el-input v-model="form2.reason" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="停产时间" label-width="100px" style="width: 299px ; display: inline-block; text-align: left" size="small">
          <el-date-picker
            v-model="form2.time"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <div style="margin-left: 140px;margin-top: 10px">
        <el-button type="primary" @click="submitRemark()">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { ProjectNameSelect, MachineDrawingSelect, TeamSelect, TableDataSelect, editTime, editStop, TableDta2Select, editA, editchar } from '@/api/ManufacturingCenter/SparePartsForScrap'
export default {
  data() {
    return {
      remark1: '',
      remark2: '',
      disabledB: false,
      tableData4: [],
      judge: '',
      ProjectNameValue: '', // 项目名称
      ProjectName: [], // 项目名称下拉框
      MachineDrawingValue: '', // 机床号
      MachineDrawing: [], // 机床图号下拉框
      TeamValue: '', // 组号
      Team: [], // 组号下拉框
      currentPage1: 1,
      pageSize1: 10,
      total1: null,
      currentPage2: 1,
      pageSize2: 10,
      total2: null,
      tableData: [], // 主表格数据
      tableData2: [], // 次表格数据
      loading: '', // 主表格加载
      loading2: '', // 次表格加载
      ProjectCode_check: '', // 项目流水号_check
      MachineCode_check: '', // 机床流水号_check
      TeamCode_check: '', // 组件流水号_check
      ComponentPartsBasicCattleTaxNumber_check: 0, // 组件零件基本件流水号_check
      ComponentPartsBasicCattleTaxNumber: '', // 组件零件基本件流水号
      CutCode_check: 0, // 工艺计划流水号_check
      CutCode: '', // 工艺计划流水号
      CutCodeBefore_check: 0, // 工艺计划流水号_补投前_check
      CutCodeBefore: '', // 工艺计划流水号_补投前
      Assess_check: 1, // 考核状态_check
      Assess: 6, // 考核状态
      PartDrawing_check: '', // 零件图号_check
      PartDrawing: '', // 零件图号
      dialogeditVisible: false, // 编辑对话框可见性
      dialogeditVisible1: false, // 编辑对话框可见性
      code: '', // 工艺计划流水号
      temp: '', // 是否停产中间值
      disabled: false,
      form: {
        input1: '',
        input2: ''
      },
      form2: {
        reason: '',
        time: ''
      },
      radio: '',
      input3: '', // 开始时间
      input4: '', // 结束时间
      inputA: 1,
      inputB: '',
      inputC: 1,
      inputD: 1
    }
  },
  created() {
    this.fetchProjectNameData()
    this.fetchTableData()
  },
  methods: {
    // 项目名称查询
    fetchProjectNameData() {
      this.getSelect(ProjectNameSelect, ['项目名称', '项目流水号'], 'ProjectName', this.Assess)
    },
    // 机床图号查询
    fetchMachineDrawingData() {
      this.Team = []
      this.TeamValue = ''
      this.getSelect(MachineDrawingSelect, ['机床图号', '机床流水号'], 'MachineDrawing', this.ProjectNameValue)
    },
    // 组号查询
    fetchTeamDrawingData() {
      this.getSelect(TeamSelect, ['组号', '组件流水号'], 'Team', this.MachineDrawingValue)
    },
    fetchTableData() {
      this.loading = true
      this.ProjectNameValue ? this.ProjectCode_check = 1 : this.ProjectCode_check = 0
      this.MachineDrawingValue ? this.MachineCode_check = 1 : this.MachineCode_check = 0
      this.TeamValue ? this.TeamCode_check = 1 : this.TeamCode_check = 0
      this.PartDrawing ? this.PartDrawing_check = 1 : this.PartDrawing_check = 0
      this.tableData2 = []
      this.getTable(TableDataSelect, [this.ProjectCode_check, this.ProjectNameValue, this.MachineCode_check, this.MachineDrawingValue, this.TeamCode_check, this.TeamValue, this.ComponentPartsBasicCattleTaxNumber_check, this.ComponentPartsBasicCattleTaxNumber, this.CutCode_check, this.CutCode, this.CutCodeBefore_check, this.CutCodeBefore, this.Assess_check, this.Assess, this.PartDrawing_check, this.PartDrawing, this.currentPage1, this.pageSize1], ['tableData', 'total1', 'loading'])
    },
    fetchTableData2(row) {
      if (row) {
        this.disabled = false
        this.judge = row.批次零件状态
        this.CutCodeBefore = row.工艺计划流水号
        TableDta2Select(row.工艺计划流水号, this.currentPage2, this.pageSize2).then(response => {
          this.tableData2 = response.data.rows
          this.total2 = response.data.total
          for (let i = 0; i < response.data.rows.length; i++) {
            if (response.data.rows[i].工序状态 === '4') {
              this.inputB = this.inputB + 1
            }
            if (response.data.rows[i].工序状态 !== '4') {
              this.inputB = response.data.rows[i].工序顺序
              break
            }
          }
        })
      }
    },
    handleSizeChange1(val) {
      this.currentPage1 = 1
      this.pageSize1 = val
      this.fetchTableData()
    },
    handleCurrentChange1(val) {
      this.currentPage1 = val
      this.fetchTableData()
    },
    handleSizeChange2(val) {
      this.pageSize2 = val
      TableDta2Select(this.CutCodeBefore, this.currentPage2, this.pageSize2).then(response => {
        this.tableData2 = response.data.rows
        this.total2 = response.data.total
      })
      this.currentPage2 = 1
      this.pageSize2 = 10
    },
    handleCurrentChange2(val) {
      this.currentPage2 = val
      TableDta2Select(this.CutCodeBefore, this.currentPage2, this.pageSize2).then(response => {
        this.tableData2 = response.data.rows
        this.total2 = response.data.total
      })
      this.currentPage2 = 1
      this.pageSize2 = 10
    },
    handleEdit(row) {
      this.remark1 = ''
      this.remark2 = ''
      this.form2.reason = ''
      this.form2.time = ''
      this.dialogeditVisible = true
      this.form.input1 = row.零件图号
      if (row.批次零件状态 === '2') {
        this.radio = '停止生产'
      } else if (row.批次零件状态 === '1') {
        this.radio = '正常生产'
      }
      this.input3 = row.机加工开始时间
      this.input4 = row.机加工结束时间
      this.code = row.工艺计划流水号
    },
    async chaEdit() {
      this.radio === '正常生产' ? this.temp = '1' : this.temp = '2'
      const Time = await editTime(this.code, this.input3, this.input4)
      const Char = await editchar(this.remark1, this.remark2, this.code)
      const Stop = await editStop(this.code, 2)
      if (Time.data[0].result === '1' && Char.data[0].result === '1' && Stop.data[0].result === '1') {
        this.dialogeditVisible = false
        this.$message.success('信息更新成功')
        this.fetchTableData()
      } else {
        this.$message.error('信息更新失败')
      }
    },
    // 提交添加或者修改
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.chaEdit()
        }
      })
    },
    // 重置表单
    callOff(formName) {
      this.form = {}
      this.dialogeditVisible = false
      this.$refs[formName].resetFields()
    },
    editA() {
      if (this.judge === '2') {
        editA(this.CutCodeBefore, this.inputA, this.inputB).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('补投成功')
            this.fetchTableData()
          } else {
            this.$message.error('补投失败')
          }
        })
      } else { this.$message.warning('请选择停产零件') }
    },
    editB() {
      if (this.judge === '2') {
        editA(this.CutCodeBefore, this.inputC, this.inputD).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('补投成功')
            this.fetchTableData()
          } else {
            this.$message.error('补投失败')
          }
        })
      } else { alert('请选择停产零件') }
    },
    goToCapacityBalance() {
      this.$router.push({ path: '/ProductionCapacityBalance/index' })
    },
    remark() {
      if (this.radio === '停止生产') {
        this.form2.reason = ''
        this.form2.time = ''
        this.dialogeditVisible1 = true
      }
    },
    submitRemark() {
      this.remark1 = this.form2.reason
      this.remark2 = this.form2.time
      this.dialogeditVisible1 = false
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:200px;
  }
</style>
