<template>
	<!-- 规划修改 -->
	<div class="planningFirstEdit pageContent">
		<a-space class="search">
			规划名称:<a-input v-model:value="SearchFormDel.planName" />
			<div class="btnBox">
				<a-button type="primary" @click="Search"> 查询 </a-button>
				<a-button @click="onReset"> 重置 </a-button>
				<a-button class="pub" @click="onAddRow"> 新增 </a-button>
			</div>
		</a-space>
		<div class="list">
			<a-table :dataSource="tableData" :pagination="pagination" :columns="tableColumns">
				<template #bodyCell="{ column, record, index }">
					<template v-if="column.key === 'INDEX'">{{ index + 1 }}</template>
					<template v-else-if="column.key === 'CZ'">
						<a-button class="view" @click="onViewRow(record)">查看</a-button>
						<a-button class="edit" @click="onEditRow(record)">编辑</a-button>
						<a-button class="remove" @click="onDeleteRow(record)">删除</a-button>
					</template>
				</template>
			</a-table>
		</div>
		<a-modal v-model:visible="AddEditDialog.open" :title="AddEditDialog.title" @ok="onDialogOk" width="800px">
			<DIYForm
				ref="DialogForm"
				v-if="['add', 'edit'].includes(AddEditDialog.mode)"
				:rules="AddEditDialog.formRules"
				:column="2"
				:model="AddEditDialog.form"
				:options="AddEditDialog.formOptions"
			></DIYForm>
			<DIYDescriptions
				:data="AddEditDialog.form"
				:options="AddEditDialog.formOptions"
				:column="2"
				v-else-if="AddEditDialog.mode === 'view'"
			>
				<template #attachment="{ row }">
					<DIYUPLoad listType="picture-card" :urls="AddEditDialog.form[row.name]" disabled />
				</template>
			</DIYDescriptions>
			<template #footer v-if="AddEditDialog.mode === 'view'"></template>
		</a-modal>
	</div>
</template>
<script setup name="planningFirstEdit">
	import { listPagination } from '@/hook/listlistPagination.js'
	import API from '@/api/policy.info/planningFirstEdit'
	import PFO from '@/api/policy.info/planningFirstOpinion'
	import PF from '@/api/policy.info/planningFirst'

	import global from '@/api/basic.info/global.js'
	import { useRouter } from 'vue-router'
	import { Modal } from 'ant-design-vue'
	import { onMountedWatch } from '@/hook/onMounted.js'
	import { useRoute } from 'vue-router'
	const route = useRoute()
	import utils from '@/utils/utils'
	const router = useRouter()
	const tableColumns = [
		{ title: '序号', width: 100, align: 'center', key: 'INDEX' },
		{ title: '规划名称', dataIndex: 'planName' },
		{ title: '修改时间', dataIndex: 'updateTime' },
		{ title: '操作', width: 400, key: 'CZ' }
	]
	const PlanNameList = ref([])
	const DialogForm = ref(null)
	const AddEditDialog = reactive({
		open: false,
		title: '',
		mode: 'add',
		formRules: {
			planName: [{ required: true, trigger: 'change', message: '请输入规划名称！' }],
			updateTime: [{ required: true, trigger: 'change', message: '请选择修改时间！' }],
			planContent: [{ required: true, trigger: 'change', message: '请输入规划正文！' }]
		},
		formOptions: [
			{
				label: '规划名称',
				name: 'planName',
				mode: 'select',
				values: PlanNameList,
				onSelect(ev, row) {
					getPlanDetail(row.value).then((reg) => {
						reg.firstDraftId = reg.id
						reg.modifyTime = utils.getDate('Y-M-D H:I:S')
						AddEditDialog.form = reg
					})
				},
				span: 2
			},
			{ label: '修改时间', name: 'updateTime', mode: 'date', span: 2, valueFormat: 'YYYY-MM-DD', format: 'YYYY-MM-DD' },
			{ label: '规划正文', name: 'planContent', mode: 'textarea', span: 2 },
			{
				label: '附件',
				listType: 'picture-card',
				name: 'attachment',
				mode: 'fileurls',
				multiple: true,
				change: changeUpload,
				span: 2
			}
		],
		form: {}
	})
	onMounted(() => {
		getPlanName()
	})

	const { tableData, onSearch, onReset, handlePageChange, pagination, Search, SearchFormDel } = listPagination(
		{ planName: '' },
		API.get,
		{},
		{},
		'682'
	)

	onMountedWatch(route, '/basic.info/planningFirstEdit', onSearch, pagination)

	function getPlanName(key) {
		PFO.getPlanName({ mapNow: 1 }).then((reg) => {
			PlanNameList.value = (reg?.mapNow || []).map((v) => {
				return { label: v.planName, value: v.id }
			})
		})
	}
	function getPlanDetail(id) {
		return PF.detail({ id }).then((reg) => {
			return reg
		})
	}

	function changeUpload(fd) {
		return global.uploadFile(fd).then((reg) => {
			return reg
		})
	}
	function onViewRow(row) {
		AddEditDialog.form = JSON.parse(JSON.stringify(row))

		if (AddEditDialog.form.attachment == '') {
			console.log('AddEditDialog.form.attachment', AddEditDialog.form.attachment)
		} else {
			AddEditDialog.form.attachment = AddEditDialog.form.attachment.split(',')
		}

		AddEditDialog.mode = 'view'
		AddEditDialog.title = '查看规划修改'
		AddEditDialog.open = true
	}
	function onAddRow() {
		AddEditDialog.form = {}
		AddEditDialog.mode = 'add'
		AddEditDialog.title = '添加规划修改'
		AddEditDialog.open = true
	}

	function onEditRow(row) {
		AddEditDialog.form = JSON.parse(JSON.stringify(row))

		if (AddEditDialog.form.attachment == '') {
			console.log('AddEditDialog.form.attachment', '空')
		} else {
			AddEditDialog.form.attachment = (AddEditDialog.form.attachment || '').split(',').filter((v) => v)
		}
		AddEditDialog.mode = 'edit'
		AddEditDialog.title = '编辑规划修改'
		AddEditDialog.open = true
	}
	function onDeleteRow(row) {
		Modal.confirm({
			content: '是否确认删除？',
			onOk() {
				return API.delete([{ id: row.id }]).then((reg) => {
					onSearch()
				})
			},
			onCancel() {
				Modal.destroyAll()
			}
		})
	}
	function onDialogOk() {
		const URLs = {
			add: API.add,
			edit: API.edit
		}
		DialogForm.value
			.validate()
			.then((reg) => {
				let fd = JSON.parse(JSON.stringify(AddEditDialog.form))
				console.log(fd.attachment)
				if (Array.isArray(fd.attachment)) {
					fd.attachment = (fd.attachment || []).join(',')
				} else {
					console.log('过')
				}

				return URLs[AddEditDialog.mode](fd)
			})
			.then((reg) => {
				AddEditDialog.open = false
				pagination.current = 1
				pagination.pageSize = 5
				onSearch()
			})
	}
</script>
<style lang="less" scoped>
	.view {
		background-color: rgba(112, 125, 233);
		color: #fff;
	}
	.edit {
		background-color: rgba(210, 176, 61);
		color: #fff;
	}
	.remove {
		background-color: rgba(255, 70, 70);
		color: #fff;
	}
	.add {
		background-color: rgba(83, 184, 36);
		color: #fff;
	}
	.pageContent {
		background: #fff;
		padding: 10px 20px;
		.search {
			padding: 10px 0;
			.btnBox {
				display: flex;
				> * {
					margin-right: 10px;
				}
			}
		}
	}
</style>
