<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 codeFilter from '@/mixins/code-filter';
import {
	deleteWarehouseArea,
	getWarehouseAreaPage,
	getWarehouseAreaListAll,
} from '@/api/warehouse/warehouse-area-setup';
import { getWarehouseList } from '@/api/warehouse/warehouse-setup';
import tableHeightMixin from '@/mixins/tableHeightMixin';

const tableProps = [
	{
		prop: 'warehouseName',
		label: '仓库名称',
	},
	{
		prop: 'name',
		label: '库区名称',
	},
	{
		prop: 'areaType',
		label: '库区类型',
	},
	{
		prop: 'enabled',
		label: '是否启用',
		filter: codeFilter('deactivate'),
	},
];

export default {
	mixins: [basicPage, tableHeightMixin],
	data() {
		return {
			urlOptions: {
				getDataListURL: getWarehouseAreaPage,
				deleteURL: deleteWarehouseArea,
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`warehouse:warehouse-area-setup:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi(`warehouse:warehouse-area-setup:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			tableData: [],
			formConfig: [
				{
					type: 'select',
					label: '仓库',
					selectOptions: [],
					param: 'warehouseId',
					defaultSelect: '',
					filterable: true,
				},
				{
					type: 'select',
					label: '库区',
					selectOptions: [],
					param: 'areaId',
					defaultSelect: '',
					filterable: true,
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('warehouse:warehouse-area-setup:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {
		getWarehouseList().then((response) => {
			this.formConfig[0].selectOptions = response.data;
		});
		getWarehouseAreaListAll().then((response) => {
			this.formConfig[1].selectOptions = response.data;
		});
	},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.warehouseId = val.warehouseId;
					this.listQuery.areaId = val.areaId;
					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);
			}
		},
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
			getWarehouseAreaListAll().then((response) => { //刷新搜索框库区
				this.formConfig[1].selectOptions = response.data;
			});
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        if (response.hasOwnProperty('data')) {
          this.tableData = response.data.list;
          this.listQuery.total = response.data.total;
        } else {
          this.tableData = []
        }
        this.dataListLoading = false;
      });
    },
	},
};
</script>
