<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"
        size = "mini"
        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="mini" type="primary" icon="el-icon-sort-down" style="margin: 0 0 0 0px" @click="selecting(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
              v-loading="listLoading"
              :data="tableData1"
              border
              style="width: 100%;"
              size = "mini"
              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: 0px 25px">
            <el-button size="small" type="warning" icon="el-icon-edit" @click="addSub()" >替换件</el-button>
          </div>
          <div style="margin: 10px 29px">
            <el-button size="small" type="primary" icon="el-icon-edit" @click="exchange()" >替换</el-button>
          </div>
        </el-col>
        <el-col :span="11">
          <div>
            <el-table
              :data="tableData2"
              border
              size = "mini"
              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>
          </div>
        </el-col>
      </el-row>
      <!--新增替换件对话框-->
      <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="ContractNumberValue" 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="TotalContractAmount" label-width="100px">
            <el-input v-model="form.ExchangeNumberx" size="small" placeholder="请输入替换数量" style="width: 200px"/>
          </el-form-item>
          <el-form-item label="备注" prop="TotalContractAmount" 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('form')">确定</el-button>
        </div>
      </el-dialog>
      <!--选择替换件-->
      <el-dialog :visible.sync="dialogFormVisible2" title="被替换件" center width="600px" height="600px">
        <el-form :rules="rules" label-position="left">
          <el-form-item label="物料名称" prop="ContractNumberValue" label-width="150px" size="mini">
            <el-input v-model="materialName" size="small" placeholder="物料名称" style="width: 200px"/>
            <el-button type="primary" size = "mini" @click="getSubstituted()">查询</el-button>
          </el-form-item>
          <el-table
            :data="tableDataSub"
            border
            size = "mini"
            style="width: 100%;"
            height="200px">
            <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="120">
              <template slot-scope="scope" align="center">
                <el-button size="mini" 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>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size = "mini" @click="callOff('form')">取消</el-button>
          <el-button size = "mini" type="primary" @click="chooseSub()">确 定</el-button>
        </div>
      </el-dialog>
    </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="120">
          <template slot-scope="scope" align="center">
            <el-button size="mini" type="primary" icon="el-icon-edit" style="margin: 0 0 0 0px" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" style="margin: 0 0 0 0px" @click="dropListinfor(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchmachine, searchTeam, searchPurchasePart, searchSubstituted, addSubstitutepart, searchSubstitutedCard, deleteSubstitutedCard, editSubstitutedCard } from '@/api/Inventory/SubstitutePart'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      ProjectValue: '',
      Project: [],
      ProjectValue_check: 1,
      MachineValue: '',
      Machine: [],
      MachineValue_check: 1,
      GroupNumValue: '',
      GroupNumValue_check: 1,
      GroupNum: [],
      tableDataPurchase: [],
      tableDataExchange: [],
      tableData1: [],
      tableData2: [],
      listNumber: '',
      PageCurrent: 1,
      PageSize: 10,
      total: null,
      picikingPeople: '', // 领料人流水号
      partProjectNumber: '', // 项目流水号
      partProjectName: '', // 项目名称
      partMachineNumber: '', // 机床流水号
      partMachineName: '', // 机床名称
      partGroupNumber: '', // 分组流水号
      partGroupName: '', // 分组名称
      OutNumber: '', // 出库数量
      outNote: '', // 出库备注
      dialogFormVisible: false,
      dialogFormVisible2: false,
      arr: [],
      tableDataSub: [],
      form: {
        ExchangeNumber: '', // 交换件编号
        ExchangeNumberx: '', // 交换件数量
        ExchangeNote: '' // 交换件备注
      },
      rules: { // 表单验证规则
        ExchangeNumber: [{ required: true, message: '请输入交换件编号', trigger: 'blur' }],
        ExchangeNumberx: [{ required: true, message: '请输入交换件数量', trigger: 'blur' }],
        ExchangeNote: [{ required: true, message: '请输入交换件备注', trigger: 'change' }]
      },
      listLoading: '',
      listLoading1: '',
      substituteListNumber: '',
      title: '',
      materialName: '',
      materialName_check: 0
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    this.fetchData()
    this.getSubstitutedCard()
    this.getpeopleid()
  },
  methods: {
    fetchData() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'Project') // 项目名称
    },
    // 获取人员编号
    getpeopleid() {
      this.picikingPeople = this.id
      console.log(this.picikingPeople)
    },
    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() { // 查询组号
      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 = []
      if (this.ProjectValue === '') {
        this.ProjectValue_check = 0
      } else {
        this.ProjectValue_check = 1
      }
      if (this.MachineValue === '') {
        this.MachineValue_check = 0
      } else {
        this.MachineValue_check = 1
      }
      if (this.GroupNumValue === '') {
        this.GroupNumValue_check = 0
      } else {
        this.GroupNumValue_check = 1
      }
      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 => {
        for (let i = 0; i < response.data.rows.length; i++) {
          if (response.data.rows[i].组件零件基本件流水号 === '0') {
            response.data.rows[i].零件类型 = response.data.rows[i].零件类型代码
            response.data.rows[i].类型名称 = response.data.rows[i].外购件类型
          }
        }
        for (let i = 0; i < response.data.rows.length; i++) {
          if (response.data.rows[i].组件零件流水号 === '0') {
            response.data.rows[i].物料名称 = response.data.rows[i].零件名称
            response.data.rows[i].项目名称 = response.data.rows[i].基本件项目名称
            response.data.rows[i].机床图号 = response.data.rows[i].基本件机床图号
            response.data.rows[i].组号 = response.data.rows[i].基本件组号
            response.data.rows[i].项目流水号 = response.data.rows[i].基本件项目流水号
            response.data.rows[i].机床流水号 = response.data.rows[i].基本件机床流水号
            response.data.rows[i].组件流水号 = response.data.rows[i].基本件组件流水号
          }
        }
        this.tableDataPurchase = response.data.rows
        this.listLoading = false
      })
    },
    // 重置表单
    callOff() {
      this.dialogFormVisible = false
      this.dialogFormVisible2 = false
    },
    addSub() {
      this.dialogFormVisible2 = true
    },
    // 被替换件查询
    getSubstituted() {
      if (this.materialName === '') {
        this.materialName_check = 0
      } else {
        this.materialName_check = 1
      }
      searchSubstituted().then(response => {
        this.tableDataSub = response.data
      })
    },
    // 替换单查询
    getSubstitutedCard() {
      this.listLoading1 = true
      searchSubstitutedCard().then(response => {
        this.tableDataExchange = response.data
        this.listLoading1 = false
      })
    },
    // 选中零件交换
    selecting(row) {
      const arr = new Array(row)
      this.tableData1 = arr
      console.log(this.tableData1)
    },
    // 选中被零件交换
    selected(row) {
      const arr = new Array(row)
      this.tableData2 = arr
    },
    // 确认选择零件
    chooseSub() {
      this.dialogFormVisible2 = false
    },
    // 增加替换件
    addListDetailinfor() {
      addSubstitutepart(this.tableData2[0].离线合同明细流水号, this.tableData1[0].采购合同明细流水号, this.form.ExchangeNumber, this.tableData1[0].物料名称, this.tableData2[0].货位流水号, this.tableData2[0].物料名称, this.picikingPeople, this.form.ExchangeNumberx, this.form.ExchangeNote).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('增加成功')
        } else { this.$message.error('增加失败') }
      })
      this.dialogFormVisible = false
      this.getSubstitutedCard()
    },
    // 打开替换表单
    exchange() {
      this.form = []
      this.dialogFormVisible = true
      this.title = '替换'
    },
    // 删除替换单
    dropListinfor(row) {
      this.substituteListNumber = row.替换单流水号
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSubstitutedCard(this.substituteListNumber).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.getSubstitutedCard().then(response => {
            })
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEdit(row) {
      this.substituteListNumber = row.替换单流水号
      this.form.ExchangeNumber = row.替换单编号
      this.form.ExchangeNumberx = row.替换数量
      this.form.ExchangeNote = row.备注
      this.title = '编辑'
      this.dialogFormVisible = true
    },
    submitEdit() {
      this.editTable(editSubstitutedCard, [this.substituteListNumber, this.form.ExchangeNumber, this.form.ExchangeNumberx, this.form.ExchangeNote], 'form', function() { this.getSubstitutedCard(); this.dialogFormVisible = false })
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.searchPartinfo()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.searchPartinfo()
    }
  }
}
</script>

<style scoped>

</style>
