<template>
	<!-- 添加或修改项目信息对话框 -->
	<el-dialog title="载具信息表单" v-model="visible" width="400px" append-to-body draggable>
		<div class="form-container">
			<el-form ref="carrierFormRef" class="master-container" :rules="rules" v-loading="loading" :model="form" label-width="80px">
				<!--        <el-form-item label="载具编号" prop="code" required>-->
				<!--          <el-input v-model.trim="form.code" placeholder="请输入载具编号" />-->
				<!--        </el-form-item>-->
				<el-form-item label="父级类别" prop="parentId">
					<el-tree-select v-model="form.categoryId" :data="parentCategories" :render-after-expand="false" />
				</el-form-item>
				<!--        <el-form-item label="是否多批" prop="status">-->
				<!--          <el-select v-model="form.isAllowMore">-->
				<!--            <el-option v-for="item in is_allow_more" :key="item.value" :label="item.label"-->
				<!--                       :value="item.value"></el-option>-->
				<!--          </el-select>-->
				<!--        </el-form-item>-->
				<el-form-item label="备注" prop="remark">
					<el-input v-model.trim="form.remark" placeholder="请输入备注" />
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<el-button type="primary" icon="Check" @click="handleSave">确 定</el-button>
			<el-button icon="Close" @click="handleCancel">取 消</el-button>
		</template>
	</el-dialog>
</template>
<script setup>
import { getCarrier, getCategory, updateCarrier } from '@/api/business/carrier'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['handleSaveSuccess'])
/** 字典数组区 */
const { is_allow_more } = proxy.useDict('is_allow_more')
/** 表单抽屉 页变量 */
const parentCategories = ref([])
const loading = ref(false)
const visible = ref(false)
const form = ref({})
const categoryFrom = ref({})
const rules = {
	code: [{ required: true, message: '载具编号不能为空', trigger: 'blur' }],
}

/***********************  方法区  ****************************/
/** 打开对话框 */
const open = (data) => {
	reset()
	visible.value = true
	loading.value = true
	form.value.categoryId = data.categoryId
	getCarrier(data.id || 0).then((res) => {
		if (res.data) {
			form.value = res.data
			form.value.isAllowMore = form.value.isAllowMore + ''
		}
		loading.value = false
	})
}

/** 表单重置 */
const reset = () => {
	form.value = {
		id: null,
		categoryId: 0,
		code: '',
		remark: '',
	}
	proxy.resetForm('carrierFormRef')
}

getCategory(0).then((res) => {
	if (res.data.formData) {
		categoryFrom.value = res.data.formData
	}
	parentCategories.value = res.data.parentOptions
	loading.value = false
})

/** 提交按钮 */
const handleSave = () => {
	proxy.$refs['carrierFormRef'].validate((valid) => {
		if (valid) {
			updateCarrier(form.value).then((res) => {
				if (res.code === 601) {
					proxy.$modal.msgWarn(res.msg)
					return
				}
				if (res.code === 200) {
					proxy.$modal.msgSuccess('操作成功')
					emit('handleSaveSuccess')
				}
				visible.value = false
			})
		}
	})
}

/** 取消按钮 */
const handleCancel = () => {
	visible.value = false
	reset()
}

/** 暴露给父组件的方法 */
defineExpose({
	open,
})
</script>
