﻿<template>
	<div class="invoice-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="单号" prop="codeNum">
							<el-input v-model="ruleForm.codeNum" placeholder="请输入单号" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="业务日期" prop="startDate">
							<el-date-picker v-model="ruleForm.startDate" type="date" placeholder="业务日期" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="业务类型" prop="businessType">
						<el-select v-model="ruleForm.businessType" placeholder="请选择" clearable>
                            <el-option label="销售出库" value="销售出库" />
                            <el-option label="其他出库" value="其他出库" />
                        </el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="客户" prop="custom" :rules="[{ required: true, message: '客户不能为空', trigger: 'blur' }]">
                            <el-select v-model="ruleForm.custom" placeholder="请选择" clearable >
                                <el-option :label="item.name" :value="item.name" v-for="item, index in customs"
                                    :key="index" />
                            </el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="仓库" prop="warehouse" :rules="[{ required: true, message: '仓库不能为空', trigger: 'blur' }]">
                            <el-select v-model="ruleForm.warehouse" placeholder="请选择" clearable >
                                <el-option :label="item.name" :value="item.name" v-for="item, index in warehouses"
                                    :key="index" />
                            </el-select>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门" prop="department">
							<el-input v-model="ruleForm.department" placeholder="请输入部门" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="车牌号" prop="carNumber">
							<el-input v-model="ruleForm.carNumber" placeholder="请输入车牌号" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="停车位" prop="parkingSpace">
							<el-input v-model="ruleForm.parkingSpace" placeholder="请输入停车位" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="业务员" prop="salesman">
							<el-input v-model="ruleForm.salesman" placeholder="请输入业务员" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="收货单位" prop="consignee">
							<el-input v-model="ruleForm.consignee" placeholder="请输入收货单位" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="交货日期" prop="deliveryDate">
							<el-date-picker v-model="ruleForm.deliveryDate" type="date" placeholder="交货日期" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="交货地址" prop="deliveryAddress">
							<el-input v-model="ruleForm.deliveryAddress" placeholder="请输入交货地址" maxlength="128" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注" prop="remarks">
							<el-input v-model="ruleForm.remarks" placeholder="请输入备注" maxlength="128" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="方案客户拓展" prop="customExpand">
							<el-input v-model="ruleForm.customExpand" placeholder="请输入方案客户拓展" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="发货拓展" prop="sendOutExpand">
							<el-input v-model="ruleForm.sendOutExpand" placeholder="请输入发货拓展" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addInvoice, updateInvoice, detailInvoice } from "/@/api/main/invoice";
	import { listWarehouse } from '/@/api/main/warehouse';
	import { listSupplier } from '/@/api/main/supplier';
	import { listCustom } from '/@/api/main/custom';

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	const warehouses = ref<any>([]);
	const suppliers = ref<any>([]);
	const customs = ref<any>([]);

	//自行添加其他规则
	const rules = ref<FormRules>({
		businessType: [{required: true, message: '请输入业务类型！', trigger: 'blur',},],
		custom: [{required: true, message: '请输入客户！', trigger: 'blur',},],
		warehouse: [{required: true, message: '请输入仓库！', trigger: 'blur',},],
	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			ruleForm.value = (await detailInvoice(rowData.id)).data.result;
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addInvoice(values);
				} else {
					await updateInvoice(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};







	// 页面加载时
	onMounted(async () => {
		warehouses.value = (await listWarehouse()).data.result;
		suppliers.value = (await listSupplier()).data.result;
		customs.value = (await listCustom()).data.result;
	});

	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




