<template>
	<div>
		<div class="home-container layout-pd">

			<div class="ustitles">潍坊市房地产交易信息{{ state.month }}月月报统计表</div>

			<el-card shadow="hover" class="layout-padding-auto mb10">
				<div class="ctitle">
					<div class="title">手动上传</div>
					<el-button type="primary" size="default" @click="download(0)">下载Excel</el-button>
				</div>
				<movement @removeshow="removeshow" :month="state.month" :list="state.escalation"></movement>
			</el-card>
			<el-card shadow="hover" class="layout-padding-auto">
				<div class="ctitle">
					<div class="title">系统生成</div>
					<el-button type="primary" size="default" @click="download(1)">下载Excel</el-button>
				</div>
				<automaticv :month="state.month" :list="state.regular"></automaticv>
			</el-card>
		</div>

		<el-dialog v-model="state.show" title="退回" width="400px" :before-close="handleClose">
			<el-input v-model="state.form.remark" :rows="5" type="textarea" placeholder="请输入退回理由" />
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.show = false">取消</el-button>
					<el-button type="primary" @click="examine">确认退回</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="daydetail">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage, ElNotification } from 'element-plus';
import { entApi } from "/@/api/ent";
import { useRoute, useRouter } from "vue-router";
import { getmonth } from "/@/api/report/month";

const route = useRoute();
const router = useRouter();
import mittBus from "/@/utils/mitt";

// 自动
const automaticv = defineAsyncComponent(
	() => import("/@/views/report/month/components/automaticvTable.vue")
);

// 手动
const movement = defineAsyncComponent(
	() => import("/@/views/report/month/components/movementTable.vue")
);

const handleClose = () => {
	state.show = false;
}
// 弹出框退回
const removeshow = (id: any) => {
	state.form.id = id;
	state.show = true;
}

const examine = async () => {
	let sfrom = state.form;
	if (sfrom.id == '') return ElMessage.error('暂未找到id')
	if (sfrom.remark == '') return ElMessage.error('请输入理由')
	let item = await getmonth().examineup(state.form)
	if (item.code == 500) return ElMessage.error(item.msg)
	ElMessage.success(item.msg)
	gettable();
	state.show = false;
	state.form = { id: '', remark: '' }
}


const goBack = () => {
	router.go(-1);
	mittBus.emit(
		"onCurrentContextmenuClick",
		Object.assign({}, { contextMenuClickId: 1, ...route })
	);
};

// 定义变量内容
const state = reactive({
	form: { id: '', remark: '' },
	show: false,
	uid: '' as any,
	month: '',
	// 自动
	regular: [] as any,
	// 手动
	escalation: [] as any
});

