<template>
  <div class="app-container">
    <el-card>
      <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" style="margin: 0 0 0 10px" @click="ADD()">增加</el-button>
    </el-card>
    <el-card>
      <el-table
        :data="tableData"
        border
        style="width: 100%;max-height: 400px"
        height="400"
        @row-click="getWarehouse">
        <el-table-column label="仓库编号" align="center" width="200">
          <template slot-scope="scope">
            {{ scope.row.仓库编号 }}
          </template>
        </el-table-column>
        <el-table-column label="仓库名称" align="center" width="180">
          <template slot-scope="scope">
            {{ scope.row.仓库名称 }}
          </template>
        </el-table-column>
        <el-table-column label="仓库描述" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row.仓库描述 }}
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center">
          <template slot-scope="scope">
            {{ scope.row.地址 }}
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column prop="budget" label="领用人" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.领用人 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="handleEdit(scope.row)">修改</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" style="margin: 0 0 0 10px" @click="ADDLocate()">增加</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading"
        :data="tableData2"
        border
        style="width: 100%;max-height: 400px"
        height="400">
        <el-table-column label="货位名称" align="center" width="200">
          <template slot-scope="scope">
            {{ scope.row.货位名称 }}
          </template>
        </el-table-column>
        <el-table-column label="工作中心" align="center" width="180">
          <template slot-scope="scope">
            {{ scope.row.工作中心 }}
          </template>
        </el-table-column>
        <el-table-column label="永久" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row.永久 }}
          </template>
        </el-table-column>
        <el-table-column label="不可用量" align="center">
          <template slot-scope="scope">
            {{ scope.row.不可用量 }}
          </template>
        </el-table-column>
        <el-table-column label="仓库名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.仓库名称 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="handleLocateEdit(scope.row)">修改</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="locateDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--仓库表单-->
    <el-dialog :visible.sync="dialogFormVisible" title="增加仓库" center style="min-height: 300px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库编号" prop="WarehouseNumber">
              <el-input
                v-model="form.WarehouseNumber"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="WarehouseName">
              <el-input
                v-model="form.WarehouseName"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库描述" prop="WarehouseDescription">
              <el-input
                v-model="form.WarehouseDescription"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="Address">
              <el-input
                v-model="form.Address"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="领用者" prop="TotalContractAmount" label-width="100px">
              <el-select v-model="PeopleValue" 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-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="Note">
              <el-input
                v-model="form.Note"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')">确定</el-button>
      </div>
    </el-dialog>
    <!--货位表单-->
    <el-dialog :visible.sync="dialogFormVisible2" title="货位" center style="min-height: 300px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="货位名称" prop="货位名称">
              <el-input
                v-model="form2.货位名称"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作中心" prop="工作中心">
              <el-input
                v-model="form2.工作中心"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="永久" prop="永久">
              <el-input
                v-model="form2.永久"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="不可用量" prop="不可用量">
              <el-input
                v-model="form2.不可用量"
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callLocateOff('form2')">取消</el-button>
        <el-button type="primary" @click="Locatesubmit('form2')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { initWarehouse, addWarehouse, alterWarehouse, deleteWarehouse, searchLocate, addLocate, alterLocate, deleteLocate, searchPeople } from '@/api/Inventory/InventoryManagement'
