<template>
	<a-card :bordered="false" style="margin-bottom: 10px" class="mb-2">
		<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="saleOrderNumber">
						<a-input v-model:value="searchFormState.saleOrderNumber" placeholder="订单编号查询" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="订单名称" name="saleOrderName">
						<a-input v-model:value="searchFormState.saleOrderName" placeholder="订单名称查询" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="订单类型" name="saleOrderType">
						<a-select v-model:value="searchFormState.saleOrderType" placeholder="订单类型查询"
								  :options="orderTypeList"
						> </a-select>
					</a-form-item>
				</a-col>

				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="货品名称" name="saleGoodsName">
							<a-input v-model:value="searchFormState.saleGoodsName" placeholder="货品名称查询" />
						</a-form-item>
					</a-col>
				</template>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizSaleOrderAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizSaleOrderEdit') && record.saleStatus == '1'">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizSaleOrderEdit', 'bizSaleOrderDelete'], 'and') && record.saleStatus == '1'" />
						<a-button style="color:red" type="link" danger size="small" v-if="hasPerm('bizSaleOrderDelete')" @click="deleteConfig(record)">删除</a-button>
					</a-space>
				</template>
				<template v-if="column.dataIndex === 'saleOrderType'">
					<a-tag
						:color="
							record.saleOrderType === '1'
								? 'orange'
								: record.saleOrderType === '2'
								  ? 'green'
								  : 'purple'
						"
					>
						{{ $TOOL.dictTypeData('order_type', record.saleOrderType) }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'saleStatus'">
					<a-tag
						:color="
							record.saleStatus === '1'
								? 'processing'
								: record.saleStatus === '2'
								  ? 'volcano'
								  : 'red'
						"
					>
						{{ $TOOL.dictTypeData('sale_order_status', record.saleStatus) }}
					</a-tag>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizsaleorder">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizSaleOrderApi from '@/api/biz/bizSaleOrderApi'
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';
	import tool from '@/utils/tool'

	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const submitLoading = ref(false)

	//查询数据
	const searchFormState = ref({})
	const searchFormRef = ref()
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const orderTypeList = tool.dictList('order_type')

	const columns = [
		{
			title: '订单编号',
			dataIndex: 'saleOrderNumber',
			align:'center'
		},
		{
			title: '订单名称',
			dataIndex: 'saleOrderName',
			align:'center'
		},
		{
			title: '订单类型',
			dataIndex: 'saleOrderType',
			align:'center'
		},
		{
			title: '客户名称',
			dataIndex: 'customerName',
			align:'center'
		},
		{
			title: '货品名称',
			dataIndex: 'saleGoodsName',
			align:'center'
		},
		{
			title: '订单重量(吨)',
			dataIndex: 'saleOrderWeight',
			align:'center'
		},
		{
			title: '状态',
			dataIndex: 'saleStatus',
			align:'center'
		},
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return bizSaleOrderApi.bizSaleOrderPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizSaleOrder = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizSaleOrderApi.bizSaleOrderDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 删除
	const deleteConfig = (record) => {

		Modal.confirm({
			title: '确定删除该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params = [
					{
						id: record.id
					}
				]

				bizSaleOrderApi
					.bizSaleOrderDelete(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}
	// 批量删除
	const deleteBatchBizSaleOrder = (params) => {
		bizSaleOrderApi.bizSaleOrderDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
