<!--零件报废补投-->
<template>
  <div class="app-container">
    <el-card style="width: 1360px">
      <el-row>
        <!--<el-select v-model="ProjectNameValue" placeholder="请选择项目名称" style="width:150px;margin-bottom: 3px" 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>-->
        <el-select v-model="MachineDrawingValue" placeholder="请选择机床图号" style="width:150px" 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>
        <el-select v-model="TeamValue" placeholder="请选择组号" style="width:150px;margin-left: 10px" clearable size="small">
          <el-option
            v-for="item in Team"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-input v-model="PartDrawing" placeholder="请输入零件图号" size="small" style="width: 150px;margin-left: 10px" clearable/>
        <el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 10px" plain @click="currentPage1=1; fetchTableData()">查询</el-button>
      </el-row>
    </el-card>
    <el-row>
      <el-col style="width: 850px">
        <el-card style="width: 845px">
          <el-table
            v-loading="loading"
            :data="tableData"
            :row-class-name="tableRowClassName1"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(226,223,223,1)"
            height="520"
            highlight-current-row
            border
            style="width: 850px; text-align: center"
            @current-change="fetchTableData2">
            <el-table-column
              align="center"
              prop="零件图号"
              label="零件号"
              width="173px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.零件图号 }}</template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="零件名称"
              label="零件名称"
              width="129px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.零件名称 }}</template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="province4"
              label="投产数"
              width="75px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.投产数量 }}</template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="province4"
              label="在产数"
              width="75px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.批次数量 }}</template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="province4"
              label="报废数"
              width="75px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.报废数量 }}</template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="province4"
              label="补投数"
              width="75px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.补投数量 }}</template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="province5"
              label="计划开始时间"
              width="110px"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.机加工开始时间 }}</template>
            </el-table-column>
            <!--            <el-table-column-->
            <!--              align="center"-->
            <!--              prop="province7"-->
            <!--              label="状态"-->
            <!--              width="50px"-->
            <!--            >-->
            <!--              <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"
              label="操作"
              width="90px"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-button :disabled="scope.row.批次零件状态 === '2'" type="text" size="mini" icon="el-icon-s-help" @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-col>
      <el-col style="width: 510px">
        <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="520"
            border
            style="width: 510px">
            <el-table-column
              width="50px"
              prop="province1"
              label="顺序"
              align="center"
            >
              <template slot-scope="scope">{{ scope.row.工序顺序 }}</template>
            </el-table-column>
            <el-table-column
              width="87px"
              prop="province2"
              label="工艺名称"
              align="center"
            >
              <template slot-scope="scope">{{ scope.row.工艺名称 }}</template>
            </el-table-column>
            <el-table-column
              prop="province4"
              label="工艺要求"
              align="center"
              width="100px"
            >
              <template slot-scope="scope">{{ scope.row.工艺要求 }}</template>
            </el-table-column>
            <el-table-column
              width="80px"
              prop="province5"
              label="单件定额"
              align="center"
            >
              <template slot-scope="scope">{{ scope.row.单件定额工时 }}</template>
            </el-table-column>
            <el-table-column
              width="80px"
              prop="province6"
              label="准结定额"
              align="center"
            >
              <template slot-scope="scope">{{ scope.row.准结定额工时 }}</template>
            </el-table-column>
            <el-table-column
              width="90px"
              prop="province7"
              label="完成状态"
              align="center"
            >
              <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-col>
    </el-row>
    <el-card style="width: 1360px">
      <div style="margin-top: 10px">报废后,补投的零件号后加B+报废次数</div>
      <!--<div style="margin-top: 10px; display: block">-->
      <!--<span>补投接着加工零件数量</span>-->
      <!--<el-input v-model="inputA" auto-complete="off" style="width: 100px" size="small"/>-->
      <!--<span>开始工序</span>-->
      <!--<el-input v-model="inputB" :disabled="true" auto-complete="off" style="width: 100px" size="small"/>-->
      <!--<el-button type="primary" plain 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: 10px; display: block">
        <span>补投重新加工零件数量</span>
        <el-input-number v-model="inputC" :step="1" :min="0" :max="spareNumber-SupplementationNumber" style="width: 100px" size="small" />
        <span>开始工序</span>
        <el-input v-model="inputD" :disabled="true" auto-complete="off" style="width: 100px" size="small"/>
        <el-button :disabled="spareNumber-SupplementationNumber===0" type="primary" size="small" plain @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 v-el-drag-dialog :visible.sync="dialogeditVisible" :title="title" :close-on-click-modal="false" width="370px" center >
      <el-form ref="form2" :model="form2" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-form-item label="零件图号">
          <el-input v-model="form.input1" :disabled="true" auto-complete="off" size="small" style="width: 160px"/>
        </el-form-item>
        <el-form-item label="报废数量" prop="报废数量">
          <el-input-number v-model="form2.报废数量" :step="1" :min="0" :max="PCNumber" size="small" style="width: 160px"/>
        </el-form-item>
        <el-form-item label="报废原因" prop="报废原因">
          <el-input v-model="form2.报废原因" auto-complete="off" size="small" style="width: 160px"/>
        </el-form-item>
        <el-form-item label="报废时间" prop="报废时间">
          <el-date-picker
            v-model="form2.报废时间"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            style="width: 160px"
            placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form2')">取消</el-button>
        <el-button type="primary" @click="submit('form2')">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog v-el-drag-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.报废原因" 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.报废时间"
            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 { MachineDrawingSelect, TeamSelect, TableDataSelect, editSpare, TableDta2Select, editA } from '@/api/ManufacturingCenter/SparePartsForScrap'
