<!-- 数据量 -->
<template>
	<div class="pricemoitorrecord">
		<a-card :bordered="false">
			<a-space class="search">
				<div class="silt-title">
					<span style="width: 70px; margin-right: 8px; text-align: right; display: block; line-height: 30px">{{
						'企业名称'
					}}</span>
					<a-input style="width: 220px" v-model:value="SearchFormDel.unitName" placeholder="请输入" />
				</div>
				<div class="btnBox">
					<a-button type="primary" class="primarySele" @click="Search"> 查询 </a-button>
					<a-button class="primarySele snowy-buttom-left" @click="onReset"> 重置 </a-button>
					<!-- <a-button type="primary" class="primarySele" @click="onView('add', {})"> 新增 </a-button> -->
					<a-button class="btnl colro" type="primary" :disabled="!hasSelected" :loading="state.loading" @click="start">
						批量共享
					</a-button>
				</div>
			</a-space>
		</a-card>

		<div class="list" style="margin-top: 10px">
			<a-table
				:data-source="tableData"
				:pagination="pagination"
				:columns="tableColumns"
				:row-key="(record) => record"
				:row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }"
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex == 'creditLevel'">
						<div v-if="record.creditLevel == '1'">一级</div>
						<div v-if="record.creditLevel == '2'">二级</div>
						<div v-if="record.creditLevel == '3'">三级</div>
					</template>
					<template v-if="column.dataIndex === 'assessResult'">
						<div v-if="record.assessResult == '1'">优秀</div>
						<div v-if="record.assessResult == '2'">良好</div>
						<div v-if="record.assessResult == '3'">一般</div>
					</template>

					<template v-if="column.dataIndex === 'isOpen'">
						<!-- <div :style="{ height: '50px' }"> -->
						<a-switch
							:checked="record.isOpen == '0' ? false : true"
							:checked-children="'是'"
							:un-checked-children="'否'"
							@change="handleSwitchChangeDel(record)"
						/>
						<!-- </div> -->
					</template>

					<template v-if="column.dataIndex === 'isShare'">
						<div class="switch_del">
							<a-switch
								:checked="record.isShare == '0' ? false : true"
								:checked-children="'是'"
								:un-checked-children="'否'"
								@change="handleSwitchChange(record)"
								size="default"
							/>
						</div>
					</template>
					<template v-if="column.key === '操作'">
						<a-button type="link" @click="onView('view', record)">查看</a-button>
						<!-- <a-button type="link" @click="onView('edit', record)">编辑</a-button>
						<a-popconfirm title="确定要删除吗？" @confirm="onView('del', record)">
							<a-button type="link">删除</a-button>
						</a-popconfirm> -->
					</template>
				</template>
			</a-table>
		</div>
	</div>
</template>
<script setup name="sharing">
	import business from '@/api/basic.info/verifyQuery.js'
	import { listPagination } from '@/hook/listlistPagination.js'
	import { reactive, ref } from 'vue'
	import { useRouter } from 'vue-router'
	import { defineComponent } from 'vue'
	import { message } from 'ant-design-vue'
	const formRef = ref(null)
	const router = useRouter()

	const tableColumns = [
		// { title: '序号', key: 'INDEX' },
		{
			title: '序号',
			dataIndex: 'INDEX',
			align: 'center',
			customRender: ({ text, record, index }) => {
				return `${index + 1}`
			}
		},
		{ title: '企业名称', align: 'center', dataIndex: 'unitName' },
		{ title: '信用等级', align: 'center', dataIndex: 'creditLevel' },
		{ title: '信用评价', align: 'center', dataIndex: 'assessResult' },
		{ title: '评价结果', align: 'center', dataIndex: 'assessResultDepict' },
		{ title: '是否公开评价结果', align: 'center', dataIndex: 'isOpen' },
		{ title: '是否共享', align: 'center', dataIndex: 'isShare' }
		// { title: '操作', align: 'center', key: '操作' }
	]

	const handleSwitchChangeDel = async (v) => {
		if (v.isOpen == 1) {
			const res = await business.XYDAXedit({ id: v.id, isOpen: 0 })
		} else {
			const res = await business.XYDAXedit({ id: v.id, isOpen: 1 })
		}
		await onSearch()
	}

	//共享
	const handleSwitchChange = async (v) => {
		// console.log(v)
		if (v.isShare == 1) {
			const res = await business.XYPJshares([{ id: v.id, isShare: 0 }])
		} else {
			const res = await business.XYPJshares([{ id: v.id, isShare: 1 }])
		}
		message.success('操作成功')
		await onSearch()
	}

	const state = reactive({
		selectedRowKeys: [],
		loading: false
	})
	const hasSelected = computed(() => state.selectedRowKeys.length > 0)
	const start = async () => {
		state.loading = true
		const res = await business.XYPJshares(del.value)
		setTimeout(() => {
			state.loading = false
			message.success('操作成功')
			onSearch()
		}, 300)
	}
	const del = ref([])
	const onSelectChange = (selectedRowKeys) => {
		state.selectedRowKeys = []
		del.value = []
		state.selectedRowKeys = selectedRowKeys
		// selectedRowKeys.forEach((element) => {
		// 	const index = del.value.findIndex((item) => item.id == element.id)
		// 	if (index !== -1) {
		// 		del.value.splice(index, 1)
		// 	} else {
		// 		del.value.push(element)
		// 	}
		// })
		del.value = state.selectedRowKeys.map((item) => {
			return {
				id: item.id,
				isShare: 1
			}
		})
		console.log(del.value)
	}

	const { DEl, change, tableData, onSearch, handlePageChange, pagination, Search, onReset, SearchFormDel } =
		listPagination({ unitName: '' }, business.XYDAlist, {}, { isEntAppQuery: 1, sortOrder: 'ASCEND' })

	// 查看表单数据
	const onView = (record, row) => {
		switch (record) {
			case 'add':
				router.push('/business/personnelDel?add')
				break
			case 'del':
				DEl(row)
				break
			case 'view':
				router.push(`/verifyQuery/statistical/statisticalDel?code=${true}&id=${row.id}`)
				break
			case 'edit':
				router.push(`/business/personnelDel?code=1&id=${row.id}`)
				break
			default:
				// router.push('/business/personnelDel')
				break
		}
	}
</script>
<style lang="less" scoped>
	.search {
		padding-top: 20px;
		padding-left: 50px;
		margin-bottom: 32px;
		.btnBox {
			width: 100%;
			display: inline-block;
			display: flex;
			justify-content: space-around;
			align-items: center;
			.primarySele {
				margin-left: 20px;
				margin-right: 20px;
			}
		}
	}
	.silt-title {
		display: flex;
	}
	.switch_del {
		height: 40px;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
