<template>
	<el-dialog
		:title="isEdit ? '修改库区' : '新增库区'"
		v-model="isShowDialog"
		width="600px"
		:close-on-click-modal="false"
		@close="onCancel"
	>
		<el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="100px">
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="库区名称" prop="zoneName">
						<el-input v-model="ruleForm.zoneName" placeholder="请输入库区名称" clearable />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="库区编码" prop="zoneCode">
						<el-input v-model="ruleForm.zoneCode" placeholder="请输入库区编码" clearable />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="所属仓库" prop="warehouseId">
						<el-input v-model="warehouseDisplayName" placeholder="请选择仓库" readonly style="width: 100%" @click="onSelectWarehouse">
							<template #append>
								<el-button :icon="Search" @click="onSelectWarehouse" />
							</template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="库区类型" prop="zoneType">
						<el-select v-model="ruleForm.zoneType" placeholder="请选择库区类型" clearable style="width: 100%">
							<el-option label="存储区" value="存储区" />
							<el-option label="拣货区" value="拣货区" />
							<el-option label="暂存区" value="暂存区" />
							<el-option label="退货区" value="退货区" />
							<el-option label="发货区" value="发货区" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="备注" prop="remark">
						<el-input v-model="ruleForm.remark" type="textarea" :rows="3" placeholder="请输入备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="onCancel">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="submitLoading">{{ isEdit ? '修 改' : '添 加' }}</el-button>
			</span>
		</template>
	</el-dialog>
	<SelectWarehouseDialog ref="selectWarehouseRef" @confirm="onWarehouseSelected" />
</template>

<script lang="ts">
export default {
	name: 'editZone',
};
</script>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { Search } from '@element-plus/icons-vue';
import type { FormInstance, FormRules } from 'element-plus';
import { createZone, updateZone, getZone } from '/@/api/assets/zone';
import SelectWarehouseDialog from '/@/views/assets/component/selectWarehouseDialog.vue';

// 定义props
defineProps<{
	warehouseOptions: any[];
}>();

// 定义事件
const emit = defineEmits(['getZoneList']);

// 表单ref
const formRef = ref<FormInstance>();

// 弹窗状态
const isShowDialog = ref(false);
const isEdit = ref(false);
const submitLoading = ref(false);

// 仓库选择弹窗ref
const selectWarehouseRef = ref();
// 仓库显示名称
const warehouseDisplayName = ref('');

// 打开仓库选择弹窗
const onSelectWarehouse = () => {
	selectWarehouseRef.value?.openDialog();
};

// 仓库选择确认
const onWarehouseSelected = (row: any) => {
	ruleForm.warehouseId = row.id;
	warehouseDisplayName.value = row.warehouseName;
};

// 表单数据
const ruleForm = reactive({
	id: '',
	zoneName: '',
	zoneCode: '',
	zoneType: '',
	warehouseId: '',
	remark: '',
});

// 表单验证规则
const rules = reactive<FormRules>({
	zoneName: [{ required: true, message: '请输入库区名称', trigger: 'blur' }],
	warehouseId: [{ required: true, message: '请选择所属仓库', trigger: 'change' }],
	zoneCode: [{ required: true, message: '库区编码', trigger: 'blur' }],
	zoneType: [{ required: true, message: '库区类型', trigger: 'change' }],
});

// 重置表单
const resetForm = () => {
	ruleForm.id = '';
	ruleForm.zoneName = '';
	ruleForm.zoneCode = '';
	ruleForm.zoneType = '';
	ruleForm.warehouseId = '';
	ruleForm.remark = '';
	warehouseDisplayName.value = '';
};

// 打开弹窗
const openDialog = async (row?: any) => {
	resetForm();
	isEdit.value = !!row;
	
	if (row) {
		try {
			const res: any = await getZone(row.id);
			const data = res.data;
			ruleForm.id = data.id || '';
			ruleForm.zoneName = data.zoneName || '';
			ruleForm.zoneCode = data.zoneCode || '';
			ruleForm.zoneType = data.zoneType || '';
			ruleForm.warehouseId = data.warehouseId || '';
			warehouseDisplayName.value = data.warehouseName || '';
			ruleForm.remark = data.remark || '';
		} catch (error) {
			console.error('获取库区详情失败:', error);
		}
	}
	
	isShowDialog.value = true;
};

// 取消
const onCancel = () => {
	isShowDialog.value = false;
	formRef.value?.resetFields();
};

// 提交
const onSubmit = async () => {
	const form = formRef.value;
	if (!form) return;
	
	form.validate(async (valid: boolean) => {
		if (valid) {
			submitLoading.value = true;
			try {
				const data = {
					id: ruleForm.id || undefined,
					zoneName: ruleForm.zoneName,
					zoneCode: ruleForm.zoneCode,
					zoneType: ruleForm.zoneType,
					warehouseId: ruleForm.warehouseId,
					remark: ruleForm.remark,
				};
				
				if (isEdit.value) {
					await updateZone(data);
					ElMessage.success('修改成功');
				} else {
					await createZone(data);
					ElMessage.success('添加成功');
				}
				
				isShowDialog.value = false;
				emit('getZoneList');
			} catch (error) {
				console.error('提交失败:', error);
			} finally {
				submitLoading.value = false;
			}
		}
	});
};

// 暴露方法
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
</style>