export default {
  data() {
    return {
      remark1: '',
      remark2: '',
      disabledB: false,
      tableData4: [],
      judge: '',
      MachineDrawingValue: '', // 机床号
      MachineDrawing: [], // 机床图号下拉框
      TeamValue: '', // 组号
      Team: [], // 组号下拉框
      title: '',
      PCNumber: '', // 批次数量
      spareNumber: '', // 报废数量
      SupplementationNumber: '', // 补投数量
      currentPage1: 1,
      pageSize1: 40,
      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: {
        报废数量: '',
        报废原因: '',
        报废时间: ''
      },
      rules: {
        报废时间: [{ required: true, message: '请选择报废时间', trigger: 'change' }],
        报废数量: [{ required: true, message: '请输入报废数量', trigger: 'blur' }],
        报废原因: [{ required: true, message: '请输入报废原因', trigger: 'blur' }]
      },
      radio: '',
      input3: '', // 开始时间
      input4: '', // 结束时间
      inputA: 1,
      inputB: '',
      inputC: 1,
      inputD: 1
    }
  },
  created() {
    this.fetchMachineDrawingData()
    this.fetchTableData()
  },
  methods: {
    // 项目名称查询
    // fetchProjectNameData() {
    //   this.getSelect(ProjectNameSelect, ['项目名称', '项目流水号'], 'ProjectName', this.Assess)
    // },
    // 机床图号查询
    fetchMachineDrawingData() {
      this.Team = []
      this.TeamValue = ''
      this.getSelect(MachineDrawingSelect, ['机床图号', '机床流水号'], 'MachineDrawing')
    },
    // 组号查询
    fetchTeamDrawingData() {
      this.getSelect(TeamSelect, ['组号', '组件流水号'], 'Team', this.MachineDrawingValue)
    },
    fetchTableData() {
      this.loading = true
      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.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.spareNumber = row.报废数量
        this.inputC = 0
        this.SupplementationNumber = row.补投数量
        this.inputC = row.报废数量 - 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
            }
          }
        })
      }
    },
    tableRowClassName1({ row }) {
      if (parseInt(row.报废数量) > parseInt(row.补投数量)) {
        return 'baofei'
      }
    },
    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) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.remark1 = ''
      this.remark2 = ''
      this.PCNumber = parseInt(row.批次数量)
      this.title = row.零件名称
      this.dialogeditVisible = true
      this.form.input1 = row.零件图号
      this.code = row.工艺计划流水号
    },
    // 提交添加或者修改
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form2.报废数量 > 0) {
            editSpare(this.code, this.form2.报废数量, this.form2.报废原因, this.form2.报废时间).then(response => {
              if (response.data[0].result === '1') {
                this.$message.success('报废成功')
                this.dialogeditVisible = false
                this.fetchTableData()
              } else {
                this.$message.success('报废失败')
              }
            })
          } else {
            this.$message.warning('报废数量不能为零')
          }
        }
      })
    },
    // 重置表单
    callOff(formName) {
      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.spareNumber - this.SupplementationNumber > 0) {
        if (this.inputC > 0) {
          editA(this.CutCodeBefore, this.inputC).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('补投成功')
              this.fetchTableData()
              this.inputC = 0
            } else {
              this.$message.error('补投失败')
            }
          })
        } else {
          this.$message.warning('补投数量不能为零')
        }
      } 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:0px;
  }
  .el-date-editor{
    width:200px;
  }
  .el-form--label-left >>> .el-form-item__label {
    text-align: right;
  }
  >>>.el-dialog--center .el-dialog__body {
    text-align: initial;
    padding: 10px 20px 0px
  }
  >>>.el-dialog__header {
    padding: 20px 20px 0px;
  }
  >>>.el-dialog__footer {
    padding: 0px 20px 20px;
    /*text-align: right;*/
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  >>>.el-form-item {
    margin-bottom: 13px;
  }
</style>
<style>
  .el-table .baofei{
    background: #FF9797;
  }
</style>
