<template>
  <div class="app-container">
    <el-card>
      <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="searchListinfor()">查询</el-button>
      <el-table
        v-loading="listLoading"
        :data="tableData1"
        border
        size="mini"
        style="width: 100%;"
        height="300"
        @row-click = "chooseListinfor">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column label="领料单编号" align="center" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.领料单编号 }}
          </template>
        </el-table-column>
        <el-table-column label="领料时间" align="center" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.领料时间 }}
          </template>
        </el-table-column>
        <el-table-column label="领料人" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
      </el-table>
      <el-table
        v-loading="listLoading1"
        :data="tableDataPicking"
        size="mini"
        border
        style="width: 100%;"
        height="400">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column label="项目名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床图号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="组号" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.组号 }}
          </template>
        </el-table-column>
        <el-table-column label="领用数量" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.出库数量 }}
          </template>
        </el-table-column>
        <el-table-column label="仓库" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.仓库名称 }}
          </template>
        </el-table-column>
        <el-table-column label="货位" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.货位名称 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="零件类型" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.类型名称 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="200">
          <template slot-scope="scope">
            <el-button :disabled="scope.row.是否出库" size="mini" type="danger" icon="el-icon-back" style="margin: 0 0 0 0px" @click="ErrorpartOut(scope.row)">异常</el-button>
            <el-button :disabled="scope.row.是否出库" size="mini" type="primary" icon="el-icon-back" style="margin: 0 0 0 0px" @click="partOut(scope.row)">出库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading1"
        :data="tableDataExchange"
        border
        size="mini"
        style="width: 100%;"
        height="200">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column label="交换单编号" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.交换单编号 }}
          </template>
        </el-table-column>
        <el-table-column label="交换件名称" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.交换件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床图号" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="组号" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.组号 }}
          </template>
        </el-table-column>
        <el-table-column label="被交换件名称" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.被交换件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="被交换件项目名称" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.被交换件项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="被交换件机床图号" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.被交换件机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="被交换件组号" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.被交换件组号 }}
          </template>
        </el-table-column>
        <el-table-column label="交换数量" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.交换数量 }}
          </template>
        </el-table-column>
        <el-table-column label="申请人" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.申请时间 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="审批状态" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.审批状态 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="100">
          <template slot-scope="scope">
            <el-button :disabled="scope.row.是否禁用" size="mini" type="primary" icon="el-icon-back" style="margin: 0 0 0 0px" @click="execExchangePartOut(scope.row)">出库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--异常出库对话框-->
    <el-dialog :visible.sync="dialogFormVisible" title="异常出库" center width="380px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left">
        <el-form-item label="异常数量" prop="ContractNumberValue" label-width="100px" size="small">
          <el-input v-model="form.ErrorNumber" size="small" placeholder="异常数量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="异常原因" prop="TotalContractAmount" label-width="100px">
          <el-input v-model="form.Reason" size="small" placeholder="异常原因" style="width: 200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchList, searchListDetail, PurchasePart, BasicPart, WorkShopPart, ListDetailOut, ErrorOut, ExchangeOut, ExchangePartOut } from '@/api/Inventory/PartOut'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      ProjectValue: ' ',
      Project: [],
      ProjectValue_check: 1,
      MachineValue: ' ',
      Machine: [],
      MachineValue_check: 1,
      GroupNumValue: ' ',
      GroupNumValue_check: 1,
      GroupNum: [],
      tableDataBasic: [],
      tableDataPurchase: [],
      tableData1: [],
      tableDataPicking: [], // 领料单明细表格
      listNumber: '',
      pickingListNumber: null, // 领料单编号
      pickingListNumber_check: 1,
      pickingListNumberx: '', // 领料单流水号
      picikingPeople: '', // 领料人流水号
      partProjectNumber: '', // 项目流水号
      partProjectName: '', // 项目名称
      partMachineNumber: '', // 机床流水号
      partMachineName: '', // 机床名称
      partGroupNumber: '', // 分组流水号
      partGroupName: '', // 分组名称
      OutNumber: '', // 出库数量
      outNote: '', // 出库备注
      getpicikingPeople: '', // 领料人
      listLoading: '',
      listLoading1: '',
      rules: { // 表单验证规则
        Normalnumber: [{ required: true, message: '请输入正常数量', trigger: 'blur' }],
        ErrorNumber: [{ required: true, message: '请输入异常数量', trigger: 'blur' }],
        Reason: [{ required: true, message: '请选择领用人', trigger: 'blur' }]
      },
      form: {
        Normalnumber: '',
        ErrorNumber: '',
        Reason: ''
      },
      dialogFormVisible: false,
      projectnumber: '',
      machinenumber: '',
      teamnumber: '',
      tableDataExchange: [], // 交换件列表
      purchaseDeatileNumber: '', // 采购合同明细流水号
      askPurchaseNumber: '', // 请购单明细流水号
      craftnumber: '', // 工艺计划流水号
      partType: '', // 零件类型
      locatenumber: '',
      partName: '' // 名称
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    this.getpeopleid()
  },
  methods: {
    // 获取人员编号
    getpeopleid() {
      this.picikingPeople = this.id
    },
    // 查询领料单
    searchListinfor() {
      if (this.pickingListNumber === null) {
        this.pickingListNumber_check = 0
      }
      this.listLoading = true
      searchList(this.pickingListNumber_check, this.pickingListNumber).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          response.data[i].领料时间 = response.data[i].领料时间.substring(0, response.data[i].领料时间.length - 8)
        }
        this.tableData1 = response.data
        this.listLoading = false
      })
    },
    // 查询交换单
    searchExchange() {
      ExchangeOut(this.pickingListNumberx).then(response => {
        const data = response.data
        for (let i = 0; i < data.length; i++) {
          data[i].是否出库 === 1 ? data[i].是否禁用 = true : data[i].是否禁用 = false
        }
        this.tableDataExchange = data
      })
    },
    // 查询领料单明细
    searchListDetailt() {
      this.listLoading1 = true
      searchListDetail(this.pickingListNumberx).then(response => {
        const data = response.data
        for (let i = 0; i < data.length; i++) {
          data[i].是否出库 === '1' ? data[i].是否禁用 = true : data[i].是否禁用 = false
        }
        this.tableDataPicking = data
        console.log(this.tableDataPicking)
        this.listLoading1 = false
      })
    },
    // 选择领料单
    chooseListinfor(row) {
      this.pickingListNumberx = row.领料单流水号
      this.getpicikingPeople = row.领料人流水号
      console.log(this.pickingListNumberx, 223344)
      this.searchListDetailt()
      this.searchExchange()
    },
    // 出库
    partOut(row) {
      if (row.出库类型 === 3) {
        WorkShopPart(row.请购单明细流水号, this.getpicikingPeople, row.出库数量, row.货位流水号, row.物料与货位对照流水号, row.备注).then(response => {
          if (response.data[0].result === '1') {
            ListDetailOut(row.领料单明细流水号)
            this.$message.success('出库成功')
            this.searchListDetailt()
          } else { this.$message.error('出库失败') }
        })
      } else if (row.出库类型 === 2) {
        PurchasePart(row.采购合同明细流水号, this.getpicikingPeople, row.出库数量, row.货位流水号, row.物料与货位对照流水号, row.备注).then(response => {
          if (response.data[0].result === '1') {
            ListDetailOut(row.领料单明细流水号)
            this.$message.success('出库成功')
            this.searchListDetailt()
          } else { this.$message.error('出库失败') }
        })
      } else {
        BasicPart(row.工艺计划流水号, row.出库数量, row.收件信息ID, this.getpicikingPeople, row.备注).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('出库成功')
            ListDetailOut(row.领料单明细流水号)
            this.searchListDetailt()
          } else { this.$message.error('增加失败') }
        })
      }
    },
    ErrorpartOut(row) {
      console.log(row, 1111)
      this.dialogFormVisible = true
      this.projectnumber = row.项目流水号
      this.machinenumber = row.机床流水号
      this.teamnumber = row.组件流水号
      this.purchaseDeatileNumber = row.采购合同明细流水号
      this.askPurchaseNumber = row.请购单明细流水号
      this.craftnumber = row.工艺计划流水号
      this.locatenumber = row.货位流水号
      this.partType = row.零件类型
      this.partName = row.名称
    },
    callOff(form) {
      this.form = []
      this.dialogFormVisible = false
    },
    execExchangePartOut(row) {
      ExchangePartOut(row.交换单流水号, row.被交换件采购合同明细流水号, row.申请人, row.交换数量, row.被交换货位流水号, row.备注).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('出库成功')
          this.dialogFormVisible = false
          this.form = []
        } else {
          this.$message.error('出库失败')
        }
      })
    },
    submitEdit() {
      ErrorOut(this.projectnumber, this.machinenumber, this.teamnumber, this.partName, this.picikingPeople, this.purchaseDeatileNumber, this.askPurchaseNumber, this.craftnumber, this.locatenumber, this.form.ErrorNumber, this.form.Reason, this.partType).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('添加成功')
          this.dialogFormVisible = false
          this.form = []
        } else {
          this.$message.error('添加失败')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
