<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"
			:selectWidth="selectHasPermi"
			@selection-change="selectChange"
			:max-height="tableH"
			:table-data="tableData">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="168"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />

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

<script>
import AddOrUpdate from './add-or-updata';
import Drawer from './drawer';
import basicPage from '@/mixins/basic-page';
import { parseTime } from '@/mixins/code-filter';
import { getWarehouseRealtimeLocationPage } from '@/api/warehouse/warehouseRealtimeLocation';
import { listData } from '@/api/system/dict/data';
import { publicFormatter } from '@/utils/dict';
import { mapGetters } from 'vuex';
import tableHeightMixin from '@/mixins/tableHeightMixin';

const tableProps = [
	{
		prop: 'warehouseName',
		label: '仓库名称',
	},
	{
		prop: 'areaName',
		label: '库区名称',
	},
	{
		prop: 'areaType',
		label: '库区类型',
	},
	{
		prop: 'name',
		label: '库位名称',
	},
	{
		prop: 'code',
		label: '库位编码',
	},
	{
		prop: 'type',
		label: '库位类型',
		filter: publicFormatter('location_type'),
	},
	{
		prop: 'status',
		label: '库位状态',
		filter: publicFormatter('AreaDetStatus'),
	},
	{
		prop: 'palletCode',
		label: '托盘编码',
	},
	{
		prop: 'inTime',
		label: '入库时间',
		filter: parseTime,
		width: 150,
	},
	{
		prop: 'goodsName',
		label: '物品名称',
	},
	{
		prop: 'remark',
		label: '备注',
	},
];

export default {
	mixins: [basicPage, tableHeightMixin],
	computed: {
		...mapGetters(['nickname']),
	},
	data() {
		return {
			urlOptions: {
				getDataListURL: getWarehouseRealtimeLocationPage,
			},
			listQuery: {
				warehouseId: '',
			},
			tableData: [],
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`warehouse:warehouse-manage:in&out:detail`)
					? {
							type: 'detail',
							btnName: '详情',
					  }
					: undefined,
				this.$auth.hasPermi(`warehouse:warehouse-manage:in&out:out`)
					? {
							type: 'out',
							btnName: '出库',
							showParam: {
								type: '&',
								data: [
									{
										type: 'unequal',
										name: 'status',
										value: 0,
									},
								],
							},
					  }
					: undefined,
				this.$auth.hasPermi(`warehouse:warehouse-manage:in&out:in`)
					? {
							type: 'in',
							btnName: '入库',
							showParam: {
								type: '&',
								data: [
									{
										type: 'equal',
										name: 'status',
										value: 0,
									},
								],
							},
					  }
					: undefined,
				this.$auth.hasPermi(`warehouse:warehouse-manage:in&out:move`)
					? {
							type: 'move',
							btnName: '移库',
							showParam: {
								type: '&',
								data: [
									{
										type: 'unequal',
										name: 'status',
										value: 0,
									},
								],
							},
					  }
					: undefined,
			].filter((v) => v),
			drawerVisible: false,
			selectData: [],
      selectHasPermi:this.$auth.hasPermi(`warehouse:warehouse-manage:in&out:out`)
					? 55
					: undefined,
			formConfig: [
				{
					type: 'input',
					label: '库位编码',
					placeholder: '库位编码',
					param: 'kcode',
				},
				{
					type: 'input',
					label: '托盘编码',
					placeholder: '托盘编码',
					param: 'tcode',
				},
				{
					type: 'select',
					label: '库位状态',
					selectOptions: [],
					param: 'status',
					defaultSelect: '',
					filterable: true,
					labelField: 'label',
					valueField: 'value',
				},
				{
					type: 'datePicker',
					label: '入库时间',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'timestamp',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'searchTime',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: '',
					btnName: '批量出库',
					name: 'allOut',
					color: 'primary',
				},
			],
		};
	},
	components: {
		AddOrUpdate,
		Drawer,
	},
	created() {
		let str = this.$router.currentRoute.path;
		let lastIndex = str.lastIndexOf('/');
		this.listQuery.warehouseId = str.slice(lastIndex + 1);
		const queryParams = {
			pageNo: 1,
			pageSize: 99,
			dictType: 'AreaDetStatus',
		};
		listData(queryParams).then((response) => {
			this.formConfig[2].selectOptions = response.data.list;
		});
	},
	methods: {
		selectChange(val) {
			this.selectData = val;
			if (this.selectData.length > 0) {
				this.formConfig[5].type = 'button';
			} else {
				this.formConfig[5].type = '';
			}
		},
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.locationCode = val.kcode;
					this.listQuery.palletCode = val.tcode;
					this.listQuery.status = val.status;
					this.listQuery.inTime = val.searchTime ? val.searchTime : null;
					this.getDataList();
					break;
				case 'allOut':
					this.addOrUpdateVisible = true;
					this.addOrEditTitle = '批量出库';
					this.$nextTick(() => {
						this.$refs.addOrUpdate.init('批量出库', this.selectData);
					});
					break;
				default:
					console.log(val);
			}
		},
		otherMethods(val) {
			if (val.type === 'out') {
				this.addOrUpdateVisible = true;
				this.addOrEditTitle = '出库';
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init(val.data.id);
				});
			} else if (val.type === 'in') {
				this.drawerVisible = true;
				this.$nextTick(() => {
					this.$refs.drawerRef.init(val.data, this.nickname);
				});
			} else if (val.type === 'detail') {
				this.drawerVisible = true;
				this.$nextTick(() => {
					this.$refs.drawerRef.init(val.data, this.nickname,true);
				});
			} else if (val.type === 'move') {
				this.addOrUpdateVisible = true;
				this.addOrEditTitle = '移库';
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init('移库', val.data.locationId);
				});
			}
		},
		handleCancel() {
			this.addOrUpdateVisible = false;
			this.addOrEditTitle = '';
		},
	},
};
</script>
<style>
.app-container .el-table .el-table__cell {
	padding: 0;
	height: 35px;
}
</style>
