	<!-- 购销计划管理 -->
<template>
	<div>
		<a-space class="header">
			<div class="inputBox">
				<span class="title-box">{{ '库区名称' }}</span>
				<a-input class="select-box" style="width: 220px" v-model:value="informationData.orgName" placeholder="请输入" />
			</div>
			<div class="btnBox">
				<a-button style="margin-right: 16px" @click="searchData">查询</a-button>
				<a-button style="margin-right: 16px" @click="reset">重置</a-button>
				<a-button style="margin-right: 16px" type="primary" @click="workOutData">编制</a-button>
				<a-button class="flow" style="margin-right: 16px" @click="batch">批量下发</a-button>
			</div>
		</a-space>
		<a-table :dataSource="dataSource" @change="onChangePage" :row-key="(record) => record.id"
			:row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange, getCheckboxProps: getCheckboxProps, }"
			:pagination="informationData" :columns="columns">
			<template #bodyCell="{ column, record }">
				<template v-if="column.key === 'planType'">
					<span>{{ record.planType === 1 ? '销售计划' : '收购计划' }}</span>
				</template>
				<template v-if="column.key === 'action'">
					<span>
						<a-button class="check" @click="check(record)">查看</a-button>
						<a-button v-if="record.status != 1" class="edit" @click="edit(record)">编辑</a-button>
						<a-popconfirm v-if="record.status != 1" title="确定删除此条数据？" @confirm="deleteData(record)">
							<a-button class="delete">删除</a-button>
						</a-popconfirm>
						<a-popconfirm v-if="record.status != 1" title="确定下发此条数据？" @confirm="distribute(record)">
							<a-button class="flow">下发</a-button>
						</a-popconfirm>
					</span>
				</template>
			</template>
		</a-table>
		<!-- 查看--编辑--新增等操作 -->
		<SalesPlanDialog :salesData="salesData" @isVisible="isVisible" />
	</div>
</template>
<script setup name="reserveScaleManagement">
import reserveQuantityApi from '@/api/reserve.info/reserveQuantityApi.js'
import SalesPlanDialog from './salesNew.vue'
import { message } from 'ant-design-vue'
const columns = [
	{
		title: '序号',
		dataIndex: 'Index',
		align: 'center',
		customRender: ({ text, record, index }) => {
			return `${index + 1}`
		}
	},
	{
		title: '库区名称',
		dataIndex: 'orgName',
		align: 'center',
		key: 'orgName'
	},
	{
		title: '计划单号',
		dataIndex: 'orgName',
		align: 'center',
		key: 'orgName'
	},
	{
		title: '计划名称',
		dataIndex: 'planName',
		align: 'center',
		key: 'planName'
	},
	{
		title: '计划类型',
		dataIndex: 'planType',
		align: 'center',
		key: 'planType'
	},
	{
		title: '计划下达时间',
		dataIndex: 'planReleaseTime',
		align: 'center',
		key: 'planReleaseTime'
	},
	{
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		key: 'action',
		width: '320px'
	}
]
const dataSource = ref([])
const salesData = ref({
	visible: false,
	titleText: '',
	showLock: false,
	isType: '',
	id: ''
})

