<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 8px;margin-bottom: 8px">
        <span style="margin: 5px">领料单编号:</span>
        <el-input v-model="pickingListNumber" clearable size="small" style="width:200px" />
        <el-button size="small" type="success" icon="el-icon-search" style="margin: 0 0 0 10px" @click="searchList()">查询</el-button>
        <el-button size="small" type="warning" style="float: right;margin-right: 10px" @click="prepareConfirm()">备料确认</el-button>
        <el-button :disabled="审批未通过===true" size="small" type="primary" style="float: right" @click="inputPassword()">领料确认</el-button>
      </div>
    </el-card>
    <div style="width: 27%;float: left;margin-right: 16px">
      <el-card>
        <el-table :data="pickListTable1" :row-class-name="tableRowClassName" class="shenpi" highlight-current-row border size="mini" width="100%" height="570px" @row-click="clickList">
          <el-table-column prop="领料单编号" label="领料单编号" show-overflow-tooltip align="center" width="100px">
            <template slot-scope="scope">
              {{ scope.row.领料单编号 }}
            </template>
          </el-table-column>
          <el-table-column label="领料人" prop="领料人" show-overflow-tooltip align="center" min-width="60px">
            <template slot-scope="scope">
              {{ scope.row.姓名 }}
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="备注" show-overflow-tooltip align="center" min-width="90px">
            <template slot-scope="scope">
              {{ scope.row.领料单备注 }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="float:left;">
          <el-pagination
            :current-page="pageCurrent3"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize3"
            :total="total3"
            layout="total"
            @size-change="handleSizeChange3"
            @current-change="handleCurrentChange3"/>
        </div>
      </el-card>
    </div>
    <div style="width: 70%;float: left">
      <el-card>
        <el-table :data="pickListTable2" :row-class-name="tableRowClassName1" highlight-current-row border style="width: 100%;" size="mini" height="570" @selection-change="handleSelectionChange">
          <el-table-column type="selection" align="center" width="45"/>
          <el-table-column label="名称" show-overflow-tooltip prop="名称" align="center" width="90">
            <template slot-scope="scope">
              {{ scope.row.名称 }}
            </template>
          </el-table-column>
          <el-table-column label="图号或型号" prop="图号或型号" align="center" min-width="90" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.图号或型号 || '—' }}
            </template>
          </el-table-column>
          <el-table-column label="规格" prop="规格" align="center" min-width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.规格 || '—' }}
            </template>
          </el-table-column>
          <el-table-column label="领用" align="center" min-width="50">
            <template slot-scope="scope">
              {{ scope.row.总数量 }}
            </template>
          </el-table-column>
          <el-table-column label="出库" align="center" min-width="120">
            <template slot-scope="scope">
              <el-input-number :disabled="Number(scope.row.总数量) <= Number(scope.row.出库数量1) || scope.row.货位存量1 === null" :max="scope.row.货位存量1" v-model="scope.row.出库数量" size="mini" style="width: 100%"/>
            </template>
          </el-table-column>
          <el-table-column label="备件" align="center" min-width="50">
            <template slot-scope="scope">
              {{ scope.row.备件数量 }}
            </template>
          </el-table-column>
          <el-table-column label="库存" align="center" min-width="50">
            <template slot-scope="scope">
              {{ scope.row.货位存量1 }}
            </template>
          </el-table-column>
          <el-table-column label="已领" align="center" min-width="50">
            <template slot-scope="scope">
              {{ scope.row.出库数量1 }}
            </template>
          </el-table-column>
          <el-table-column v-if="true" label="备料" align="center" width="60">
            <template slot-scope="scope">
              <el-tag v-if="Number(scope.row.是否备料确认)===1" size="mini" type="success">已备</el-tag>
              <el-tag v-else type="warning" size="mini">未备</el-tag>
            </template>
          </el-table-column>
          <!--          <el-table-column v-if="true" label="领料" align="center" width="60">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-tag v-if="(Number(scope.row.总数量) <= Number(scope.row.出库数量1)) || Number(scope.row.是否领料确认) === 1" type="success" size="mini">已领</el-tag>-->
          <!--              <el-tag v-else-if="Number(scope.row.总数量) > Number(scope.row.出库数量1) && Number(scope.row.出库数量1) > 0" type="primary" size="mini">未完</el-tag>-->
          <!--              <el-tag v-else-if="Number(scope.row.出库数量1) === 0" type="warning" size="mini">未领</el-tag>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="机床图号" prop="机床图号" align="center" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.机床图号 }}
            </template>
          </el-table-column>
          <el-table-column label="组号" align="center" min-width="50">
            <template slot-scope="scope">
              {{ scope.row.组号 }}
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible1" title="物料出库" center width="380px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left">
        <el-form-item label="机床" prop="machineValue" label-width="100px">
          <el-select v-model="form1.machineValue" :disabled="true" filterable size="small" placeholder="机床" style="width: 200px;margin-right:10px" @change="searchGroup()">
            <el-option
              v-for="item in machine"
              :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">&nbsp;&nbsp;{{ item.name }}</div>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分组" prop="groupNumValue" label-width="100px">
          <el-select v-model="form1.groupNumValue" :disabled="true" filterable size="small" placeholder="分组" style="width: 200px;margin-right:10px">
            <el-option
              v-for="item in group"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="出库数量" prop="outNumber" label-width="100px" size="small">
          <el-input-number v-model="form1.outNumber" :min="0.01" :max="form1.outNumberMax" :precision="2" size="small" placeholder="出库数量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="出库人" prop="personValue" label-width="100px">
          <el-select v-model="form1.personValue" :disabled="true" size="small" style="width: 200px;margin-right:10px">
            <el-option
              v-for="item in person"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="出库类别" prop="outTypeValue" label-width="100px">
          <el-select v-model="form1.outTypeValue" size="small" placeholder="出库类别" style="width: 200px;margin-right:10px">
            <el-option
              v-for="item in outType"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1=false">取消</el-button>
        <el-button type="primary" @click="submitOutStore()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible2" title="异常记录" center width="380px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left">
        <el-form-item label="异常数量" prop="errorNumber" label-width="100px" size="small">
          <el-input v-model="form2.errorNumber" size="small" placeholder="异常数量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="异常原因" prop="reason" label-width="100px">
          <el-input v-model="form2.reason" size="small" placeholder="异常原因" style="width: 200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2=false">取消</el-button>
        <el-button type="primary" @click="submitErrorRecord()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible3" title="领料确认" center width="380px">
      <el-form ref="form3" :model="form3" :rules="rules3" label-position="left">
        <el-form-item label="出库类别" prop="outTypeValue" label-width="100px">
          <el-select v-model="form3.outTypeValue" size="small" placeholder="出库类别" style="width: 200px;margin-right:10px">
            <el-option
              v-for="item in outType"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="领料部门" label-width="100px" size="small">
          <el-input v-model="form3.department" size="small" placeholder="领料部门" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="人员编号" prop="password" label-width="100px" size="small">
          <el-input v-model="form3.password" size="small" placeholder="人员编号" style="width: 200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible3=false">取消</el-button>
        <el-button type="primary" @click="pickConfirm1()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible4" title="归还" center width="380px">
      <el-form ref="form4" :model="form4" :rules="rules4" label-position="left">
        <el-form-item label="仓库" prop="仓库流水号" label-width="100px" size="small">
          <el-select v-model="form4.仓库流水号" placeholder="仓库名称" @change="getLocate">
            <el-option
              v-for="item in inventoryName"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="货位" prop="货位流水号" label-width="100px" size="small">
          <el-select v-model="form4.货位流水号" placeholder="货位名称">
            <el-option
              v-for="item in locateName"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="归还数量" prop="归还数量" label-width="100px" size="small">
          <el-input-number :min="0" :max="最大归还量" v-model="form4.归还数量" size="small" placeholder="归还数量" style="width: 200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible4=false">取消</el-button>
        <el-button type="primary" @click="submitSendBack()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sendBack, initPickPerson, initMachineProject, searchgroup, searchList, searchListDetail, submitOutStore, initOutType } from '@/api/Inventory/MaterialOut'
