<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" style="margin: 0 0 0 10px" @click="ADD()">新建仓库</el-button>
      </div>
      <el-table :data="tableData" border size="mini" style="width: 100%;max-height: 300px" height="300" @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="仓库描述" min-width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.仓库描述 }}
          </template>
        </el-table-column>
        <el-table-column label="地址" min-width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.地址 }}
          </template>
        </el-table-column>
        <el-table-column label="联系人" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.联系人 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="handleEdit(scope.$index, scope.row, 'form')">修改</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
            <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline" style="margin: 0 0 0 10px" @click="ADDLocate()">增加货位</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <el-table v-loading="listLoading" :data="tableData2" border size="mini" style="width: 100%;max-height: 300px" height="300">
        <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 align="center" label="二维码" width="150">
          <template slot-scope="scope">
            <canvas :id="canvas[scope.$index]" class="code"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <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 width="450px">
      <el-form ref="form" :model="form" :rules="rules" label-position="center" 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"
                clearable
                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"
                clearable
                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"
                clearable
                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"
                clearable
                size="small"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="ContactpeopleValue">
              <el-select v-model="form.ContactpeopleValue" clearable filterable size="small" placeholder="联系人" style="width: 200px;margin-right:10px">
                <el-option
                  v-for="item in Contactpeople"
                  :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"
                clearable
                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-select v-model="form2.永久" clearable filterable placeholder="请选择" size="small" style="width: 200px;margin-right:10px">
                <el-option
                  v-for="item in options"
                  :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="不可用量">
              <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'
import QRCode from 'qrcode'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      Contactpeople: [], // 联系人
      count2: 0, // 清表单验证计数器
      tableData: [],
      tableData2: [],
      wareHouse: '',
      dialogFormVisible: false, // 仓库表单
      temp: '',
      form: {
        WarehouseNumber: '', // 仓库编号
        WarehouseName: '', // 仓库名称
        WarehouseDescription: '', // 仓库描述
        Address: '', // 地址
        ContactpeopleValue: '',
        Note: '' // 备注
      },
      rules: { // 表单验证规则
        WarehouseNumber: [{ required: true, message: '请输入仓库编号', trigger: 'blur' }],
        ContactpeopleValue: [{ 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: [],
      canvas: [],
      options: [{
        value: '1',
        label: '是'
      }, {
        value: '0',
        label: '否'
      }],
      role: ''
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  created() {
    this.getTable()
    this.getpeopleid()
  },
  methods: {
    // 获取人员编号
    getpeopleid() {
      this.role = this.token
      this.searchPeopleData()
    },
    // 查询仓库
    getTable() {
      this.tableData = []
      this.listLoading = true
      initWarehouse().then(response => {
        this.listLoading = false
        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].备注,
            人员编号: response.data[i].人员编号
          })
        }
      })
    },
    getWarehouse(row) {
      this.wareHouse = row.仓库流水号
      this.getLocate()
    },
    searchPeopleData() { // 查询人员
      this.Contactpeople = []
      searchPeople(this.role).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Contactpeople.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    getLocate() {
      this.tableData2 = []
      searchLocate(this.wareHouse).then(response => {
        const data = response.data
        for (let i = 0; i < data.length; i++) {
          this.canvas.push(response.data[i].货位流水号.toString())
          this.tableData2.push({
            货位流水号: response.data[i].货位流水号,
            货位名称: response.data[i].货位名称,
            工作中心: response.data[i].工作中心,
            永久: response.data[i].永久,
            不可用量: response.data[i].不可用量,
            仓库名称: response.data[i].仓库名称
          })
        }
        return data
      }).then(res => {
        for (let i = 0; i < res.length; i++) {
          QRCode.toCanvas(document.getElementById(this.canvas[i]), res[i].货位流水号.toString(), function(error) {
            if (error) console.error(error)
            console.log('success!')
          })
        }
        this.tableData2 = res
      })
    },
    // 点击增加仓库
    ADD() {
      this.form = []
      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.ContactpeopleValue = ''
            this.form.Note = ''
            this.temp = ''
          } else if (this.temp === 2) {
            this.chaEdit()
            this.temp = ''
          }
        }
      })
    },
    // 执行增加仓库
    addTableData() {
      addWarehouse(this.form.WarehouseNumber, this.form.WarehouseName, this.form.WarehouseDescription, this.form.Address, this.form.ContactpeopleValue, this.form.Note).then(response => {
        console.log(this.form.WarehouseNumber, this.form.WarehouseName, this.form.WarehouseDescription, this.form.Address, this.form.ContactpeopleValue, this.form.Note)
        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.ContactpeopleValue = ''
          this.form.Note = ''
        } else {
          this.$message.error('添加失败')
        }
        this.getTable()
      })
    },
    // 编辑
    handleEdit(index, row, formName) {
      this.temp = 2
      this.dialogFormVisible = true
      this.form.WarehouseNumber = row.仓库编号
      this.form.WarehouseName = row.仓库名称
      this.form.WarehouseDescription = row.仓库描述
      this.form.Address = row.地址
      this.form.ContactpeopleValue = 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.ContactpeopleValue, this.form.Note).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.dialogFormVisible = false
          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) {
      this.form = {}
      this.form2.ContractNumberValue = ''
      this.form2.EntryName = ''
      this.form2.TotalContractAmount = ''
      this.form.PaymentPhaseValue = 1
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 点击增加货位
    ADDLocate() {
      this.form2 = []
      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('删除失败')
          }
        })
      })
    },
    useqrcode() {
      // 生成的二维码内容，可以添加变量
      this.QueryDetail = this.num
      var canvas = document.getElementById('canvas')
      QRCode.toCanvas(canvas, this.QueryDetail, function(error) {
        if (error) console.error(error)
        console.log('success!')
      })
    },
    // 重置表单
    callLocateOff(formName) {
      this.form2 = {}
      this.form2.货位名称 = ''
      this.form2.工作中心 = ''
      this.form2.仓库流水号 = ''
      this.form.永久 = ''
      this.form.不可用量 = ''
      this.dialogFormVisible2 = false
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
  .code{
    width:120px!important;
    height: auto!important;
  }
  .el-card{
    margin-bottom: 15px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
</style>