const informationData = ref({
	orgName: '',
	current: 1,
	size: 10
})
// 表格多选
const selectedRowKey = ref([]) //需要传递给批量下发的数据
const selectedRow = ref([]) // 
const selectedRowKeyList = ref([]) //用于展示列表勾选状态的勾选
const onSelectChange = (selectedRowKeys, selectedRows) => {
	selectedRowKey.value = []
	selectedRowKeys.forEach(item => {
		selectedRowKey.value.push({
			id: item,
			status: 1
		})
	})
	selectedRowKeyList.value = selectedRowKeys
	selectedRow.value = selectedRows
}
// 控制这一行是否可选
const getCheckboxProps = (record) => ({
	disabled: record.status !== 0,  //*** 当 record.status !== '0' 时，这一行就禁选了
})
// 分页操作
const onChangePage = (ev) => {
	informationData.value = { ...ev }
	if (isShowName.value == '1') {
		informationData.value.orgName = ''
	}
	onSearch()
}
// 列表分页
const onSearch = () => {
	const fd = { ...informationData.value }
	reserveQuantityApi.purchaseSalesPage(fd).then((res) => {
		selectedRowKey.value = []
		selectedRow.value = []
		selectedRowKeyList.value = []
		dataSource.value = []
		dataSource.value = res.records
		informationData.value.total = res.total
	})
}
// 查询
const isShowName = ref('1')
const searchData = () => {
	informationData.value.current = 1
	if (informationData.value.total > 10) {
		isShowName.value = '2'
	} else {
		isShowName.value = '1'
	}
	onSearch()
}
// 重置
const reset = () => {
	salesData.value.isType = ''
	informationData.value = {
		orgName: '',
		grainNatureName: '',
		grainVarietyName: '',
		current: 1,
		size: 10
	}
	onSearch()
}
//批量下发
const batch = () => {
	informationData.value.orgName = ''
	if (selectedRowKey.value.length > 0) {
		reserveQuantityApi.purchaseSalesListUpdate(selectedRowKey.value).then((res) => {
			onSearch()
		})
	} else {
		message.warning('请选择需要下发的数据')
	}
}
// 下发
const distribute = (row) => {
	informationData.value.orgName = ''
	reserveQuantityApi.purchaseSalesListUpdate([{ id: row.id, status: 1 }]).then((res) => {
		onSearch()
	})
}
// 删除
const deleteData = (row) => {
	reserveQuantityApi.purchaseSalesDelete([{ id: row.id, planType: row.planType }]).then((res) => {
		onSearch()
	})
}
// 编制
const workOutData = () => {
	salesData.value = {}
	salesData.value.isType = '1'
	salesData.value.titleText = '新增购销计划管理'
	salesData.value.visible = true
	salesData.value.showLock = false
}
// 查看
const check = (row) => {
	// salesData.value.id = row.id
	salesData.value = row
	salesData.value.isType = '3'
	salesData.value.titleText = '查看购销计划管理'
	salesData.value.visible = true
	salesData.value.showLock = true
}
// 编辑
const edit = (row) => {
	// salesData.value.id = row.id
	salesData.value = row
	salesData.value.isType = '2'
	salesData.value.titleText = '编辑购销计划管理'
	salesData.value.visible = true
	salesData.value.showLock = false
}
// 子组件传递
const isVisible = (val) => {
	salesData.value.visible = false
	salesData.value.showLock = false
	salesData.value.titleText = ''
	informationData.value.orgName = ''
	onSearch()
}

onMounted(() => {
	onSearch()
})
</script>
<style scoped>
.header {
	display: flex;
	padding: 20px;
	width: 100%;
}

.inputBox {
	display: flex;
}

.input-box-sle {
	width: 92%;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-right: 24px;
}

.select-box {
	margin-left: 12px;
}

.title-box {
	margin-left: 20px;
	line-height: 30px;
}

.inputBoxContent {
	width: 400px;
}

.silt-title {
	width: 280px;
	display: flex;
	align-items: center;
	margin-right: 32px;
}

.btnBox {
	width: 260px;
	margin-left: 20px;
	justify-content: start;
	display: flex;
}

.ant-input-group-addon {
	border: 0;
}

.ant-input,
.ant-btn {
	border-radius: 5px;
}

.ant-pagination {
	margin-top: 12px;
	float: right;
	margin-right: 16px;
}

.add-btn {
	background-color: rgba(112, 125, 233);
}

.check {
	background-color: rgba(112, 125, 233);
	color: #fff;
	margin-left: 8px;
}

.edit {
	background-color: rgba(222, 186, 80);
	color: #fff;
	margin-left: 8px;
}

.flow {
	background-color: #22ce8d;
	color: #fff;
	margin-left: 8px;
}

.delete {
	background-color: rgba(255, 70, 70);
	color: #fff;
	margin-left: 8px;
}

.audit {
	background-color: rgba(83, 184, 36);
	color: #fff;
	margin-left: 8px;
}

.ant-table-thead>tr>th {
	text-align: center;
}

.ant-table-tbody>tr>td {
	text-align: center;
}

::v-deep .ant-pagination-options {
	display: none !important;
}

::v-deep .ant-table-selection {
	display: none !important;
}
</style>

