<template>
	<a-page-header style="padding: 10px; font-size: 20px" @back="handleBack">
		<template #extra>
			<a-button v-if="route.query.type !== 'SEARCH'" key="1" type="primary" @click="onSubmit">保存</a-button>
		</template>
	</a-page-header>

	<a-card :bordered="false" title="客户">
		<DynamicForm
			:allDisabled="route.query.type === 'SEARCH'"
			:formItems="clientFormItems"
			:model="formData"
			:rules="formRules"
			ref="formRef1"
		/>
	</a-card>

	<a-card :bordered="false" class="mt-4">
		<a-tabs v-model:activeKey="activeKey">
			<a-tab-pane key="1" tab="联系信息">
				<DynamicForm
					:allDisabled="route.query.type === 'SEARCH'"
					:formItems="baseFormItems"
					:model="formData"
					:rules="formRules"
					ref="formRef2"
				/>
			</a-tab-pane>
			<a-tab-pane key="2" tab="销售区域管理" force-render>
				<DynamicTable :initialData="data" :columns="columns" rowKey="id"></DynamicTable>
			</a-tab-pane>
		</a-tabs>
	</a-card>
</template>

<script setup name="clientDetail">
	import { required } from '@/utils/formRules'
	import sysBrandApi from '@/api/base/brand/sysBrandApi'
	import customerCategoryApi from '@/api/base/customer/customerCategoryApi'
	import customerApi from '@/api/base/customer/customerApi'

	import useFormHandler from '@/hook/useFormHandler'
	import tool from '@/utils/tool'
	import { useRoute } from 'vue-router'
	const route = useRoute()
	import DynamicTable from '@/components/DynamicTable/index.vue'
	import cityOptions from '@/utils/cityOptions'

	const formRules = {
		name: [required('请输入名称')],
		type: [required('请输入类型')],
		appid: [required('请输入AppID')],
		secret: [required('请输入AppSecret')]
	}

	// 级联选择
	const onCascaderChange = (value) => {
		formData.province = value[0] && value[0]
		formData.city = value[1] && value[1]
		formData.county = value[2] && value[2]
	}

	const clientFormItems = reactive([
		{
			label: '编码：',
			name: 'number',
			type: 'a-input',
			span: 8,
			attrs: {
				placeholder: '请选择类型',
				options: tool.dictList('OFFICIAL_ACCOUNT_TYPE')
			}
		},
		{
			label: '名称：',
			name: 'name',
			type: 'a-input',
			span: 8,
			rules: [required('请输入名称')],
			attrs: {
				placeholder: '请输入名称',
				allowClear: true
			}
		},
		{
			label: '简称：',
			name: 'shortName',
			type: 'a-input',
			span: 8,
			attrs: {
				placeholder: '请输入名称',
				allowClear: true
			}
		},
		{
			label: '可用状态：',
			name: 'enabledState',
			type: 'a-select',
			span: 8,
			attrs: {
				placeholder: '请选择可用状态',
				options: tool.dictList('COMMON_STATUS')
			},
			defaultValue: 'ENABLE'
		},
		{
			label: '品牌：',
			name: 'brandId',
			type: 'a-select',
			span: 8,
			rules: [required('请选择品牌')],
			attrs: {
				placeholder: '请选择品牌',
				options: tool.dictList('COMMON_STATUS'),
				fieldNames: {
					label: 'name',
					value: 'id'
				}
			}
		},
		{
			label: '客户分类：',
			name: 'categoryId',
			type: 'a-tree-select',
			span: 8,
			rules: [required('请选择客户分类')],
			attrs: {
				placeholder: '请选择客户分类',
				allowClear: true,
				fieldNames: {
					children: 'children',
					label: 'name',
					value: 'id'
				}
			}
		},
		{
			label: '详细地址：',
			name: 'provinceName',
			type: 'a-cascader',
			span: 8,
			rules: [required('请选择详细地址')],
			attrs: {
				placeholder: '请选择详细地址',
				options: cityOptions,
				onChange: onCascaderChange
			}
		},
		{
			name: 'address',
			type: 'a-textarea',
			span: 24,
			attrs: {
				placeholder: '请输入详细地址',
				allowClear: true,
				fieldNames: {
					children: 'children',
					label: 'name',
					value: 'id'
				}
			}
		},
		{
			label: '备注：',
			name: 'remarks',
			type: 'a-textarea',
			span: 24,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		}
	])

	const baseFormItems = [
		{
			label: '联系人：',
			name: 'contacts',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择类型'
			}
		},
		{
			label: '手机：',
			name: 'phone',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择类型'
			}
		},
		{
			label: '固话：',
			name: 'tel',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择类型'
			}
		},
		{
			label: '传真：',
			name: 'fax',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择类型'
			}
		},
		{
			label: '电子邮箱：',
			name: 'email',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		},
		{
			label: 'QQ：',
			name: 'qq',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		},
		{
			label: '微信：',
			name: 'wechat',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		},
		{
			label: '联系地址：',
			name: 'contactAddress',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		}
	]

	const formRef1 = ref(null)
	const formRef2 = ref(null)

	const { state, formData, submitLoading, formRefs, onSubmit, handleBack, fetchData } = useFormHandler(
		[...clientFormItems, ...baseFormItems],
		{
			submitForm: customerApi.customerSubmitForm,
			getDetail: customerApi.customerDetail
		}
	)

	onMounted(async () => {
		formRefs.value = [formRef1.value, formRef2.value]
		fetchData(route.query.type).then((res) => {
			if (res) {
				formData.provinceName = [res.province, res.city, res.county]
			}
		})

		// 选择品牌
		sysBrandApi
			.sysBrandList({
				enabledState: 'ENABLE'
			})
			.then((res) => {
				clientFormItems.forEach((item) => {
					if (item.name === 'brandId') {
						item.attrs.options = res
					}
				})
			})

		// 客户分类

		customerCategoryApi
			.customerCategoryTree({
				enabledState: 'ENABLE'
			})
			.then((res) => {
				clientFormItems.forEach((item) => {
					if (item.name === 'categoryId') {
						item.attrs.treeData = res
					}
				})
			})
	})

	let activeKey = ref('1')

	const data = ref([
		{ id: '1', name: 'John', age: 28 },
		{ id: '2', name: 'Jane', age: 22 }
	])

	const columns = [
		{
			title: '上游仓库代码',
			dataIndex: 'name',
			editable: true,
			dataType: 'text' // 或 'number', 'select'
		},
		{
			title: '上游仓库名称',
			dataIndex: 'age',
			editable: true,
			dataType: 'number'
		}
	]
</script>
