<template>
	<!-- 意见反馈 -->
	<div class="feedback 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.collectionDeadline" valueFormat="YYYY-MM-DD" />
			</a-space>
			<div class="btnBox">
				<a-button type="primary" @click="Search"> 查询 </a-button>
				<a-button @click="onReset"> 重置 </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="onFeedbackRow(record)">反馈</a-button>
					</template>
				</template>
			</a-table>
		</div>
		<a-modal v-model:visible="FeedbackDialog.open" :title="FeedbackDialog.title" width="800px">
			<DIYDescriptions :data="FeedbackDialog.row" :options="FeedbackDialog.planOptions" :column="2">
				<template #attachment="{ row }">
					<DIYUPLoad listType="picture-card" v-model="FeedbackDialog.row[row.name]" disabled />
				</template>
			</DIYDescriptions>
			<DIYForm
				ref="DialogForm"
				:rules="FeedbackDialog.formRules"
				:column="1"
				v-if="FeedbackDialog.mode === 'add'"
				:model="FeedbackDialog.form"
				:options="FeedbackDialog.formOptions"
			></DIYForm>
			<DIYDescriptions v-else :data="FeedbackDialog.form" :options="FeedbackDialog.formOptions" :column="2">
				<template #attachment="{ row }">
					<DIYUPLoad listType="picture-card" v-model="FeedbackDialog.form[row.name]" disabled />
				</template>
			</DIYDescriptions>
			<template #footer>
				<a-button @click="FeedbackDialog.open = false">取消</a-button>
				<a-button v-if="FeedbackDialog.form.status != 2" class="opinion" @click="onDialogOk(1)">保存</a-button>
				<a-button v-if="FeedbackDialog.form.status != 2" type="primary" @click="onDialogOk(2)">发布</a-button>
			</template>
		</a-modal>
	</div>
</template>
<script setup name="feedback">
	import { listPagination } from '@/hook/listlistPagination.js'
	import global from '@/api/basic.info/global.js'
	import { useRouter } from 'vue-router'
	import API from '@/api/policy.info/feedback'
	import ORG from '@/api/basic.info/unReporCityApi.js'
	import { globalStore } from '@/store'
	const global_store = globalStore()
	import { onMountedWatch } from '@/hook/onMounted.js'
	import { useRoute } from 'vue-router'
	const route = useRoute()
	const userInfo = computed(() => {
		return global_store.userInfo
	})
	const router = useRouter()
	const tableColumns = [
		{ title: '序号', width: 100, align: 'center', key: 'INDEX' },
		{ title: '规划名称', dataIndex: 'planName' },
		{ title: '截止日期', dataIndex: 'collectionDeadline' },
		{ title: '操作', width: 400, key: 'CZ' }
	]
	const ORGList = ref([])
	const DialogForm = ref(null)
	const FeedbackDialog = reactive({
		open: false,
		title: '',
		row: undefined,
		formRules: {
			adviceContent: [{ required: true, trigger: 'change', message: '请输入反馈意见！' }]
		},
		planOptions: [
			{ label: '规划名称', name: 'planName', type: 'input', span: 2 },
			// {label:'收集处室',name:'收集处室',type:'select',span:1,values:ORGList},
			{ label: '规划正文', name: 'planContent', type: 'textarea', span: 2 },
			{ label: '附件', name: 'attachment', span: 2 }
		],
		formOptions: [
			{ label: '反馈意见', name: 'adviceContent', mode: 'textarea', span: 1 },
			{
				label: '附件',
				listType: 'picture-card',
				name: 'attachment',
				mode: 'fileurls',
				multiple: true,
				change: changeUpload,
				span: 1
			}
			//
		],
		form: {}
	})

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

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

	function getORGList() {
		ORG.unReporCityquery().then((reg) => {
			ORGList.value = reg.map((v) => {
				return { label: v.name, value: v.id }
			})
		})
	}

	function onFeedbackRow(row) {
		FeedbackDialog.row = JSON.parse(JSON.stringify(row))
		FeedbackDialog.row.attachment = (FeedbackDialog.row.adviceAttachment || '').split(',').filter((v) => v)
		FeedbackDialog.row.sysOrgId = (FeedbackDialog.row.sysOrgId || '').split(',').filter((v) => v)
		FeedbackDialog.mode = 'add'
		FeedbackDialog.form.attachment = ''
		//
		API.detail({ id: row.id }).then((reg) => {
			if (reg) {
				FeedbackDialog.form = reg
				FeedbackDialog.title = '意见收集'
				console.log('来来', FeedbackDialog.form.attachment)
				// 1 保存 ， 2提交
				FeedbackDialog.mode = reg.status == 2 ? 'view' : 'add'
				console.log('24356789', FeedbackDialog.form)
				if (reg.attachment == '') {
					console.log('AddEditDialog.form.attachment', '空')
				} else {
					FeedbackDialog.form.attachment = (reg.attachment || '').split(',').filter((v) => v)
				}
			}
		})
		FeedbackDialog.title = '意见收集'
		FeedbackDialog.open = true
	}
	function changeUpload(fd) {
		return global.uploadFile(fd).then((reg) => {
			return reg
		})
	}
	function onDialogOk(status) {
		const URLs = {
			add: API.add,
			edit: API.edit
		}
		DialogForm.value
			.validate()
			.then((reg) => {
				let fd = JSON.parse(JSON.stringify(FeedbackDialog.form))
				fd.attachment = (fd.attachment || []).join(',')
				fd.adviceCollectId = FeedbackDialog.row.id
				fd.sysUserId = userInfo.value.id
				fd.sysOrgId = userInfo.value.orgId
				fd.status = status
				// console.log(FeedbackDialog.form.status, FeedbackDialog.form.status == 1 ? 'edit' : 'add')
				return URLs[FeedbackDialog.form.status == 1 ? 'edit' : 'add'](fd)
			})
			.then((reg) => {
				FeedbackDialog.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;
	}
	.opinion {
		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>
