<template>
	<a-card :bordered="false" style="margin-bottom: 10px">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item name="searchKey" label="物资名称">
						<a-input v-model:value="searchFormState.itemName" placeholder="请输入物资名称" />
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-button type="primary" @click="table.refresh(true)">
						<template #icon><SearchOutlined /></template>
						查询
					</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			@resizeColumn="(w, col) => {
				col.width = w
			}"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" v-if="hasPerm('bizHouseAdd')"  @click="formRef.onOpen(undefined, searchFormState)">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete v-if="hasPerm('bizBatchHouseDelete')" :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchCWare" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'returnTime'">
					<span v-if="record.returnTime">{{record.returnTime}}</span>
					<a-tag v-else color="red">未归还</a-tag>
				</template>
				<template v-if="column.dataIndex === 'returnImgUrl'">
					<div v-viewer>
						<img v-if="record.returnImgUrl" class="record-img" :src="record.returnImgUrl" alt="/">
						<span v-else>暂无图片</span>
					</div>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a v-if="!record.returnTime" @click="giveRef.onOpen(record.id)">归还</a>
					<a-divider type="vertical" />
					<a v-if="hasPerm('bizHouseEdit')" @click="formRef.onOpen(record, searchFormState)">编辑</a>
					<a-divider type="vertical" v-if="hasPerm(['bizHouseEdit', 'bizHouseDelete'], 'and')" />
					<a-popconfirm title="确定删除吗？" @confirm="deleteWare(record)">
						<a-button type="link" v-if="hasPerm('bizHouseDelete')" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<Give ref="giveRef" @successful="table.refresh(true)"></Give>
</template>

<script setup name="ware">
	import Form from './form.vue'
	import Give from './give.vue'
	import bizWareApi from "@/api/biz/bizWareApi"
	import { Empty } from 'ant-design-vue'
	import { isEmpty, cloneDeep } from "lodash-es"

	const table = ref()
	const formRef = ref()
	const giveRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const show = ref(true)
	const columns = [
		{
			title: '物资编号',
			dataIndex: 'itemCode',
			fixed: true
		},
		{
			title: '物资名称',
			dataIndex: 'itemName',
			width: 200,
			resizable: true,
			ellipsis: true
		},
		{
			title: '物资类别',
			dataIndex: 'itemTypeName'
		},
		{
			title: '品牌',
			dataIndex: 'brand'
		},
		{
			title: '型号规格',
			dataIndex: 'specification'
		},
		{
			title: '仓库位置',
			dataIndex: 'warehouseName'
		},
		{
			title: '单位',
			dataIndex: 'unit'
		},
		{
			title: '领用数量',
			dataIndex: 'useQuantity'
		},
		{
			title: '领用部门',
			dataIndex: 'departmentName'
		},
		{
			title: '领用人名称',
			dataIndex: 'userName'
		},
		{
			title: '领用时间',
			dataIndex: 'useTime',
			width: 150
		},
		{
			title: '归还时间',
			dataIndex: 'returnTime',
			width: 150,
			sorter: true
		},
		{
			title: '归还图片',
			dataIndex: 'returnImgUrl'
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: 200,
			fixed: 'right'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['basePigpenEdit', 'basePigpenDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	const loadData = async (parameter) => {
		return await bizWareApi.getWarePage(Object.assign(parameter, searchFormState.value)).then(data => {
			return data
		})
	}

	const deleteBatchCWare = (params) => {
		bizWareApi.delWare(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	// 删除
	const deleteWare = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizWareApi.delWare(params).then(() => {
			table.value.refresh(true)
		})
	}

</script>

<style scoped>
.record-img {
	width: 40px;
	height: 40px;
}
</style>
