<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="listQuery" size="small" :inline="true" label-position="top">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="仓库编号">
            <el-input clearable placeholder="仓库编号" v-model="listQuery.warehouseCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓库名称">
            <el-input clearable placeholder="仓库名称" v-model="listQuery.warehouseName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="管理员">
            <el-input clearable placeholder="管理员" v-model="listQuery.adminId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="搜索">
            <el-button style="width: 100%;" type="primary" icon="el-icon-search"
              @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="重置">
            <el-button style="width: 100%;" icon="el-icon-refresh" type="primary"
              @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" class="editButton" size="small" icon="el-icon-plus"
          @click="handleCreate">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="small" @click="handleDownload">导出</el-button>
      </el-col>
    </el-row>

    <!-- 查询结果 -->
    <div class="border-card">
      <el-table height="450" size="small" :data="list" v-loading="listLoading"
        element-loading-text="正在查询中。。。" border fit highlight-current-row>

        <el-table-column type="index" label="序号" header-align="center" align="center">
        </el-table-column>

        <el-table-column align="center" min-width="100px" label="仓库编号" prop="warehouseCode">
        </el-table-column>

        <el-table-column align="center" min-width="100px" label="仓库名称" prop="warehouseName">
        </el-table-column>

        <el-table-column align="center" min-width="80px" label="管理员" prop="adminId">
        </el-table-column>

        <el-table-column align="center" min-width="80px" label="联系电话" prop="adminPhone">
        </el-table-column>

        <el-table-column align="center" min-width="200px" label="仓库地址" prop="warehouseAddress">
        </el-table-column>

        <el-table-column align="center" min-width="150px" label="备注" prop="remarks">
        </el-table-column>

        <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" class="editText" size="small"
              @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="deleteText" size="small"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="listQuery.page" :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible"
      width="70%">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left"
        label-width="100px" style='margin: 0 50px;'>
        <el-form-item label="仓库编号" prop="warehouseCode">
          <el-input v-model="dataForm.warehouseCode" placeholder="请输入仓库编号"></el-input>
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="dataForm.warehouseName" placeholder="请输入仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="管理员" prop="adminId">
          <el-input v-model="dataForm.adminId" placeholder="请输入仓库管理员"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="adminPhone">
          <el-input v-model="dataForm.adminPhone" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"
            placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="warehouseAddress">
          <el-input v-model="dataForm.warehouseAddress" placeholder="请输入仓库地址"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入备注"
            v-model="dataForm.remarks">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarehouse, createWarehouse, updateWarehouse, deleteWarehouse } from '@/api/warehouse'
import waves from '@/directive/waves'

export default {
  directives: { waves },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        warehouseCode: '',
        warehouseName: '',
        adminId: ''
      },
      dataForm: {
        warehouseCode: undefined,
        warehouseName: undefined,
        adminId: undefined,
        adminPhone: undefined,
        warehouseAddress: undefined,
        remarks: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑仓库',
        create: '添加仓库'
      },
      rules: {
        warehouseCode: [
          { required: true, message: '仓库编号不能为空', trigger: 'blur' }
        ],
        warehouseName: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }],
        adminId: [{ required: true, message: '管理员不能为空', trigger: 'blur' }],
        adminPhone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
        warehouseAddress: [{ required: true, message: '仓库地址不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetQuery() {
      this.listQuery = {
        page: 1,
        limit: 10,
        warehouseCode: '',
        warehouseName: '',
        adminId: ''
      }
      this.handleQuery()
    },
    getList() {
      this.listLoading = true
      listWarehouse(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleQuery() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        warehouseCode: undefined,
        warehouseName: undefined,
        adminId: undefined,
        adminPhone: undefined,
        warehouseAddress: undefined,
        remarks: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createWarehouse(this.dataForm).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateWarehouse(this.dataForm).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWarehouse({ id: row.id }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => { })
    },
    handleDownload() {
      window.location.href = process.env.BASE_API + '/warehouse-info/export'
    }
  }
}
</script>