export default {
  data() {
    return {
      tableData: [],
      tableData2: [],
      wareHouse: '',
      dialogFormVisible: false, // 仓库表单
      temp: '',
      form: {
        WarehouseNumber: '', // 仓库编号
        WarehouseName: '', // 仓库名称
        WarehouseDescription: '', // 仓库描述
        Address: '', // 地址
        Note: '' // 备注
      },
      rules: { // 表单验证规则
        WarehouseNumber: [{ required: true, message: '请输入仓库编号', trigger: 'change' }],
        WarehouseName: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
        WarehouseDescription: [{ required: true, message: '请输入仓库描述', trigger: 'blur' }],
        Address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        Note: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      },
      dialogFormVisible2: false, // 货位表单
      form2: {
        货位名称: '',
        工作中心: '',
        永久: '',
        不可用量: ''
      },
      rules2: { // 表单验证规则
        货位名称: [{ required: true, message: '请输入货位名称', trigger: 'change' }],
        工作中心: [{ required: true, message: '请输入工作中心', trigger: 'blur' }],
        永久: [{ required: true, message: '请输入永久', trigger: 'blur' }],
        不可用量: [{ required: true, message: '请输入不可用量', trigger: 'blur' }]
      },
      HouseNumber: '',
      LocateNumber: '',
      listLoading: '',
      PeopleValue: '',
      People: []
    }
  },
  created() {
    this.getTable()
    this.searchPeopleData()
  },
  methods: {
    // 查询仓库
    getTable() {
      this.tableData = []
      this.listLoading = true
      initWarehouse().then(response => {
        this.listLoading = false
        console.log(response)
        for (let i = 0; i < response.data.length; i++) {
          this.tableData.push({
            仓库流水号: response.data[i].仓库流水号,
            仓库编号: response.data[i].仓库编号,
            仓库名称: response.data[i].仓库名称,
            仓库描述: response.data[i].仓库描述,
            地址: response.data[i].地址,
            联系人: response.data[i].联系人,
            备注: response.data[i].备注
          })
        }
      })
    },
    getWarehouse(row) {
      this.wareHouse = row.仓库流水号
      this.getLocate()
    },
    searchPeopleData() { // 查询人员
      searchPeople().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.People.push({
            label: response.data[i].姓名,
            value: response.data[i].考勤编号
          })
        }
      })
    },
    getLocate() {
      this.tableData2 = []
      searchLocate(this.wareHouse).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.tableData2.push({
            货位流水号: response.data[i].货位流水号,
            货位名称: response.data[i].货位名称,
            工作中心: response.data[i].工作中心,
            永久: response.data[i].永久,
            不可用量: response.data[i].不可用量,
            仓库名称: response.data[i].仓库名称
          })
        }
      })
    },
    // 点击增加仓库
    ADD() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible = true
      this.temp = 1
    },
    // 提交添加或者修改仓库
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.temp === 1) {
            this.addTableData()
            this.form.WarehouseNumber = ''
            this.form.WarehouseName = ''
            this.form.WarehouseDescription = ''
            this.form.Address = ''
            this.form.Contactpeople = ''
            this.form.Note = ''
            this.temp = null
          } else if (this.temp === 2) {
            this.chaEdit()
            this.temp = null
          }
        }
      })
    },
    // 执行增加仓库
    addTableData() {
      addWarehouse(this.form.WarehouseNumber, this.form.WarehouseName, this.form.WarehouseDescription, this.form.Address, this.PeopleValue, this.form.Note).then(response => {
        this.dialogFormVisible = false
        if (response.data[0].result === '1') {
          this.$message.success('添加成功')
          this.form.WarehouseNumber = ''
          this.form.WarehouseName = ''
          this.form.WarehouseDescription = ''
          this.form.Address = ''
          this.form.Contactpeople = ''
          this.form.Note = ''
        } else {
          this.$message.error('添加失败')
        }
        this.getTable()
      })
    },
    // 编辑
    handleEdit(row) {
      this.temp = 2
      this.dialogFormVisible = true
      this.form.WarehouseNumber = row.仓库编号
      this.form.WarehouseName = row.仓库名称
      this.form.WarehouseDescription = row.仓库描述
      this.form.Address = row.地址
      this.form.Contactpeople = row.联系人
      this.form.Note = row.备注
      this.HouseNumber = row.仓库流水号
    },
    // 执行修改
    chaEdit() {
      alterWarehouse(this.HouseNumber, this.form.WarehouseNumber, this.form.WarehouseName, this.form.WarehouseDescription, this.form.Address, this.form.Contactpeople, this.form.Note).then(response => {
        this.dialogFormVisible = false
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.form.WarehouseNumber = ''
          this.form.WarehouseName = ''
          this.form.WarehouseDescription = ''
          this.form.Address = ''
          this.form.Contactpeople = ''
          this.form.Note = ''
          this.getTable()
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    // 删除仓库
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWarehouse(row.仓库流水号).then(response => {
          if (response.data[0].result === '1') {
            this.getTable()
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    // 重置表单
    callOff(formName) {
      console.log(1515)
      this.form = {}
      this.form2.ContractNumberValue = ''
      this.form2.EntryName = ''
      this.form2.TotalContractAmount = ''
      this.form.PaymentPhaseValue = 1
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 点击增加货位
    ADDLocate() {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.dialogFormVisible2 = true
      this.temp = 1
    },
    // 提交添加或者修改货位
    Locatesubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.temp === 1) {
            this.addLocateTable()
            this.form2.货位名称 = ''
            this.form2.工作中心 = ''
            this.form2.仓库流水号 = ''
            this.form2.永久 = ''
            this.form2.不可用量 = ''
            this.temp = null
          } else if (this.temp === 2) {
            this.LocateEdit()
            this.temp = null
          }
        }
      })
    },
    // 执行增加货位
    addLocateTable() {
      addLocate(this.form2.货位名称, this.form2.工作中心, this.wareHouse, this.form2.永久, this.form2.不可用量).then(response => {
        this.dialogFormVisible2 = false
        if (response.data[0].result === '1') {
          this.$message.success('添加成功')
          this.form.货位名称 = ''
          this.form.工作中心 = ''
          this.form.仓库流水号 = ''
          this.form.永久 = ''
          this.form.不可用量 = ''
        } else {
          this.$message.error('添加失败')
        }
        this.getLocate()
      })
    },
    // 编辑货位
    handleLocateEdit(row) {
      this.temp = 2
      this.form2.货位名称 = row.货位名称
      this.form2.工作中心 = row.工作中心
      this.form2.仓库流水号 = row.仓库流水号
      this.form2.永久 = row.永久
      this.form2.不可用量 = row.不可用量
      this.LocateNumber = row.货位流水号
      this.dialogFormVisible2 = true
    },
    // 执行修改货位
    LocateEdit() {
      alterLocate(this.LocateNumber, this.form2.货位名称, this.form2.工作中心, this.form2.仓库流水号, this.form2.永久, this.form2.不可用量).then(response => {
        this.dialogFormVisible2 = false
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.form.货位名称 = ''
          this.form.工作中心 = ''
          this.form.仓库流水号 = ''
          this.form.永久 = ''
          this.form.不可用量 = ''
          this.getLocate()
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    // 删除货位
    locateDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteLocate(row.货位流水号).then(response => {
          if (response.data[0].result === '1') {
            this.getLocate()
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    // 重置表单
    callLocateOff(formName) {
      this.form2 = {}
      this.form2.货位名称 = ''
      this.form2.工作中心 = ''
      this.form2.仓库流水号 = ''
      this.form.永久 = ''
      this.form.不可用量 = ''
      this.dialogFormVisible2 = false
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
