<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">
		<a-row :gutter="16">
			<a-col :span="6">
				<div className="s-table-tool">
					<div className="s-table-tool-left">分类</div>
					<!--  斑马纹 -->
					<div className="layout-items-center s-table-tool-right">
						<span v-for="item in tool" :key="item.name">
							<!-- 新增 -->
							<a-tooltip :title="item.title" class="s-tool-item" v-if="item.name === 'plus'" @click="handleAddTree">
								<component class="icons" :is="item.icon"></component>
							</a-tooltip>
							<!-- 修改 -->
							<a-tooltip :title="item.title" class="s-tool-item" v-if="item.name === 'edit'" @click="handleEditTree">
								<component class="icons" :is="item.icon"></component>
							</a-tooltip>
							<!-- 删除 -->
							<a-tooltip :title="item.title" class="s-tool-item" v-if="item.name === 'delete'">
								<a-popconfirm title="确定要删除吗？" ok-text="确认" cancel-text="取消" @confirm="handleDelTree">
									<component class="icons" :is="item.icon"></component>
								</a-popconfirm>
							</a-tooltip>
							<!-- 刷新 -->
							<!--							<a-tooltip :title="item.title" class="s-tool-item" v-if="item.name === 'refresh'">
								<component class="icons" :is="item.icon"></component>
							</a-tooltip>-->
						</span>
					</div>
				</div>
				<a-directory-tree
					v-model:expandedKeys="expandedKeys"
					v-model:selectedKeys="selectedKeys"
					multiple
					:fieldNames="{
						children: 'children',
						title: 'name',
						key: 'id'
					}"
					:tree-data="treeData"
					@select="handleTreeClick"
				></a-directory-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="toolConfig"
					:row-selection="options.rowSelection"
				>
					<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 === 'enabledState'">
							<a-switch
								checkedValue="ENABLE"
								unCheckedValue="DISABLED"
								checked-children="启用"
								un-checked-children="停用"
								v-model:checked="record.enabledState"
							/>
						</template>
						<template v-if="column.dataIndex === 'type'">
							{{ $TOOL.dictTypeData('OFFICIAL_ACCOUNT_TYPE', record.type) }}
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a
									@click="
										navigateTo('/basicData/materiel/detail', {
											type: 'SEARCH',
											id: record.id
										})
									"
									v-if="hasPerm('customerEdit')"
									>查看</a
								>
								<a-divider type="vertical" v-if="hasPerm(['customerEdit', 'customerDelete'], 'and')" />
								<a
									@click="
										navigateTo('/basicData/materiel/detail', {
											type: 'EDIT',
											id: record.id
										})
									"
									v-if="hasPerm('customerEdit')"
									>编辑</a
								>
								<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')">删除</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-col>
		</a-row>
	</a-card>
	<materialCategoryForm ref="materialCategoryFormRef" @successful="successful"></materialCategoryForm>
</template>

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

	const materielColumn = [
		{
			title: '编码',
			dataIndex: 'number',
			sorter: (a, b) => a.address.length - b.address.length,
			sortDirections: ['descend', 'ascend'],
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '名称',
			dataIndex: 'name',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '规格型号',
			dataIndex: 'specification',
			align: 'center',
			resizable: true,
			width: 100
		},
		// {
		// 	title: '分类',
		// 	dataIndex: 'name',
		// 	align: 'center',
		// 	resizable: true,
		// 	width: 100
		// },
		{
			title: '品牌',
			dataIndex: 'brandName',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '保质期',
			dataIndex: 'shelfLifeUnit',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '基本单位',
			dataIndex: 'baseUnitName',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '生产单位',
			dataIndex: 'produceUnitName',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '采购单位',
			dataIndex: 'purchaseUnitName',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '可用状态',
			dataIndex: 'enabledState',
			align: 'center',
			resizable: true,
			width: 100
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			sorter: (a, b) => a.address.length - b.address.length,
			sortDirections: ['descend', 'ascend'],
			align: 'center',
			resizable: true,
			width: 100
		}
	]

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

	const tool = [
		{
			name: 'plus',
			icon: 'plus-outlined',
			title: '新增'
		},
		{
			name: 'edit',
			icon: 'edit-outlined',
			title: '编辑'
		},
		{
			name: 'delete',
			icon: 'delete-outlined',
			title: '删除'
		},
		{
			name: 'refresh',
			icon: 'sync-outlined',
			title: '刷新'
		}
	]

	// 树结构
	const expandedKeys = ref(['0-0', '0-1'])
	const selectedKeys = ref([])
	const treeData = ref([])
	const materialCategoryFormRef = ref(null)
	let treeRow = {}

	const handleTreeClick = (selectedKeys, event) => {
		treeRow = event.node
		searchFormState.value.categoryId = selectedKeys[0]
		tableRef.value.refresh()
	}

	const handleAddTree = () => {
		materialCategoryFormRef.value.onOpen()
	}

	const handleEditTree = () => {
		if (!treeRow.id) return message.error('!请选择要编辑的数据')
		materialCategoryFormRef.value.onOpen(treeRow)
	}

	const handleDelTree = () => {
		if (!treeRow.id) return message.error('!请选择要删除的数据')
		materialCategoryApi.materialCategoryDelete([{ id: treeRow.id }]).then((res) => {
			selectedKeys.value = []
			searchFormState.value.categoryId = null
			treeRow = {}
			tableRef.value.refresh()

			materialCategoryApi.materialCategoryTree().then((res) => {
				treeData.value = res || []
			})
		})
	}

	const successful = () => {
		selectedKeys.value = []
		searchFormState.value.categoryId = null
		treeRow = {}
		tableRef.value.refresh()

		materialCategoryApi.materialCategoryTree().then((res) => {
			treeData.value = res
		})
	}

	onMounted(() => {
		materialCategoryApi.materialCategoryTree().then((res) => {
			treeData.value = res || []
		})
	})
</script>

<style lang="less" scoped>
	.s-table-tool {
		display: flex;
		margin-bottom: 16px;
		.s-table-tool-left {
			flex: 1;
		}
		.s-table-tool-right {
			.s-tool-item {
				font-size: 16px;
				@apply ml-4;
				cursor: pointer;
			}
		}
	}
</style>
