<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<search-bar
			:isFold="true"
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />

		<!-- 列表 -->
		<base-table
			v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:table-data="tableData">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="140"
				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"
			:before-close="handleCancel"
			width="50%">
			<add-or-update
				ref="addOrUpdate"
				@refreshDataList="successSubmit"></add-or-update>
			<slot name="footer">
				<el-row slot="footer" type="flex" justify="end">
					<el-col :span="24">
						<el-button size="small" class="btnTextStyle" @click="handleCancel">
							取消
						</el-button>
					</el-col>
				</el-row>
			</slot>
		</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,
	outWarehouseRealtimeLocation,
} from '@/api/warehouse/warehouseRealtimeLocation';
import { listByWarehouse } from '@/api/warehouse/warehouseLocation';
import { getWarehouseList } from '@/api/warehouse/warehouse-info';
import { listData } from '@/api/system/dict/data';
import { publicFormatter } from '@/utils/dict';
import { mapGetters } from 'vuex';

const tableProps = [
	{
		prop: 'warehouseName',
		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,
		minWidth: 100,
	},
	{
		prop: 'remark',
		label: '备注',
	},
];

export default {
	mixins: [basicPage],
	computed: {
		...mapGetters(['nickname']),
	},
	data() {
		return {
			urlOptions: {
				getDataListURL: getWarehouseRealtimeLocationPage,
				outURL: outWarehouseRealtimeLocation,
			},
			listQuery: {
				storageType: 1,
			},
			tableData: [],
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`raw-material:warehouse-realtime-location:query`)
					? {
							type: 'out',
							btnName: '出库',
					  }
					: undefined,
				this.$auth.hasPermi(`raw-material:warehouse-realtime-location:query`)
					? {
							type: 'in',
							btnName: '入库',
					  }
					: undefined,
				this.$auth.hasPermi(`raw-material:warehouse-realtime-location:query`)
					? {
							type: 'detail',
							btnName: '详情',
					  }
					: undefined,
			].filter((v) => v),
			drawerVisible: false,
			formConfig: [
				{
					type: 'select',
					label: '库位名',
					selectOptions: [],
					param: 'name',
					defaultSelect: '',
					filterable: true,
				},
				{
					type: 'input',
					label: '托盘编码',
					placeholder: '托盘编码',
					param: 'code',
				},
				{
					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',
				},
			],
		};
	},
	components: {
		AddOrUpdate,
		Drawer,
	},
	created() {
		const queryParams = {
			pageNo: 1,
			pageSize: 99,
			dictType: 'AreaDetStatus',
		};
		listData(queryParams).then((response) => {
			this.formConfig[2].selectOptions = response.data.list;
		});
		getWarehouseList().then((response) => {
			response.data.forEach((item) => {
				if (item.storageType === this.listQuery.storageType) {
					listByWarehouse(item.id).then((response) => {
						this.formConfig[0].selectOptions = response.data;
					});
				}
			});
		});
	},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.locationId = val.name;
					this.listQuery.palletCode = val.code;
					this.listQuery.status = val.status;
					this.listQuery.inTime = val.searchTime ? val.searchTime : null;
					this.getDataList();
					break;
				default:
					console.log(val);
			}
		},
		otherMethods(val) {
			if (val.type === 'out') {
				this.$confirm(
					`确定对${'[名称=' + val.data.name + ']'}执行出库操作?`,
					'提示',
					{
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				)
					.then(() => {
						const data = {
							realtimeLocationId: val.data.id,
							operator: this.nickname,
						};
						this.urlOptions.outURL(data).then(({ data }) => {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getDataList();
								},
							});
						});
					})
					.catch(() => {});
			} else if (val.type === 'in') {
				this.drawerVisible = true;
				this.$nextTick(() => {
					this.$refs.drawerRef.init(val.data, this.nickname);
				});
			} else if (val.type === 'detail') {
				this.addOrUpdateVisible = true;
				this.addOrEditTitle = val.data.name + ' -产品信息';
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init(val.data.id);
				});
			}
		},
		handleCancel() {
			this.addOrUpdateVisible = false;
			this.addOrEditTitle = '';
		},
	},
};
</script>
<style>
.app-container .el-table .el-table__cell {
	padding: 0;
	height: 35px;
}
</style>
