<template>
	<!-- 初稿意见反馈 -->
	<div class="planningFirstFeedback 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>
			</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" :column="2" :options="FeedbackDialog.planOptions">
				<template #attachment="{ row }">
					<DIYUPLoad v-model:urls="FeedbackDialog.row[row.name]" disabled></DIYUPLoad>
				</template>
			</DIYDescriptions>
			<h4 class="form_title">处室意见</h4>
			<DIYForm
				ref="DialogForm"
				:model="FeedbackDialog.form"
				:options="FeedbackDialog.formOptions"
				:column="3"
				v-if="FeedbackDialog.mode === 'add'"
			></DIYForm>
			<DIYDescriptions v-else :data="FeedbackDialog.form" :column="2" :options="FeedbackDialog.formOptions">
				<template #attachment="{ row }">
					<DIYUPLoad v-model:urls="FeedbackDialog.form[row.name]" listType="picture-card" disabled></DIYUPLoad>
				</template>
			</DIYDescriptions>
			<template #footer>
				<a-button @click="FeedbackDialog.open = false">取消</a-button>
				<a-button v-if="FeedbackDialog.form.statusFlag != 2" class="opinion" @click="onDialogOk(1)">保存</a-button>
				<a-button v-if="FeedbackDialog.form.statusFlag != 2" type="primary" @click="onDialogOk(2)">发布</a-button>
			</template>
		</a-modal>
	</div>
</template>
<script setup name="planningFirstFeedback">
	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/planningFirstFeedback'
	import { globalStore } from '@/store'
	import utils from '@/utils/utils'
	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: 'endDate' },
		{ title: '操作', width: 400, key: 'CZ' }
	]
	const DialogForm = ref(null)
	const FeedbackDialog = reactive({
		open: false,
		title: '',
		row: undefined,
		formRules: {
			_处室名称: [{ required: true, trigger: 'change', message: '请输入填写处室名称！' }],
			_人员名称: [{ required: true, trigger: 'change', message: '请输入填写人员名称！' }],
			_时间: [{ required: true, trigger: 'change', message: '请输入选择时间！' }],
			_处室建议: [{ required: true, trigger: 'change', message: '请输入处室建议！' }]
		},
		formOptions: [
			{ label: '处室名称', name: 'sysOrgName', mode: 'text', required: true, span: 1 },
			{ label: '人员名称', name: 'sysUserName', mode: 'text', required: true, span: 1 },
			{ label: '时间', name: 'feedbackTime', mode: 'text', required: true, span: 1 },
			{ label: '处室建议', name: 'adviceContent', mode: 'textarea', autoSize: { minRows: 3 }, required: true, span: 3 },
			{
				label: '附件',
				listType: 'picture-card',
				name: 'attachment',
				mode: 'fileurls',
				multiple: true,
				change: changeUpload,
				span: 3
			}
		],
		planOptions: [
			{ label: '规划名称', name: 'planName', mode: 'input', span: 2 },
			// {label:'收集处室',name:'sysOrgId',mode:'select',span:2},
			{ label: '规划正文', name: 'content', mode: 'textarea', span: 2 },
			{ label: '附件', name: 'attachment', mode: 'fileurls', span: 2 }
		],
		form: {}
	})
	const { tableData, onSearch, onReset, handlePageChange, pagination, Search, SearchFormDel } = listPagination(
		{ planName: '' },
		API.get,
		{},
		{},
		'007'
	)

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

	function onFeedbackRow(row) {
		FeedbackDialog.row = JSON.parse(JSON.stringify(row))
		FeedbackDialog.row.attachment = (FeedbackDialog.row.attachment || '').split(',').filter((v) => v)

		FeedbackDialog.row.sysOrgId = (FeedbackDialog.row.sysOrgId || '').split(',').filter((v) => v)
		FeedbackDialog.mode = 'add'
		getFeedbackDetail(row.id).then((reg) => {
			if (reg) {
				FeedbackDialog.form = reg
			} else {
				FeedbackDialog.form = {
					sysUserId: userInfo.value.id,
					sysOrgId: userInfo.value.orgId
				}
			}

			if (reg) {
				console.log(reg.attachment)
				if (reg.attachment == '') {
					console.log('空')
				} else {
					// FeedbackDialog.form.attachment = reg.attachment.split(',')
					FeedbackDialog.form.attachment = (reg.attachment || '').split(',').filter((v) => v)
				}
			}

			// FeedbackDialog.form.attachment = (FeedbackDialog.row.attachment || '').split(',').filter((v) => v)
			Object.assign(FeedbackDialog.form, {
				feedbackTime: utils.getDate('Y-M-D'),
				sysOrgName: userInfo.value.orgName,
				sysUserName: userInfo.value.name,
				updateUser: userInfo.value.id
			})
			FeedbackDialog.mode = FeedbackDialog.form.statusFlag === 2 ? 'view' : 'add'
			FeedbackDialog.title = '规划整改'
			// FeedbackDialog.open = true
		})
		FeedbackDialog.open = true
	}

	function getFeedbackDetail(id) {
		return API.detail({ id }).then((reg) => {
			return reg
		})
	}
	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.firstDraftCollectId = FeedbackDialog.row.id

				if (Array.isArray(fd.attachment)) {
					fd.attachment = (fd.attachment || '').join(',')
				}

				fd.statusFlag = status
				return URLs[fd.id ? '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;
				}
			}
		}
	}
	.form_title {
		font-size: 16px;
		font-weight: bold;
		margin: 10px 0;
	}
</style>