import { mapGetters } from 'vuex'
import request from '@/utils/request'
export default {
  name: 'MaterialOut',
  data() {
    return {
      multipleSelection1: [],
      multipleSelection: [],
      pickingListNum: '', // 领料单流水号
      pickingPersonNum: '', // 领料人流水号
      pickingListNumber: '',
      pickingListNumberShow: '',
      pickListTable1: [],
      pageCurrent3: 1,
      pageSize3: 10000,
      total3: 0,
      pickListTable2: [],
      StockNumberTable: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogFormVisible4: false,
      outType: [],
      form1: {
        machineValue: '',
        groupNumValue: '',
        outNumber: '',
        outNumberMax: '', // 出库最大值
        personValue: '',
        outTypeValue: '' // 出库类别
      },
      form2: {
        errorNumber: '',
        reason: '',
        物料流水号: '',
        基本件流水号: '',
        货位流水号: ''
      },
      form3: {
        outTypeValue: '',
        department: '',
        password: ''
      },
      form4: {
        仓库流水号: '',
        货位流水号: '',
        归还数量: ''
      },
      rules1: {
        machineValue: [{ required: true, message: '请选择' }],
        groupNumValue: [{ required: true, message: '请选择' }],
        outNumber: [{ required: true, message: '请输入' }],
        personValue: [{ required: true, message: '请输入' }],
        outTypeValue: [{ required: true, message: '请选择' }] // 出库类别
      },
      rules2: {
        errorNumber: [{ required: true, message: '请输入' }],
        reason: [{ required: true, message: '请输入' }]
      },
      rules3: {
        password: [{ required: true, message: '请输入' }],
        outTypeValue: [{ required: true, message: '请选择' }]
      },
      rules4: {
        仓库流水号: [{ required: true, message: '请输入' }],
        货位流水号: [{ required: true, message: '请输入' }],
        归还数量: [{ required: true, message: '请输入' }]
      },
      machine: [],
      group: [],
      person: [],
      SELECTION: [],
      materialNum: [], // 中间变量物料流水号
      machineValue: [], // 中间变量机床流水号
      groupNumValue: [], // 中间变量组件流水号
      basePartNum: [], // 中间变量基本件流水号
      purchaseBillNum: [], // 中间变量请购单明细流水号
      parchasePartNum: [], // 中间变量标准件和外购件流水号
      inventoryNum: '', // 中间变量仓库流水号
      locationNum: '', // 中间变量货位流水号
      outNumber: [], // 中间变量出库数量
      offlineNum: [],
      inventoryName: [],
      locateName: [],
      物料流水号: '',
      基本件流水号: '',
      标准件和外购件流水号: '',
      领料单流水号: '',
      请购单明细流水号: '',
      最大归还量: '',
      审批未通过: true,
      占用数量: 0
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  mounted() {
    this.fetchData()
    this.searchList()
    this.getHouse()
  },
  methods: {
    // 获取仓库
    async getHouse() {
      const param = {
        url: '',
        method: 'post',
        data: {
          type: '1',
          name: '库存管理_货位主文件_仓库查询'
        }
      }
      request(param).then(res => {
        for (let i = 0; i < res.data.length; i++) {
          this.inventoryName.push({
            label: res.data[i].仓库名称,
            value: res.data[i].仓库流水号
          })
        }
      })
    },
    // 获取货位
    async getLocate() {
      this.locateName = []
      this.form4.货位流水号 = ''
      const param = {
        url: '',
        method: 'post',
        data: {
          type: '1',
          name: '库存管理_货位主文件_查询数据',
          param: `仓库流水号=${this.form4.仓库流水号}`
        }
      }
      request(param).then(res => {
        for (let i = 0; i < res.data.length; i++) {
          this.locateName.push({
            label: res.data[i].货位名称,
            value: res.data[i].货位流水号
          })
        }
      })
    },
    // 归还
    sendBack(row) {
      this.dialogFormVisible4 = true
      this.物料流水号 = row.物料流水号
      this.基本件流水号 = row.基本件流水号
      this.标准件和外购件流水号 = row.标准件和外购件流水号
      this.请购单明细流水号 = row.请购单明细流水号
      this.领料单流水号 = row.领料单流水号
      this.最大归还量 = row.出库数量
    },
    // 提交归还
    submitSendBack() {
      sendBack(this.form4.货位流水号, this.物料流水号, this.form4.归还数量, this.id, this.基本件流水号, this.标准件和外购件流水号, this.请购单明细流水号, this.form4.仓库流水号, this.领料单流水号).then(res => {
        console.log(res)
        if (res.data[0].result === '1') {
          this.$message.success('操作成功')
          this.dialogFormVisible4 = false
          this.clickList()
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 备料确认
    prepareConfirm() {
      this.$confirm('是否备料确认?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          url: '',
          method: 'post',
          data: {
            type: '4',
            name: `update 车间装配管理_领料单明细 set 是否备料确认 = 1 where 领料单明细流水号 in (${this.multipleSelection})`
          }
        }
        request(param).then(res => {
          if (res.data[0].result === '1') {
            this.$message.success('操作成功')
            this.clickList()
          } else {
            this.$message.error('操作失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 领料确认
    inputPassword() {
      this.form3.outTypeValue = ''
      this.form3.password = ''
      this.form3.department = ''
      this.dialogFormVisible3 = true
    },
    selectable(row, index) { // 控制某行是否可选
      console.log(row)
      return ((Number(row.总数量) <= Number(row.出库数量1))) === false
    },
    // 初始化数据
    fetchData() {
      this.person = []
      initPickPerson().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.person.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
      this.machine = []
      initMachineProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machine.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
      initOutType().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.outType.push({
            label: response.data[i].出库类别名称,
            value: response.data[i].出库类别流水号
          })
        }
      })
    },
    // 查询组号
    searchGroup() {
      this.group = []
      this.form1.groupNumValue = ''
      searchgroup(this.form1.machineValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.group.push({
            label: response.data[i].组号,
            value: Number(response.data[i].组件流水号)
          })
        }
      })
    },
    // 查询领料单列表
    searchList() {
      let check
      this.pickingListNumber ? check = 1 : check = 0
      searchList(this.pageCurrent3, this.pageSize3, check, this.pickingListNumber).then(response => {
        this.pickListTable1 = response.data.rows
        this.total3 = response.data.total
      })
    },
    handleSizeChange3(val) {
      this.pageSize3 = val
      this.pageCurrent3 = 1
      this.searchList()
    },
    handleCurrentChange3(val) {
      this.pageCurrent3 = val
      this.searchList()
    },
    // 查看领料单明细
    clickList(row) {
      row && Number(row.是否审批) ? this.审批未通过 = false : this.审批未通过 = true
      row && row.领料单流水号 ? this.pickingListNum = row.领料单流水号 : this.pickingListNum
      row && row.人员编号 ? this.pickingPersonNum = row.人员编号 : this.pickingListNum
      row && row.领料单编号 ? this.pickingListNumberShow = row.领料单编号 : this.pickingListNumberShow
      row && row.姓名 ? this.form3.personName = row.姓名 : this.form3.personName
      searchListDetail(this.pickingListNum).then(res => {
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].离线采购件流水号 !== 0) {
            res.data[i].货位存量1 = res.data[i].货位存量
          }
          if (res.data[i].请购单明细流水号 !== 0 && res.data[i].请购单明细流水号 !== '' && res.data[i].请购单明细流水号 !== null) {
            res.data[i].货位存量1 = res.data[i].货位存量
          }
        }
        this.pickListTable2 = res.data
        console.log(this.pickListTable2, 111)
      })
    },
    tableRowClassName({ row }) {
      if (parseInt(row.是否审批) === 0) {
        return 'shenpi-row'
      } else {
        return ''
      }
    },
    tableRowClassName1({ row }) {
      if (parseInt(row.出库数量1) === 0) {
        return 'weilingliao-row'
      } else if (parseInt(row.总数量) > parseInt(row.出库数量1) && parseInt(row.出库数量1)) {
        return 'weilingwan-row'
      } else {
        return ''
      }
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection1 = []
      val.map(v => {
        this.multipleSelection1.push(v.领料单明细流水号)
      })
    },
    // 提交领料确认
    pickConfirm1() {
      for (let i = 0; i < this.pickListTable2.length; i++) {
        if (this.pickListTable2[i].出库数量 !== 0) {
          this.multipleSelection.push(this.pickListTable2[i].领料单明细流水号)
          this.basePartNum.push(this.pickListTable2[i].基本件流水号)
          this.materialNum.push(this.pickListTable2[i].物料流水号)
          this.parchasePartNum.push(this.pickListTable2[i].标准件和外购件流水号)
          this.purchaseBillNum.push(this.pickListTable2[i].请购单明细流水号)
          this.groupNumValue.push(this.pickListTable2[i].组件流水号)
          this.machineValue.push(this.pickListTable2[i].机床流水号)
          this.offlineNum.push(this.pickListTable2[i].离线采购件流水号)
          this.outNumber.push(this.pickListTable2[i].出库数量)
        }
      }
      submitOutStore(this.multipleSelection, this.basePartNum, this.materialNum, this.parchasePartNum, this.purchaseBillNum, this.groupNumValue, this.machineValue, this.outNumber, this.id, this.form3.password, this.form3.outTypeValue, this.pickingListNum, this.offlineNum, this.form3.department).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('出库成功')
          this.dialogFormVisible3 = false
          searchListDetail(this.pickingListNum).then(res => {
            for (let i = 0; i < res.data.length; i++) {
              if (res.data[i].离线采购件流水号 !== 0) {
                res.data[i].货位存量1 = res.data[i].货位存量
              }
              if (res.data[i].请购单明细流水号 !== 0 && res.data[i].请购单明细流水号 !== '' && res.data[i].请购单明细流水号 !== null) {
                res.data[i].货位存量1 = res.data[i].货位存量
              }
            }
            this.pickListTable2 = res.data
          })
        } else { this.$message.error('操作失败') }
      })
    },
    // 提交出库
    submitOutStore() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          // 出库数量，出库人，基本件流水号/标准件和外购件流水号 => 记录到一个新表
          // 物料流水号，货位流水号，出库数量 => 减掉货位物料数
          submitOutStore(this.materialNum, this.locationNum, this.form1.outNumber, this.form1.personValue, this.basePartNum, this.parchasePartNum, this.purchaseBillNum, this.pickingListNum, this.pickingPersonNum, this.inventoryNum, this.form1.outTypeValue).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('出库成功')
              this.dialogFormVisible1 = false
              this.clickList()
            } else { this.$message.error('操作失败') }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .el-select{
    width:200px
  }
  .el-input{
    width:120px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .el-tag{
    margin: 0
  }
</style>
<style>
  .el-table .shenpi-row {
    background: #9BD7FC !important;
  }
  .el-table .weilingliao-row {
    background: #D8DDE6 !important;
  }
  .el-table .weilingwan-row {
    background: #E9F0F9 !important;
  }
</style>
