<template>
	<el-dialog v-model="dialogEmptyFromVisible" title="教职工信息导出" width="80%">
		<el-form :inline="true" label-width="100px" style="margin-bottom: 20px">
			<el-form-item>
				<el-tag type="info">导出依据"职工信息"页面的检索条件</el-tag>
				<el-tag type="warning" style="margin-left: 10px">请在搜索后再打开导出页面</el-tag>
			</el-form-item>
			<el-form-item label="是否退休">
				<el-select v-model="tiedTag" filterable reserve-keyword clearable style="width: 200px">
					<el-option v-for="item in YES_OR_NO" :key="item.value" :label="item.label" :value="item.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="exportTeacherInfo" :loading="exportLoading" :icon="Download">导出</el-button>
			</el-form-item>
		</el-form>

		<!-- 基础信息 -->
		<el-card shadow="never" style="margin-bottom: 20px">
			<template #header>
				<div style="display: flex; justify-content: space-between; align-items: center">
					<span>基础信息</span>
					<el-checkbox v-model="allCheckTeacherBaseInfo">全选</el-checkbox>
				</div>
			</template>
			<el-checkbox-group v-model="checkedTeacherBaseInfo">
				<el-checkbox v-for="(item, index) in teacherBasicCheckData" :label="item.value" :key="index" style="margin-right: 20px; margin-bottom: 10px">
					{{ item.label }}
				</el-checkbox>
			</el-checkbox-group>
		</el-card>

		<!-- 岗位信息 -->
		<el-card shadow="never" style="margin-bottom: 20px">
			<template #header>
				<div style="display: flex; justify-content: space-between; align-items: center">
					<span>岗位信息</span>
					<el-checkbox v-model="allCheckStationInfo">全选</el-checkbox>
				</div>
			</template>
			<el-checkbox-group v-model="checkedTeacherStationInfo">
				<el-checkbox v-for="(item, index) in teacherStationData" :key="index" :label="item.value" style="margin-right: 20px; margin-bottom: 10px">
					{{ item.label }}
				</el-checkbox>
			</el-checkbox-group>
		</el-card>

		<!-- 其他 -->
		<el-card shadow="never">
			<template #header>
				<div style="display: flex; justify-content: space-between; align-items: center">
					<span>其他</span>
					<el-checkbox v-model="allCheckedother">全选</el-checkbox>
				</div>
			</template>
			<el-checkbox-group v-model="checkedTeacherOtherInfo">
				<el-checkbox v-for="(item, index) in teacherOtherData" :key="index" :label="item.value" style="margin-right: 20px; margin-bottom: 10px">
					{{ item.label }}
				</el-checkbox>
			</el-checkbox-group>
		</el-card>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { ElNotification } from 'element-plus';
import { Download } from '@element-plus/icons-vue';
import global from '/@/components/tools/commondict.vue';
import request from '/@/utils/request';
import { makeExportTeacherInfoBySelfTask } from '/@/api/professional/professionalfile';

// Props
const props = defineProps<{
	search: any;
}>();

// 响应式数据
const tiedTag = ref('');
const YES_OR_NO = global.YES_OR_NO;
const exportLoading = ref(false);
const dialogEmptyFromVisible = ref(false);
const allCheckTeacherBaseInfo = ref(false);
const allCheckStationInfo = ref(false);
const allCheckedother = ref(false);
const checkedTeacherBaseInfo = ref<string[]>([]);
const checkedTeacherStationInfo = ref<string[]>([]);
const checkedTeacherOtherInfo = ref<string[]>([]);

const teacherBasicCheckData = [
	{ label: '姓名', value: 'realName_姓名' },
	{ label: '工号', value: 'teacherNo_工号' },
	{ label: '部门', value: 'deptCode_部门' },
	{ label: '性别', value: 'sex_性别' },
	{ label: '身份证', value: 'idCard_身份证' },
	{ label: '出生年月', value: 'birthday_出生年月' },
	{ label: '年龄', value: 'age_年龄' },
	{ label: '手机', value: 'telPhone_手机' },
	{ label: '手机2', value: 'telPhoneTwo_手机2' },
	{ label: '宅电', value: 'homePhone_宅电' },
	{ label: '民族', value: 'national_民族' },
	{ label: '籍贯', value: 'nativePlace_籍贯' },
	{ label: '出生地', value: 'birthPlace_出生地' },
	{ label: '健康状况', value: 'health_健康状况' },
	{ label: '特长', value: 'speciality_特长' },
	{ label: '银行卡号', value: 'bankNo_银行卡号' },
	{ label: '开户行', value: 'bankOpen_开户行' },
	{ label: '家庭住址', value: 'homeAddress_家庭住址' },
	{ label: '授课类型', value: 'teacherCate_授课类型' },
	{ label: '允许进出', value: 'inoutFlag_允许进出' },
	{ label: '进出情况备注', value: 'inoutRemarks_进出情况备注' },
];

