<template>
	<!-- 规划初稿 -->
	<div class="planningFirst pageContent">
		<a-space class="search">
			规划名称:<a-input v-model:value="SearchFormDel.planName" />
			完成日期:
			<a-space direction="vertical" :size="12">
				<a-date-picker v-model:value="SearchFormDel.deadline" valueFormat="YYYY-MM-DD" />
			</a-space>
			<div class="btnBox">
				<a-button type="primary" @click="Search"> 查询 </a-button>
				<a-button @click="onReset"> 重置 </a-button>
				<a-button class="add" @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, record.attachment)">编辑</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
				v-if="['add', 'edit'].includes(AddEditDialog.mode)"
				ref="DialogForm"
				:model="AddEditDialog.form"
				:rules="AddEditDialog.formRules"
				:options="AddEditDialog.formOptions"
				:column="1"
			></DIYForm>

			<DIYDescriptions
				v-else-if="AddEditDialog.mode === 'view'"
				:data="AddEditDialog.form"
				:column="1"
				:options="AddEditDialog.formOptions"
			>
				<template #attachment="{ row }">
					<DIYUPLoad v-model:urls="AddEditDialog.form[row.name]" listType="picture-card" disabled></DIYUPLoad>
				</template>
			</DIYDescriptions>
			<template #footer v-if="AddEditDialog.mode === 'view'"></template>
		</a-modal>
	</div>
</template>
<script setup name="planningFirst">
	import { Modal } from 'ant-design-vue'
	import { useRouter } from 'vue-router'
	import global from '@/api/basic.info/global'
	import API from '@/api/policy.info/planningFirst'
	import APIS from '@/api/policy.info/planningFirstOpinion'
	// import { getPlanningDetaill } from '@/api/policy.info/planningFirst'
	import { message } from 'ant-design-vue'
	import { listPagination } from '@/hook/listlistPagination.js'
	import { globalStore } from '@/store'
	import { onMountedWatch } from '@/hook/onMounted.js'
	import { useRoute } from 'vue-router'
	const route = useRoute()
	const global_store = globalStore()
	const userInfo = computed(() => {
		return global_store.userInfo
	})

	const router = useRouter()
	const tableColumns = [
		{ title: '序号', width: 100, align: 'center', key: 'INDEX' },
		{ title: '规划名称', dataIndex: 'planName' },
		{ title: '完成日期', dataIndex: 'deadline' },
		{ title: '操作', width: 400, key: 'CZ' }
	]

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

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

	const DialogForm = ref(null)
	const AddEditDialog = reactive({
		open: false,
		title: '',
		mode: 'add',
		formRules: {
			planName: [{ required: true, trigger: 'change', message: '请输入规划名称！' }],
			deadline: [{ required: true, trigger: 'change', message: '请选择完成日期！' }],
			planContent: [{ required: true, trigger: 'change', message: '请输入规划正文！' }]
		},
		formOptions: [
			{ label: '规划名称', name: 'planName', mode: 'input' },
			{ label: '完成日期', name: 'deadline', mode: 'date', valueFormat: 'YYYY-MM-DD', format: 'YYYY-MM-DD' },
			{ label: '规划正文', name: 'planContent', mode: 'textarea' },
			{
				label: '附件',
				name: 'attachment',
				mode: 'fileurls',
				multiple: true,
				change: changeUpload,
				listType: 'picture-card'
			}
		],
		form: {}
	})

	// 详情接口
	const aetaill = async (v) => {
		const res = await API.getPlanningDetaill(v)
	}

	function onViewRow(row) {
		AddEditDialog.form = JSON.parse(JSON.stringify(row))
		aetaill({ id: row.id })
		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
		getPlanName()
	}
	const PlanNameList = ref([])
	function getPlanName(key) {
		APIS.getPlanName({ mapNow: 1 }).then((reg) => {
			PlanNameList.value = reg.mapAll
		})
	}

	function getPlanNameDel(key) {
		APIS.getPlanName({ mapNow: 1 }).then((reg) => {
			// 处理规划名称 重复
			let del = ''
			del = AddEditDialog.form.planName
			PlanNameList.value = reg.mapAll.filter((item) => {
				return del !== item.planName
			})
		})
	}

	// 图片回显示出现错误需要接口
	function onEditRow(row, value) {
		AddEditDialog.form = JSON.parse(JSON.stringify(row))
		const str = row.attachment
		if (AddEditDialog.form.attachment == '') {
			console.log('AddEditDialog.form.attachment', AddEditDialog.form.attachment)
		} else {
			AddEditDialog.form.attachment = AddEditDialog.form.attachment.split(',')
		}

		AddEditDialog.mode = 'edit'
		AddEditDialog.title = '编辑规划拟稿'
		AddEditDialog.open = true
		getPlanNameDel()
	}
	function onDeleteRow(row) {
		Modal.confirm({
			content: '是否确认删除？',
			onOk() {
				return API.delete([{ id: row.id }]).then((reg) => {
					onSearch()
				})
			},
			onCancel() {
				Modal.destroyAll()
			}
		})
	}
	function onOpinionRow(row) {
		router.push({ path: '/basic.info/feedbackView', query: { id: row.id } })
	}
	function changeUpload(fd) {
		return global.uploadFile(fd).then((reg) => {
			return reg
		})
	}
	function onDialogOk() {
		const URLs = {
			add: API.add,
			edit: API.edit
		}
		// 处理规划名称 重复
		const allItemsNotSatisfied = PlanNameList.value.every((item) => {
			return AddEditDialog.form.planName !== item.planName
		})
		if (allItemsNotSatisfied) {
			DialogForm.value
				.validate()
				.then((reg) => {
					let fd = JSON.parse(JSON.stringify(AddEditDialog.form))
					if (!fd.sysOrgId) {
						fd.sysOrgId = userInfo.value.orgId
					}
					fd.attachment = (fd.attachment || []).join(',')
					return URLs[AddEditDialog.mode](fd)
				})
				.then((reg) => {
					AddEditDialog.open = false
					pagination.current = 1
					pagination.pageSize = 5
					onSearch()
				})
		} else {
			message.error('规划名称重复')
		}
	}
</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>
