<template>
  <div class="app-container">
    <el-card>
      <span>项目名称:</span>
      <el-select v-model="ProjectValue" filterable clearable size="small" placeholder="请选择项目名称" style="width: 200px;margin-right:10px" @change="searchMachine()">
        <el-option
          v-for="item in Project"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>机床图号:</span>
      <el-select v-model="MachineValue" filterable clearable size="small" placeholder="机床图号" style="width: 200px;margin-right:10px" @change="searchGroupList()">
        <el-option
          v-for="item in Machine"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>组号:</span>
      <el-select v-model="GroupNumValue" filterable clearable size="small" placeholder="组号" style="width: 200px">
        <el-option
          v-for="item in GroupNum"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button size="small" type="success" icon="el-icon-search" style="margin: 0 0 0 10px" @click="total = 0;pageSize = 10;pageList = 1;searchPartinfo()">查询</el-button><br>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading"
        :data="tableDataPurchase"
        border
        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="120">
          <template slot-scope="scope" align="center">
            <el-button size="small" type="warning" icon="el-icon-sort-up" style="margin: 0 0 0 0px" @click="selecting(scope.row)">交换</el-button>
            <el-button size="small" type="primary" icon="el-icon-sort-down" style="margin: 0 0 0 0px" @click="selected(scope.row)">被交换</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="PageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-card>
      <el-row>
        <el-col :span="11">
          <div>
            <el-table
              :data="tableData1"
              border
              style="width: 100%;"
              height="100"
            >
              <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>
          </div>
        </el-col>
        <el-col :span="2">
          <div style="margin: 10px 15px">
            <el-button size="small" type="primary" icon="el-icon-edit" @click="total = 0;pageSize = 10;pageList = 1;exchange('form')" >交换</el-button>
          </div>
        </el-col>
        <el-col :span="11">
          <div>
            <el-table
              :data="tableData2"
              border
              style="width: 100%;"
              height="100">
              <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>
          </div>
        </el-col>
      </el-row>
      <!--新增交换件对话框-->
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading1"
        :data="tableDataExchange"
        border
        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="180">
          <template slot-scope="scope" align="center">
            <el-button :disabled="scope.row.是否禁用" size="small" type="primary" icon="el-icon-edit" style="margin: 0 0 0 0px" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button :disabled="scope.row.是否禁用" size="small" type="danger" icon="el-icon-delete" style="margin: 0 0 0 0px" @click="dropListinfor(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="PageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          :total="total1"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" center width="380px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left">
        <el-form-item label="交换单编号" prop="ExchangeNumber" label-width="100px" size="small">
          <el-input v-model="form.ExchangeNumber" size="small" placeholder="请输入零件交换单编号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="交换数量" prop="ExchangeNumberx" label-width="100px">
          <el-input v-model="form.ExchangeNumberx" size="small" placeholder="请输入交换数量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="备注" prop="ExchangeNote" label-width="100px">
          <el-input v-model="form.ExchangeNote" 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 v-show="title==='交换'" type="primary" @click="addListDetailinfor('form')">确 定</el-button>
        <el-button v-show="title==='编辑'" type="primary" @click="submitEdit(scope.$index, scope.row, 'form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { initProject, searchmachine, searchTeam, searchPurchasePart, addExchangepart, searchExchangePart, dropExchangeList, editExchangeList } from '@/api/Inventory/ExchangePart'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      count1: 0,
      count2: 0, // 清表单验证计数器
      ProjectValue: '',
      Project: [],
      ProjectValue_check: 0,
      MachineValue: '',
      Machine: [],
      MachineValue_check: 1,
      GroupNumValue: '',
      GroupNumValue_check: 1,
      GroupNum: [],
      tableDataBasic: [],
      tableDataPurchase: [],
      tableDataExchange: [],
      tableData1: [],
      tableData2: [],
      tableDataPicking: [], // 领料单明细表格
      listNumber: '',
      pickingListNumber: null, // 领料单编号
      pickingListNumberx: '', // 领料单流水号
      picikingPeople: '', // 领料人流水号
      partProjectNumber: '', // 项目流水号
      partProjectName: '', // 项目名称
      partMachineNumber: '', // 机床流水号
      partMachineName: '', // 机床名称
      partGroupNumber: '', // 分组流水号
      partGroupName: '', // 分组名称
      OutNumber: '', // 出库数量
      outNote: '', // 出库备注
      dialogFormVisible: false,
      arr: [],
      form: {
        ExchangeNumber: '', // 交换件编号
        ExchangeNumberx: '', // 交换件数量
        ExchangeNote: '' // 交换件备注
      },
      rules: { // 表单验证规则
        ExchangeNumber: [{ required: true, message: '请输入交换件编号', trigger: 'blur' }],
        ExchangeNumberx: [{ required: true, message: '请输入交换件数量', trigger: 'blur' }],
        ExchangeNote: [{ required: true, message: '请输入交换件备注', trigger: 'blur' }]
      },
      PageCurrent: 1,
      PageSize: 10,
      total: null,
      PageCurrent1: 1,
      PageSize1: 10,
      total1: null,
      listLoading: '',
      listLoading1: '',
      exchangeListNumber: '',
      eTeamPartNumber: '',
      title: ''
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    this.fetchData()
    this.getpeopleid()
    this.getExchangeTable()
  },
  methods: {
    fetchData() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'Project') // 项目名称
    },
    // 获取人员编号
    getpeopleid() {
      this.picikingPeople = this.id
    },
    searchMachine() {
      this.MachineValue = ''
      this.Machine = []
      this.GroupNumValue = ''
      this.GroupNum = []
      if (this.ProjectValue !== '') {
        this.ProjectValue_check = 1
        searchmachine(this.ProjectValue_check, this.ProjectValue).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machine.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号
            })
          }
        })
      } else {
        this.ProjectValue_check = 0
      }
    },
    searchGroupList() {
      this.GroupNumValue = ''
      this.GroupNum = []
      searchTeam(this.MachineValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.GroupNum.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    searchPartinfo() {
      this.tableDataPurchase = []
      this.tableDataBasic = []
      if (this.ProjectValue === '') {
        this.ProjectValue_check = 0
      }
      if (this.MachineValue === '') {
        this.MachineValue_check = 0
      }
      if (this.GroupNumValue === '') {
        this.GroupNumValue_check = 0
      }
      this.listLoading = true
      searchPurchasePart(this.PageCurrent, this.PageSize, this.ProjectValue_check, this.ProjectValue, this.MachineValue_check, this.MachineValue, this.GroupNumValue_check, this.GroupNumValue).then(response => {
        this.tableDataPurchase = response.data.rows
        this.total = response.data.total
      })
      this.listLoading = false
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.searchPartinfo()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.searchPartinfo()
    },
    // 查询交换件
    getExchangeTable() {
      this.listLoading1 = true
      searchExchangePart(this.PageCurrent1, this.PageSize1).then(response => {
        const data = response.data.rows
        console.log(data)
        for (let i = 0; i < data.length; i++) {
          data[i].审批通过 === '1' ? data[i].是否禁用 = true : data[i].是否禁用 = false
        }
        this.tableDataExchange = data
        this.total1 = response.data.total
        this.listLoading1 = false
      })
    },
    handleSizeChange1(val) {
      this.PageCurrent1 = 1
      this.PageSize1 = val
      this.getExchangeTable()
    },
    handleCurrentChange1(val) {
      this.PageCurrent1 = val
      this.getExchangeTable()
    },
    // 选择领料单
    chooseListinfor(row) {
      this.pickingListNumberx = row.领料单流水号
      this.searchListDetailt()
    },
    // 增加交换件
    addListDetailinfor(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          addExchangepart(this.form.ExchangeNumber, this.tableData1[0].组件零件流水号, this.tableData1[0].物料流水号, this.tableData1[0].采购合同明细流水号, this.tableData2[0].组件零件流水号, this.tableData1[0].物料名称, this.tableData2[0].物料名称, this.tableData2[0].货位流水号, this.form.ExchangeNumberx, this.picikingPeople, this.form.ExchangeNote).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
            } else { this.$message.error('增加失败') }
          }).then(() => {
            this.dialogFormVisible = false
            this.PageCurrent1 = 1
            this.getExchangeTable()
          })
        }
      })
    },
    // 打开交换表单
    exchange(form) {
      if (this.tableData1.length === 0) {
        this.$message.error('请选择交换件')
      } else if (this.tableData2.length === 0) {
        this.$message.error('请选择交换件')
      } else {
        this.count1 = this.count1 + 1
        if (this.count1 !== 1) {
          this.$refs[form].resetFields()
        }
        this.title = '交换'
        this.form.ExchangeNumber = ''
        this.form.ExchangeNumberx = ''
        this.form.ExchangeNote = ''
        this.dialogFormVisible = true
      }
    },
    // 重置表单
    callOff(formName) {
      this.form.ExchangeNumber = ''
      this.form.ExchangeNumberx = ''
      this.form.ExchangeNote = ''
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 选中零件交换
    selecting(row) {
      const arr = new Array(row)
      this.tableData1 = arr
    },
    // 选中被零件交换
    selected(row) {
      const arr = new Array(row)
      this.tableData2 = arr
    },
    // 删除领料单
    dropListinfor(row) {
      this.exchangeListNumber = row.交换单流水号
      this.eTeamPartNumber = row.交换件组件零件流水号
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        dropExchangeList(this.exchangeListNumber, this.eTeamPartNumber).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.dialogFormVisible = false
            this.PageCurrent1 = 1
            this.getExchangeTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEdit(row) {
      this.exchangeListNumber = row.交换单流水号
      this.form.ExchangeNumber = row.交换单编号
      this.form.ExchangeNumberx = row.交换数量
      this.form.ExchangeNote = row.备注
      this.title = '编辑'
      this.dialogFormVisible = true
    },
    submitEdit() {
      this.editTable(editExchangeList, [this.exchangeListNumber, this.form.ExchangeNumber, this.form.ExchangeNumberx, this.form.ExchangeNote], 'form', function() { this.getExchangeTable(); this.dialogFormVisible = false })
    }
  }
}
</script>

<style scoped>

</style>
