<template>
	<page-container>
		<!-- 页面操作栏 -->
		<template #ops>
			<a-row>
				<a-col :span="18">
					<a-input-search
						v-model:value="searchKey"
						:placeholder="`请输入`"
						allow-clear
						enter-button
						@search="loadData()"
					></a-input-search>
				</a-col>
			</a-row>
		</template>
		<!-- 页面表格内容 -->
		<div style="min-height: calc(100vh - 305px)">
			<!-- 表格行 -->
			<a-table
				:columns="columns"
				:data-source="pagedata?.records"
				bordered
				:pagination="pagination"
				:loading="loading"
				row-key="key"
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'applyTypeInfo'">
						{{ record.applyTypeInfo.description }}
					</template>
					<template v-if="column.dataIndex === 'operation'">
						<a-button v-if="record.applyType === 'SCRAP_OUT'" type="link" style="margin-left: 10px">审核</a-button>
					</template>
				</template>
			</a-table>
		</div>
	</page-container>
</template>

<script setup lang="ts">
	import api from '@/api'
	import { IPage } from '@/api/api'

	const searchKey = ref('')
	const pagedata = ref<IPage<material.ApplyDTO>>()
	const loading = ref(false)

	// 加载数据的方法
	const loadData = async (page = 1, size = 10) => {
		loading.value = true
		api.materialApi.apply.searchPage(
			{
				page: page,
				size: size,
				applyType: 3,
				type: searchKey.value,
				code: searchKey.value,
				name: searchKey.value,
			},
			(data) => {
				pagedata.value = data
				loading.value = false
			},
		)
	}
	//初始加载
	loadData()

	//表格列
	const columns = [
		{
			title: '序号',
			dataIndex: 'key',
			customRender: ({ index }: { index: number }) => `${index + 1}`,
		},
		{
			title: '物料名称',
			dataIndex: 'name',
		},
		{
			title: '物料类型',
			dataIndex: 'typeName',
		},
		{
			title: '物料编码',
			dataIndex: 'code',
		},
		{
			title: '出库类型',
			dataIndex: 'applyTypeInfo',
		},

		{
			title: '申请数量',
			dataIndex: 'applyNum',
		},
		{
			title: '确认数量',
			dataIndex: 'confirmNum',
		},
		{
			title: '申请人',
			dataIndex: 'applicant',
		},
		{
			title: '申请日期',
			dataIndex: 'applyDate',
		},
		{
			title: '备注',
			dataIndex: 'remark',
		},
		{
			title: '操作',
			dataIndex: 'operation',
		},
	]

	//分页信息
	const pagination = computed(() => {
		return {
			current: pagedata.value?.current,
			pageSize: pagedata.value?.size,
			total: pagedata.value?.total,
			onChange: (page: number, pageSize: number) => {
				loadData(page, pageSize)
			},
		}
	})
</script>
