<template>
	<div class="app-container">

<!-- 搜索工作栏 -->
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />

    <!-- 列表 -->
		<base-table
      v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:max-height="tableH"
			:table-data="tableData">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="80"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />

		<!-- 对话框(添加 / 修改) -->
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="addOrUpdateVisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="40%">
			<add-or-update
				ref="addOrUpdate"
				@refreshDataList="successSubmit"></add-or-update>
		</base-dialog>
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import basicPage from '../../mixins/basic-page';
import { parseTime } from '../../mixins/code-filter';
import codeFilter from '../../mixins/code-filter';
import {
  deleteWarehouse,
  getWarehousePage,
} from "@/api/warehouse/warehouse-setup";
import tableHeightMixin from '@/mixins/tableHeightMixin';
import { publicFormatter } from '@/utils/dict';

const tableProps = [
	{
		prop: 'name',
		label: '仓库名称',
	},
	{
		prop: 'creator',
		label: '创建人',
	},
	{
		prop: 'createTime',
		label: '创建时间',
		filter: parseTime,
		minWidth: 100,
	},
	{
		prop: 'type',
		label: '仓库类型',
    filter: publicFormatter('warehouse_type')
	},
	{
		prop: 'enabled',
		label: '是否启用',
		filter: codeFilter('deactivate'),
	},
];

export default {
	mixins: [basicPage,tableHeightMixin],
	data() {
		return {
			urlOptions: {
				getDataListURL: getWarehousePage,
				deleteURL: deleteWarehouse,
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`warehouse:warehouse-setup:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
          this.$auth.hasPermi(`warehouse:warehouse-setup:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v)=>v),
			tableData: [],
			formConfig: [
				{
					type: 'input',
					label: '仓库名称',
					placeholder: '仓库名称',
					param: 'name',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('warehouse:warehouse-setup:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.name = val.name;
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 20,
						pageNo: 1,
						total: 1,
					};
					this.getDataList();
					break;
				case 'add':
					this.addOrEditTitle = '新增';
					this.addOrUpdateVisible = true;
					this.addOrUpdateHandle();
					break;
				case 'export':
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
	},
};
</script>
