<template>
  <div class="app-container">

    <el-card>
      <span>零件号:</span>
      <el-input v-model="partNum" placeholder="零件号" size="small" clearable style="width:200px"/>

      <span>任务下达时间:</span>
      <el-date-picker
        v-model="timeSlot"
        size="small"
        clearable
        style="width:200px"
        type="month"
        value-format="yyyy-MM"
        placeholder="任务下达时间"/>

      <span>合同号:</span>
      <el-input v-model="contractNum" placeholder="合同号" size="small" clearable style="width:200px"/>
      <span>外协厂家:</span>
      <el-select v-model="outHelpValue" size="small" filterable remote clearable placeholder="外协厂家">
        <el-option
          v-for="item in outHelp"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>

      <el-button type="success" size="small" icon="el-icon-search" style="margin-left: 10px;" @click="PageCurrent=1;PageSize=10;searchTable()">查询</el-button>
    </el-card>

    <el-card>

      <el-table v-loading="loading0" :data="tableData" border style="width: 100%;" height="650" stripe>

        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="50"/>
        <el-table-column label="零件号" width="250" align="center">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" width="140" align="center">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="外协厂家" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.外协厂家简称 }}
          </template>
        </el-table-column>
        <el-table-column label="合同号" width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.表单号 }}
          </template>
        </el-table-column>
        <el-table-column label="材料" width="140" align="center">
          <template slot-scope="scope">
            {{ scope.row.材料 }}
          </template>
        </el-table-column>
        <el-table-column label="项目名称" width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="批次数量" width="140" align="center">
          <template slot-scope="scope">
            {{ scope.row.批次数量 }}
          </template>
        </el-table-column>
        <el-table-column label="废品数量" width="140" align="center">
          <template slot-scope="scope">
            {{ scope.row.废品数量 }}
          </template>
        </el-table-column>
        <el-table-column label="不合格处理方式" width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.不合格处理方式 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row, 'form')">扣款</el-button>
          </template>
        </el-table-column>

      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          v-if="!loading0"
          :current-page="PageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="PageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" title="外协价格计算系数维护" center width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" class="demo-ruleForm" label-width="100px" style="width: 90%;margin: auto">

        <el-form-item label="废品数量" prop="wasteQuantity">
          <el-input v-model="form.wasteQuantity" size="small" clearable placeholder="请输入废品数量"/>
        </el-form-item>
        <el-form-item label="处理方式" prop="processingMethod">
          <el-input v-model="form.processingMethod" size="small" clearable placeholder="请输入处理方式"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item >
            <el-button size="small" @click="callOff('form')">取消</el-button>
            <el-button size="small" type="primary" @click="submit('form')">确 定</el-button>
          </el-form-item>
        </el-row>

      </el-form>

    </el-dialog>

  </div>
</template>

<script>
import { initOutHelp, searchTable, editList } from '@/api/Outsourcing/UnqualifiedTreatment'
export default {
  data() {
    return {
      num_bhg: '',
      count2: 0,
      form: {
        wasteQuantity: '', // 废品数量
        processingMethod: '' // 处理方式
      },
      dialogFormVisible1: false,
      timeSlot: '',
      years: '',
      months: '',
      loading0: false,
      contractNum: '',
      outHelpValue: '',
      outHelp: [],
      partNum: '',
      tableData: [],
      PageCurrent: 1,
      PageSize: 10,
      total: null,
      rules: { // 表单验证规则
        wasteQuantity: [{ required: true, message: '请输入废品数量', trigger: 'blur' }],
        processingMethod: [{ required: true, message: '请输入处理方式', trigger: 'blur' }]
      },
      number: ''
    }
  },
  created() {
    this.fetchData()
    this.searchTable()
  },
  methods: {
    fetchData() {
      this.outHelp = []
      initOutHelp().then(response => { // 初始化外协厂家；*这里模糊查询省一个存储过程
        for (let i = 0; i < response.data.length; i++) {
          this.outHelp.push({
            label: response.data[i].外协厂家名称,
            value: response.data[i].外协厂家流水号
          })
        }
      })
    },
    searchTable() {
      this.loading0 = true
      this.tableData = []
      if (this.contractNum === '' || this.contractNum === null) {
        this.check1 = 0
      } else { this.check1 = 1 }
      if (this.outHelpValue === '' || this.outHelpValue === null) {
        this.check2 = 0
      } else { this.check2 = 1 }
      if (this.partNum === '' || this.partNum === null) {
        this.check3 = 0
      } else { this.check3 = 1 }
      if (this.timeSlot === '' || this.timeSlot === null) {
        this.check4 = 0
        this.check5 = 0
      } else {
        this.check4 = 1
        this.check5 = 1
        this.years = this.timeSlot.substring(0, this.timeSlot.indexOf('-'))
        this.months = this.timeSlot.substring(5, 7)
      }
      searchTable(this.PageCurrent, this.PageSize, this.check2, this.outHelpValue, this.check1, this.contractNum, this.check3, this.partNum, this.check4, this.years, this.check5, this.months).then(response => {
        if (response.data.rows === 0) {
          this.loading0 = false
          this.total = response.data.total
        } else {
          this.tableData = response.data.rows
          this.total = response.data.total
          this.loading0 = false
        }
      })
    },
    // 打开编辑
    handleEdit(index, row, formName) {
      this.count2 = this.count2 + 1
      if (this.count2 !== 1) {
        this.$refs[formName].resetFields()
      }
      this.number = row.外协加工任务单明细流水号
      this.form.wasteQuantity = row.废品数量
      this.num_bhg = row.批次数量
      this.form.processingMethod = row.不合格处理方式
      this.dialogFormVisible1 = true
    },
    // 提交编辑
    submitEdit() {
      if (this.form.wasteQuantity > this.num_bhg) {
        this.$message.error('废品数量大于总数量')
      } else {
        editList(this.number, this.form.wasteQuantity, this.form.processingMethod).then(response => {
          if (response.data[0].result === '1') {
            this.dialogFormVisible1 = false
            this.$message({
              type: 'success',
              message: '信息更新成功!'
            })
            this.searchTable()
          } else {
            this.dialogFormVisible1 = false
            this.$message.error('信息更新失败')
          }
        })
      }
    },
    // 提交添加或者修改
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitEdit()
        } else {
          return false
        }
      })
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible1 = false
      this.$refs[formName].resetFields()
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  .doing-row4{
    background: #f0f9eb;
  }
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:130px
  }
  span{
    margin: 10px 0 10px 10px;
  }
</style>
<style>
  .mark{
    position:fixed;
    left:0;
    top:0;
    opacity:.5;
    width:100%;
    height:100%;
    background:#000;
    z-index:998;
  }
  .content{
    position:absolute;
    z-index:999;
  }
</style>