const teacherStationData = [
	{ label: '教师类型', value: 'teacherTypeName_教师类型' },
	{ label: '党支部', value: 'branchName_党支部' },
	{ label: '用工性质', value: 'employmentNatureName_用工性质' },
	{ label: '岗位类别', value: 'stationTypeName_岗位类别' },
	{ label: '岗位级别', value: 'stationLevelName_岗位级别' },
	{ label: '职务级别', value: 'stationDutyLevelName_职务级别' },
	{ label: '任现岗位职级时间', value: 'stationDate_任现岗位职级时间' },
	{ label: '在职情况', value: 'atStationName_在职情况' },
	{ label: '职务', value: 'dutyDesc_职务' },
	{ label: '参加工作时间', value: 'workDate_参加工作时间' },
	{ label: '退休时间', value: 'retireDate_退休时间' },
	{ label: '职位类别', value: 'teacherClassify_职位类别' },
	{ label: '任现职务时间', value: 'dutyDate_任现职务时间' },
	{ label: '进编时间', value: 'entryDutyDate_进编时间' },
	{ label: '进校时间', value: 'entrySchoolDate_进校时间' },
];

const teacherOtherData = [
	{ label: '职业资格工种', value: 'zyzg_职业资格工种' },
	{ label: '职业资格等级', value: 'zyzgLevel_职业资格等级' },
	{ label: '政治面貌', value: 'zzmm_政治面貌' },
	{ label: '学历', value: 'xl_学历' },
	{ label: '学位', value: 'xw_学位' },
	{ label: '职称', value: 'zc_职称' },
	{ label: '高校教师资格证', value: 'highCer_高校教师资格证' },
	{ label: '中等教师资格证', value: 'midCer_中等教师资格证' },
	{ label: '教师上岗证', value: 'priCer_教师上岗证' },
	// {label:'共同居住人',value:'livewith_共同居住人'},
];

// 导出文件函数
const exportForModel = async (params: any, fileNameStr: string, url: string) => {
	try {
		const res = await request({
			method: 'post',
			url: url,
			data: params,
			responseType: 'blob',
			headers: {
				'Content-Type': 'application/json',
			},
		});
		const blob = new Blob([res]);
		const fileName = fileNameStr;
		const elink = document.createElement('a');
		elink.download = fileName;
		elink.style.display = 'none';
		elink.href = URL.createObjectURL(blob);
		document.body.appendChild(elink);
		elink.click();
		URL.revokeObjectURL(elink.href);
		document.body.removeChild(elink);
	} catch (err) {
		ElNotification.error({
			title: '错误',
			message: '导出失败',
		});
	}
};

// Watch
watch(allCheckTeacherBaseInfo, (newVal) => {
	if (newVal) {
		checkedTeacherBaseInfo.value = teacherBasicCheckData.map((item) => item.value);
	} else {
		checkedTeacherBaseInfo.value = [];
	}
});

watch(allCheckStationInfo, (newVal) => {
	if (newVal) {
		checkedTeacherStationInfo.value = teacherStationData.map((item) => item.value);
	} else {
		checkedTeacherStationInfo.value = [];
	}
});

watch(allCheckedother, (newVal) => {
	if (newVal) {
		checkedTeacherOtherInfo.value = teacherOtherData.map((item) => item.value);
	} else {
		checkedTeacherOtherInfo.value = [];
	}
});

// 方法
const init = () => {
	tiedTag.value = props.search.tied;
	dialogEmptyFromVisible.value = true;
};

const exportTeacherInfo = async () => {
	if (checkedTeacherBaseInfo.value.length == 0 && checkedTeacherStationInfo.value.length == 0 && checkedTeacherOtherInfo.value.length == 0) {
		ElNotification({
			title: '警告',
			message: '请选择要导出的字段',
			type: 'warning',
			duration: 3000,
		});
		return;
	}
	exportLoading.value = true;

	const searchData = JSON.parse(JSON.stringify(props.search));
	searchData.tied = tiedTag.value;
	const params = {
		checkedTeacherBaseInfo: checkedTeacherBaseInfo.value,
		checkedTeacherStationInfo: checkedTeacherStationInfo.value,
		checkedTeacherOtherInfo: checkedTeacherOtherInfo.value,
		teacherBaseDTO: searchData,
	};

  makeExportTeacherInfoBySelfTask(params).then((res:any)=>{
    ElNotification.success({
      title: '下载后台进行中',
      message: '操作成功'
    })
  });

	setTimeout(() => {
		exportLoading.value = false;
	}, 3000);
};

// 暴露方法给父组件
defineExpose({
	init,
});
</script>

<style scoped></style>
