<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>
      <span>采购合同:</span>
      <el-select v-model="ContractValue" filterable clearable size="small" placeholder="采购合同" style="width: 200px">
        <el-option
          v-for="item in Contract"
          :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
        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="120">
          <template slot-scope="scope" align="center">
            <el-button size="mini" type="primary" icon="el-icon-sort-up" style="margin: 0 0 0 0px" @click="addDirect(scope.row)">直达</el-button>
            <el-button size="mini" type="primary" icon="el-icon-edit" style="margin: 0 0 0 0px" @click="searchDirect(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-table
        v-loading="listLoading1"
        :data="directRecord"
        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="small" type="warning" icon="el-icon-edit" style="margin: 0 0 0 0px" @click="changeDirect(scope.row)">编辑</el-button>
            <el-button 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>
    </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.DirectNumber" size="small" placeholder="到货数量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="领用者" prop="TotalContractAmount" label-width="100px">
          <el-select v-model="form.getPeople" filterable clearable size="small" placeholder="领用者" style="width: 200px;margin-right:10px" @change="searchGroupList()">
            <el-option
              v-for="item in People"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="TotalContractAmount" label-width="100px">
          <el-input v-model="form.DirectNote" 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="addDirectPartm()">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑直达件对话框-->
    <el-dialog :visible.sync="dialogFormVisible1" 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.DirectNumber" size="small" placeholder="到货数量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="领用者" prop="TotalContractAmount" label-width="100px">
          <el-select v-model="form.getPeople" filterable clearable size="small" placeholder="领用者" style="width: 200px;margin-right:10px" @change="searchGroupList()">
            <el-option
              v-for="item in People"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="TotalContractAmount" label-width="100px">
          <el-input v-model="form.DirectNote" 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="editDirectPartm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { initProject, searchmachine, searchTeam, searchDirectPart, searchPeople, searchContract, addDirectPart, searchDirectRec, editDirectRec, deleteDirectRec } from '@/api/Inventory/DirectPart'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      ProjectValue: '',
      Project: [],
      ProjectValue_check: 1,
      MachineValue: '',
      Machine: [],
      MachineValue_check: 1,
      GroupNumValue: '',
      GroupNumValue_check: 1,
      GroupNum: [],
      tableDataPurchase: [],
      form: {
        DirectNumber: '',
        getPeople: '',
        DirectNote: ''
      },
      PeopleValue: '',
      People: [],
      Contract: [],
      ContractValue: '',
      ContractValue_check: 1,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      rules: { // 表单验证规则
        DirectNumber: [{ required: true, message: '请输入直达件数量', trigger: 'blur' }],
        getPeople: [{ required: true, message: '请选择领用人', trigger: 'blur' }],
        DirectNote: [{ required: true, message: '请输入直达件备注', trigger: 'change' }]
      },
      materialNum: '',
      purchaseContractNumber: '',
      locate: '',
      PeopleNumber: '', // 入库人员流水号
      directRecord: [],
      teamPart: '',
      listLoading: '',
      listLoading1: '',
      PageCurrent: 1,
      PageSize: 10,
      total: null,
      projectNumber: '', // 项目流水号
      machineNumber: '', // 机床流水号
      teamNumber: '', // 组件流水号
      materialName: '', // 物料名称
      materialtype: '', // 物料型号
      materialMode: '', // 物料规格
      partPicture: '', // 零件图号
      directPartNumber: '',
      teamBasicPart: ''
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.searchPeopleData()
    this.searchContractData()
    this.getpeopleid()
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'Project') // 项目名称
    },
    // 获取人员编号
    getpeopleid() {
      this.PeopleNumber = this.id
    },
    searchMachine() { // 查询机床
      this.Machine = []
      this.ProjectValue ? this.ProjectValue_check = 1 : this.ProjectValue_check = 0
      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].机床流水号
          })
        }
      })
    },
    searchGroupList() { // 查询组号
      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].组件流水号
          })
        }
      })
    },
    searchPeopleData() { // 查询人员
      this.People = []
      searchPeople(this.token).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.People.push({
            label: response.data[i].姓名,
            value: Number(response.data[i].人员编号)
          })
        }
        console.log(this.People)
      })
      console.log(this.token, 88)
      console.log(this.People, 99)
    },
    searchContractData() { // 查询合同
      this.Contract = []
      searchContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Contract.push({
            label: response.data[i].采购合同编号,
            value: response.data[i].采购合同流水号
          })
        }
      })
    },
    searchPartinfo() {
      this.tableDataPurchase = []
      this.ProjectValue ? this.ProjectValue_check = 1 : this.ProjectValue_check = 0
      this.MachineValue ? this.MachineValue_check = 1 : this.MachineValue_check = 0
      this.GroupNumValue ? this.GroupNumValue_check = 1 : this.GroupNumValue_check = 0
      this.ContractValue ? this.ContractValue_check = 1 : this.ContractValue_check = 0
      this.listLoading = true
      searchDirectPart(this.PageCurrent, this.PageSize, this.ProjectValue_check, this.ProjectValue, this.MachineValue_check, this.MachineValue, this.GroupNumValue_check, this.GroupNumValue, this.ContractValue_check, this.ContractValue).then(response => {
        this.tableDataPurchase = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 查询直达件记录
    searchDirect(row) {
      this.purchaseContractNumber = row.采购合同明细流水号
      this.listLoading = true
      searchDirectRec(this.purchaseContractNumber).then(response => {
        this.directRecord = response.data
        this.listLoading = false
      })
    },
    // 打开直达件表单
    addDirect(row) {
      console.log(row)
      this.PeopleValue = ''
      this.form = {}
      this.dialogFormVisible = true
      this.materialNum = row.物料流水号
      this.purchaseContractNumber = row.采购合同明细流水号
      this.teamPart = row.组件零件流水号
      this.teamBasicPart = row.组件零件基本件流水号
      this.projectNumber = row.项目流水号
      this.machineNumber = row.机床流水号
      this.teamNumber = row.组件流水号
      this.materialName = row.物料名称
      this.materialtype = row.物料型号
      this.materialMode = row.物料规格
      this.partPicture = row.零件图号
    },
    // 打开直达件编辑表单
    changeDirect(row) {
      console.log(row)
      this.dialogFormVisible1 = true
      this.directPartNumber = row.直达件流水号
      this.form.DirectNumber = row.到货数量
      this.form.getPeople = parseInt(row.领用人员流水号)
      this.form.DirectNote = row.备注
    },
    // 修改直达件
    editDirectPartm() {
      editDirectRec(this.directPartNumber, this.form.DirectNumber, this.PeopleValue, this.form.DirectNote).then(response => {
        if (response.data[0].result === '1') {
          this.$notify({
            title: '成功',
            message: '信息修改成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.searchDirect()
        } else {
          this.$message.error('信息修改失败')
        }
      })
      searchDirectRec(this.purchaseContractNumber).then(response => {
        this.directRecord = response.data
        this.listLoading = false
      })
      this.dialogFormVisible1 = false
    },
    // 重置表单
    callOff() {
      this.form.DirectNumber = ''
      this.form.getPeople = ''
      this.form.DirectNote = ''
      this.dialogFormVisible = false
      this.dialogFormVisible1 = false
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.searchPartinfo()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.searchPartinfo()
    },
    // 增加直达件
    addDirectPartm() {
      addDirectPart(this.form.DirectNumber, this.purchaseContractNumber, this.PeopleNumber, this.form.getPeople, this.form.DirectNumber, this.teamPart, this.teamBasicPart, this.projectNumber, this.machineNumber, this.teamNumber, this.materialName, this.form.DirectNote, this.materialtype, this.materialMode, this.partPicture).then(response => {
        if (response.data[0].result === '1') {
          this.$notify({
            title: '成功',
            message: '信息添加成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.searchDirect()
        } else {
          this.$message.error('信息添加失败')
        }
      })
      this.dialogFormVisible = false
    },
    // 删除领料单
    dropListinfor(row) {
      this.directPartNumber = row.直达件流水号
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDirectRec(this.directPartNumber).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            searchDirectRec(this.purchaseContractNumber).then(response => {
              this.directRecord = response.data
            })
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
