<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="officialAccountFormItems"
			: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>
		<user-selector-plus
			ref="userSelectorPlusProRef"
			:org-tree-api="selectorApiFunction.orgTreeApi"
			:user-page-api="selectorApiFunction.userPageApi"
			:checkedUserListApi="selectorApiFunction.userListByIdListApi"
			@onBack="userSelectorOnBack"
			:radioModel="true"
		></user-selector-plus>
	</a-card>
</template>

<script setup name="stashDetail">
	import { required } from '@/utils/formRules'
	import sysStoreApi from '@/api/base/store/sysStoreApi'
	import useFormHandler from '@/hook/useFormHandler'
	import tool from '@/utils/tool'
	import { useRoute } from 'vue-router'
	import UserSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import userCenterApi from '@/api/sys/userCenterApi'
	const route = useRoute()
	const userSelectorPlusProRef = ref(null)

	// 传递设计器需要的API
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return bizOrgApi.orgTreeSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userPageApi: (param) => {
			return bizOrgApi.orgUserSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userListByIdListApi: (param) => {
			return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}

	const handleManageUser = () => {
		userSelectorPlusProRef.value.showUserPlusModal([formData.manageUserId])
	}

	const userSelectorOnBack = (data) => {
		if (data.length > 0) {
			formData.manageUserId = data[0].id
			formData.manageUserName = data[0].name
		} else {
			formData.manageUserId = null
			formData.manageUserName = null
		}
	}

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

	const officialAccountFormItems = [
		{
			label: '编码：',
			name: 'number',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择编码'
			}
		},
		{
			label: '名称：',
			name: 'name',
			type: 'a-input',
			span: 6,
			rules: [required('请输入名称')],
			attrs: {
				placeholder: '请输入名称',
				allowClear: true
			}
		},
		{
			label: '仓库条码：',
			name: 'barcode',
			type: 'a-input',
			span: 6,
			rules: [required('请输入仓库条码')],
			attrs: {
				placeholder: '请输入仓库条码',
				allowClear: true
			}
		},
		{
			label: '可用状态：',
			name: 'enabledState',
			type: 'a-select',
			span: 6,
			attrs: {
				placeholder: '请选择可用状态',
				options: tool.dictList('COMMON_STATUS')
			},
			defaultValue: 'ENABLE'
		},
		{
			label: '库存管理方式：',
			name: 'manageMode',
			type: 'a-select',
			span: 6,
			rules: [required('请选择库存管理方式')],
			attrs: {
				placeholder: '请选择库存管理方式',
				options: tool.dictList('STORE_MANAGE_MODE')
			},
			defaultValue: ''
		},
		{
			label: '备注：',
			name: 'remarks',
			type: 'a-textarea',
			span: 24,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		}
	]

	const baseFormItems = [
		{
			label: '管理员：',
			name: 'manageUserName',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择管理员',
				options: tool.dictList('OFFICIAL_ACCOUNT_TYPE'),
				readonly: true,
				onClick: handleManageUser
			}
		},
		{
			label: '手机：',
			name: 'phone',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入手机'
			}
		},
		{
			label: '固话：',
			name: 'tel',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请选择固话'
			}
		},
		{
			label: '地址：',
			name: 'address',
			type: 'a-textarea',
			span: 24,
			attrs: {
				placeholder: '请输入地址',
				allowClear: true
			}
		}
	]

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

	const { state, formData, submitLoading, formRefs, onSubmit, handleBack, fetchData } = useFormHandler(
		[...officialAccountFormItems, ...baseFormItems],
		{
			submitForm: sysStoreApi.sysStoreSubmitForm,
			getDetail: sysStoreApi.sysStoreDetail
		}
	)

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

	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>
