<template>
	<xn-form-container
		:title="formData.id ? '编辑产品' : '增加产品'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="产品名称：" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入产品名称" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="产品图片：" name="pic">
						<Upload ref="uploadPicRef" :upload-mode="'pic'"
								:upload-number="1" @uploadDone="uploadPicDone"/>
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="排序：" name="sort">
						<a-input-number v-model:value="formData.sort" placeholder="请输入排序"
										style="width: 100%;" :min="1" :max="1000"/>
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="产品标签：" name="tag">
						<a-input v-model:value="formData.tag" placeholder="请输入产品标签" allow-clear/>
					</a-form-item>
				</a-col>

				<a-col :span="18">
					<a-form-item label="产品描述：" name="remark">
						<a-textarea v-model:value="formData.remark" placeholder="请输入产品描述"
									:auto-size="{ minRows: 3, maxRows: 4 }"/>
					</a-form-item>
				</a-col>

				<a-col :span="24">
					<a-button @click="addTab">新增优势</a-button>
					<a-tabs v-model:activeKey="activeIndex" hide-add type="editable-card"
							@edit="deleteTab" style="margin-top: 15px">
						<a-tab-pane v-for="(item, index) in formData.extra.advantages"
									:key="'advantage' + (index + 1)" :tab="'优势' + (index + 1)" closable>
							<a-row>
								<a-col :span="12">
									<a-form-item label="名称：" :name="['extra', 'advantages', index, 'name']">
										<a-input v-model:value="item.name" autocomplete="off"
												 show-count :maxlength="20" placeholder="请输入名称"/>
									</a-form-item>
								</a-col>
							</a-row>
						</a-tab-pane>
					</a-tabs>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="productForm">

import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import productApi from '@/api/website/productApi'
import Upload from "@/components/XnUpload/index.vue";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
const uploadPicRef = ref()
// 表单数据
const formData = ref({
	extra: {
		advantages: []
	}
})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({
			extra: {
				advantages: []
			}
		}, recordData)
		// 处理图片
		initFiles(record)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {
		extra: {
			advantages: []
		}
	}
	visible.value = false
	// 设置tab选中
	activeIndex.value = "advantage1"
}
// 默认要校验的
const formRules = {
	name: [required('请输入产品名称')],
	pic: [required("请上传产品图片")],
	sort: [required("请输入排序")],
	remark: [required("请输入产品描述")],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		productApi.save(formDataParam, formDataParam.id)
			.then(() => {
				onClose()
				emit('successful')
			})
			.finally(() => {
				submitLoading.value = false
			})
	})
}
// 图片上传
const uploadPicDone = (data) => {
	if (data.length > 0) {
		formData.value.pic = data[0].url
	} else {
		formData.value.pic = null
	}
}
// 处理图片
const initFiles = (record) => {
	let pics = []
	if (record.pic) {
		pics.push({url: record.pic})
	}
	nextTick(() => {
		uploadPicRef.value.setFileList(pics)
	})
}

// tab相关
const activeIndex = ref('advantage1')
// 添加tab页
const addTab = () => {
	let items = formData.value.extra.advantages

	let singleNode = {
		advantage: ''
	}
	items.push(singleNode)
	formData.value.extra.advantages = [...items]
	activeIndex.value = 'advantage' + items.length
}

// 删除tab页
const deleteTab = (targetName) => {
	let removeIndex
	let items = formData.value.extra.advantages
	items.forEach((node, index) => {
		if ('advantage' + (index + 1) === targetName) {
			removeIndex = index
		}
	})
	// 移除数据
	items.splice(removeIndex, 1)
	// 设置tab选中
	activeIndex.value = "advantage1"
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
