<template>
  <!-- 仓库管理 -->
  <div class="home">
    <actionBar :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm" @openModel="openModel"
      @searchProtocol="searchProtocol"></actionBar>
    <div class="card_content">
      <!-- 表单 -->
      <tables :suspension="true" :tableList="tableList" :tableData="tableData" @buttonData="buttonData"></tables>
      <!-- 分页 -->
      <div v-if="Total">
        <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
          @changeCurrent="changeCurrent">
        </pagination>
      </div>
    </div>
    <el-dialog :title="staffTitle" :visible.sync="staffDialogVisible" width="600px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="100px"></forms>
      <span slot="footer" class="dialog-footer" v-if="operationType != 'logs'">
        <el-button plain @click="staffDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getSysUserBind
  } from '@/api/user'
  import {
    getWarehouse,
    getWarehouseDetails,
    addWarehouse,
    putWarehouse,
    delWarehouse
  } from '@/api/warehouse'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    formRules,
    employee
  } from "./warehouse.js";
  export default {
    name: 'StoreManagement',
    components: {
      actionBar,
      tables,
      pagination,
      forms
    },
    data() {
      return {
        operateList: [{
          type: 'add',
          title: '添加仓库',
          icon: 'el-icon-plus',
        }],
        formList: [{
          type: 'input',
          label: '名称/管理员名称',
          field: 'name',
          placeholder: '仓库名称/管理员名称',
        }, {
          type: 'input',
          label: 'SN',
          field: 'sn',
          placeholder: 'SN',
        }],
        searchRuleForm: {
          name: '',
          sn: '',
        },
        searchValue: {},
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        Total: 0,
        tableData: [],
        operationType: '',
        tableList: employee(),
        tableData: [],
        staffTitle: '添加',
        staffDialogVisible: false,
        formRuleList: [],
        ruleForm: {
          name: '',
          sn: '',
          address: '',
          status: '',
          userId: '',
        },
        confirmLoading: false,
        selectingData: {},
      }
    },
    mounted() {
      this.getDriver()
      const dataList = formRules();
      this.formRuleList = dataList;
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      // 获取订单列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        getWarehouse(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.Total = res.data.count
          }
        })
      },
      // 获取仓库管理员列表
      getDriver() {
        getSysUserBind({
          type: 2,
        }).then(res => {
          if (res.code == 200) {
            const arrList = []
            const arr = res.data.list
            arr.forEach((item, index) => {
              const commodity = {
                label: item.nickName,
                value: item.id,
                IsBind: item.IsBind,
              }
              arrList.push(commodity)
            })
            this.optionMatching(arrList, 'userId')
          }
        })
      },
      // 选项赋值
      optionMatching(value, field) {
        this.formRuleList.forEach((item, index) => {
          if (item.field == field) {
            item.options = value
          }
        })
      },
      // 弹窗表单添加
      handleAdd() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          if (this.operationType == 'add') {
            this.confirmLoading = true
            var params = {
              ...this.ruleForm
            }
            params.userId = Number(params.userId)
            addWarehouse(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          } else if (this.operationType == 'edit') {
            this.confirmLoading = true
            var params = {
              id: this.selectingData.id,
              address: this.ruleForm.address,
              name: this.ruleForm.name,
              sn: this.ruleForm.sn,
              status: this.ruleForm.status,
              userId: this.ruleForm.userId,
            }
            params.userId = Number(params.userId)
            putWarehouse(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      buttonData(row, type) {
        this.selectingData = row
        this.operationType = type
        if (type == 'edit') {
          this.staffTitle = '编辑'
          this.staffDialogVisible = true
          setTimeout(() => {
            this.$nextTick(() => {
              this.ruleForm = JSON.parse(JSON.stringify(row))
              this.ruleForm.userId = String(row.userId)
            })
          })
        } else if (type == 'del') {
          this.deleteUser(row.id)
        } else if (type == 'logs') {
          this.staffTitle = '详情'
          this.staffDialogVisible = true
          this.formRuleList.forEach((item, index) => {
            item.disabled = true
          })
          this.ruleForm = JSON.parse(JSON.stringify(row))
          this.ruleForm.userId = row.user.nickName
        }
      },
      openModel(type) {
        this.getDriver()
        this.staffTitle = '添加'
        this.staffDialogVisible = true
        this.operationType = type
      },
      // 删除仓库
      deleteUser(id) {
        this.$confirm('此操作将永久删除该仓库, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delWarehouse({
            id: id,
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        }).catch(() => {});
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      // 清空表单
      closeDialog() {
        this.formRuleList.forEach((item, index) => {
          item.disabled = false
        })
        this.ruleForm = {}
        this.$refs.childRules.resetCheck();
      }
    }
  }
</script>
