<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="名称" name="name">
						<a-input v-model:value="searchFormState.name" placeholder="请输入名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="编码" name="number">
						<a-input v-model:value="searchFormState.number" placeholder="请输入编码" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="可用状态" name="enabledState">
						<a-select
							v-model:value="searchFormState.enabledState"
							placeholder="请选择可用状态"
							:options="$TOOL.dictList('COMMON_STATUS')"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>

	<a-card :bordered="false" class="mt-4" style="height: 100%">
		<a-row :gutter="24">
			<a-col :span="6">
				<dynamic-tree
					ref="dynamicTreeRef"
					treeTitle="物料分类"
					:tableRef="tableRef"
					:openFormRef="materialCategoryFormRef"
					:apiModel="{
						getTree: materialCategoryApi.materialCategoryTree,
						delTree: materialCategoryApi.materialCategoryDelete
					}"
					@selectTree="selectTree"
					@delTree="delTree"
				></dynamic-tree>
			</a-col>
			<a-col :span="18">
				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:tool-config="options.toolConfig"
					:row-selection="options.rowSelection"
					:scroll="{
						x: 100,
						y: 'calc(100vh - 300px)'
					}"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button
								type="primary"
								@click="
									navigateTo('/basicData/materiel/detail', {
										type: 'ADD'
									})
								"
								v-if="hasPerm('customerAdd')"
							>
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete
								v-if="hasPerm('customerBatchDelete')"
								:selectedRowKeys="selectedRowKeys"
								@batchDelete="deleteBatchRecords"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'number'">
							<a href="#">{{ record.number }}</a>
						</template>
						<template v-if="column.dataIndex === 'enabledState'">
							<a-tag color="#87d068" v-if="record.enabledState === 'ENABLE'">启用</a-tag>
							<a-tag color="#f50" v-if="record.enabledState === 'DISABLED'">停用</a-tag>
						</template>
						<template v-if="column.dataIndex === 'type'">
							{{ $TOOL.dictTypeData('OFFICIAL_ACCOUNT_TYPE', record.type) }}
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a-tooltip title="查看">
									<a
										@click="
											navigateTo('/basicData/materiel/detail', {
												type: 'SEARCH',
												id: record.id
											})
										"
										v-if="hasPerm('customerEdit')"
									>
										<EyeOutlined />
										<!--									查看-->
									</a>
								</a-tooltip>

								<a-divider type="vertical" v-if="hasPerm(['customerEdit', 'customerDelete'], 'and')" />
								<a-tooltip title="查看">
									<a
										@click="
											navigateTo('/basicData/materiel/detail', {
												type: 'EDIT',
												id: record.id
											})
										"
										v-if="hasPerm('customerEdit')"
									>
										<FormOutlined />
										<!--									编辑-->
									</a>
								</a-tooltip>

								<a-divider type="vertical" v-if="hasPerm(['customerEdit', 'customerDelete'], 'and')" />
								<a-popconfirm title="确定要删除吗？" @confirm="deleteRecord(record)">
									<a-button type="link" danger size="small" v-if="hasPerm('customerDelete')">
										<DeleteOutlined />
										<!--										删除-->
									</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-col>
		</a-row>
	</a-card>
	<material-category-form ref="materialCategoryFormRef" @successful="successful"></material-category-form>
</template>

<script setup name="materiel">
	import materialApi from '@/api/base/material/materialApi'
	import materialCategoryApi from '@/api/base/material/materialCategoryApi'
	import MaterialCategoryForm from '@/views/basicData/materiel/detail/materialCategoryForm.vue'
	import { useTableManagement } from '@/hook/useTableManagement'
	import { materielColumn } from '@/views/basicData/materiel/column/materiel-column'

	const materialCategoryFormRef = ref(null)
	const dynamicTreeRef = ref(null)

	const {
		searchFormState,
		tableRef,
		selectedRowKeys,
		columns,
		loadData,
		reset,
		deleteRecord,
		deleteBatchRecords,
		options,
		searchFormRef,
		navigateTo
	} = useTableManagement(
		{
			page: materialApi.materialPage,
			delete: materialApi.materialDelete
		},
		materielColumn,
		['customerEdit', 'customerDelete']
	)

	const selectTree = (value) => {
		searchFormState.value.categoryId = value.id
		tableRef.value.refresh()
	}

	const delTree = () => {
		searchFormState.value.categoryId = null
		tableRef.value.refresh()
	}

	const successful = () => {
		searchFormState.value.categoryId = null
		tableRef.value.refresh()

		dynamicTreeRef.value.loadTreeData()
	}

	onMounted(() => {
		dynamicTreeRef.value.loadTreeData()
	})
</script>
