<template>
	<div>
		<template v-if="state">
			<a-form :model="createForm" layout="vertical"  class="resume-form">
				<a-row :gutter="20">
					<a-form-item label="上传营业执照" name="photo">
						<upload upload_txt="上传营业执照" @uploadSuccess="uploadPhotoSuccess"
							:success_image="addOtherForm.license_img" images_length="1" image_type="4"></upload>
					</a-form-item>
					<a-col span="24">
						<a-flex justify="flex-end">
							<a-space>
								<a-button  @click="resetForm" size="large">取消</a-button>
								<a-button type="primary" @click="saveForm" size="large">保存</a-button>
							</a-space>
						</a-flex>
					</a-col>
				</a-row>
			</a-form>
		</template>
		<template v-else>
			<div v-if="createForm.license_path">
				<image-container :need="true" :imgObj="{src:createForm.license_path? imageprefix + createForm.license_path : '/images/gongzhonghao.png',width: '200px',height:'280px', mode: 'fill'}"></image-container>
			</div>
		</template>
	</div>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineProps, watch, defineEmits, createVNode } from 'vue';
	import { PostCompanyDetailInfo, PostCompanyLicenseUpload } from '@/apis/models';
	import Upload from '@/components/form/upload.vue';
	import { warnToast, successToast } from '@/utils/toastHelper';
	import { dataForm, otherDataForm, reset } from '@/components/company/information/image/data.ts';
	import { useCommon } from '@/hooks/useCommon';
	let { imageprefix } = useCommon();
	let props = defineProps(['form_state']);
	const emit = defineEmits();
	let state = ref<Boolean>(false)
	
	let createForm = ref<CompanyLicenceType.LicenceFormType>(dataForm)
	let addOtherForm = ref<CompanyLicenceType.OtherFormType>(otherDataForm)
	
	onMounted(async () => {
		getBasic()
	})
	
	const getBasic = () => {
		PostCompanyDetailInfo().then(res => {
			createForm.value.license_path = res.data.license_path ?  res.data.license_path : ''
			addOtherForm.value = {
				license_img: res.data.license_path ? imageprefix + res.data.license_path : '',
			}
		})
	}
	
	// 营业执照
	const uploadPhotoSuccess = (data : Object) => {
		addOtherForm.value.license_img = imageprefix + data
		createForm.value.license_path = data
	}
	
	const saveForm = () => {
		PostCompanyLicenseUpload(createForm.value).then(res => {
			successToast('保存成功')
			resetForm();
			getBasic();
		})
	}
	
	const resetForm = () => {
		createForm.value = reset().dataForm as CompanyLicenceType.LicenceFormType;
		addOtherForm.value = reset().otherDataForm as CompanyLicenceType.OtherFormType;
		state.value = false;
		emit("quitEdit")
	}

	watch(() => [props.form_state], (newVal) => {
		state.value = newVal[0];
		getBasic();
	})
</script>

<style>
</style>