const gettable = async () => {
	let items = await getmonth().getDetail(state.uid)
	let _arr = items.data;
	_arr.regular.forEach((res: any) => {
		if (_arr.escalation != null) {
			_arr.escalation.forEach((cres: any) => {
				if (cres.areaCode != '370700') {
					if (res.areaCode == cres.areaCode) {
						if (res.approveArea != cres.approveArea) cres.approveAreatag = 1
						else cres.approveAreatag = 0

						if (res.resApproveArea != cres.resApproveArea) cres.resApproveAreatag = 1
						else cres.resApproveAreatag = 0

						if (res.resApproveCount != cres.resApproveCount) cres.resApproveCounttag = 1
						else cres.resApproveCounttag = 0

						if (res.resSaleDownArea != cres.resSaleDownArea) cres.resSaleDownAreatag = 1
						else cres.resSaleDownAreatag = 0

						if (res.resSaleDownCount != cres.resSaleDownCount) cres.resSaleDownCounttag = 1
						else cres.resSaleDownCounttag = 0

						if (res.resSaleUpArea != cres.resSaleUpArea) cres.resSaleUpAreatag = 1
						else cres.resSaleUpAreatag = 0

						if (res.resSaleUpCount != cres.resSaleUpCount) cres.resSaleUpCounttag = 1
						else cres.resSaleUpCounttag = 0

						if (res.inventoryTotalArea != cres.inventoryTotalArea) cres.inventoryTotalAreatag = 1
						else cres.inventoryTotalAreatag = 0

						if (res.resInventoryTotalCount != cres.resInventoryTotalCount) cres.resInventoryTotalCounttag = 1
						else cres.resInventoryTotalCounttag = 0

						if (res.resInventoryTotalArea != cres.resInventoryTotalArea) cres.resInventoryTotalAreatag = 1
						else cres.resInventoryTotalAreatag = 0

						if (res.outArea != cres.outArea) cres.outAreatag = 1
						else cres.outAreatag = 0

						if (res.resOutArea != cres.resOutArea) cres.resOutAreatag = 1
						else cres.resOutAreatag = 0

						if (res.outProportion != cres.outProportion) cres.outProportiontag = 1
						else cres.outProportiontag = 0

						if (res.outSecondArea != cres.outSecondArea) cres.outSecondAreatag = 1
						else cres.outSecondAreatag = 0

						if (res.resOutSecondArea != cres.resOutSecondArea) cres.resOutSecondAreatag = 1
						else cres.resOutSecondAreatag = 0

						if (res.resOutProportion != cres.resOutProportion) cres.resOutProportiontag = 1
						else cres.resOutProportiontag = 0

						if (res.outSecondProportion != cres.outSecondProportion) cres.outSecondProportiontag = 1
						else cres.outSecondProportiontag = 0

						if (res.resOutSecondProportion != cres.resOutSecondProportion) cres.resOutSecondProportiontag = 1
						else cres.resOutSecondProportiontag = 0

					}
				}
			})
		}

	})

	state.regular = items.data.regular
	state.month = items.data.regular[0].month
	state.escalation = items.data.escalation
}
function removeChar(str: string, char: string) {
	var regex = new RegExp(char, 'g');
	return str.replace(regex, '');
}

const download = async (row: any) => {
	let type = 0;
	let title = ''
	if (row == 0) {
		if (state.escalation == null || state.escalation.length <= 1) return ElMessage.error('暂无手动上传数据')
		type = state.escalation[1].reportType
		title = `潍坊市${state.escalation[1].month}月报数据手动上传.xlsx`
	}
	else {
		type = state.regular[1].reportType
		title = `潍坊市月报${state.regular[1].month}月系统生成.xlsx`
	}

	// 下载excel
	let item = await getmonth().downloadUrl(state.uid, type);
	let download: any = await getmonth().download(item);
	let blob = new Blob([download], { type: "application/octet-stream;" });
	const url = window.URL.createObjectURL(blob);
	const link = document.createElement("a");
	link.href = url;
	link.download = title;
	document.body.appendChild(link);
	link.click();
};

// 页面加载时
onMounted(() => {
	state.uid = route.query.id
	gettable()
});
</script>

<style scoped lang="scss">
.ctitle {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding-bottom: 10px;
}

.ustitles {
	font-size: 18px;
	font-weight: bold;
	text-align: center;
	padding-top: 20px;
	padding-bottom: 10px;
}

.ors {
	display: flex;
	flex-direction: column;
	background-color: #ecf2fe;
	padding: 20px;
	margin-bottom: 10px;
	margin: 10px;

	.orsitem {
		display: flex;
		align-items: center;
		padding-left: 20px;
		padding-bottom: 5px;

		b {
			flex: 0 0 5%;
			color: #001330;
		}
	}
}

.orstitle {
	display: flex;
	align-items: center;
	font-weight: bold;
	color: #333;
	font-size: 15px;
	margin-bottom: 10px;

	.color {
		color: #2362e1;
		margin-right: 4px;
	}
}

.title {
	font-weight: bold;
	font-size: 14px;
	border-left: 3px solid #409efe;
	padding-left: 10px;
	margin-bottom: 20px;
}

.bgwhite {
	background-color: #fff;
}
</style>