<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.photo_img" images_length="1" image_type="3"></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.photo">
				<image-container :need="true" :imgObj="{src:createForm.photo? imageprefix + createForm.photo : '/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, PostCompanyPhotoUpload } 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<CompanyBasicType.BasicFormType>(dataForm)
	let addOtherForm = ref<CompanyBasicType.OtherFormType>(otherDataForm)
	
	onMounted(async () => {
		getBasic()
	})
	
	const getBasic = () => {
		PostCompanyDetailInfo().then(res => {
			createForm.value.photo = res.data.photo ? res.data.photo : ''
			addOtherForm.value = {
				photo_img: res.data.photo ? imageprefix + res.data.photo : '',
			}
		})
	}
	
	// 营业执照
	const uploadPhotoSuccess = (data : Object) => {
		addOtherForm.value.photo_img = imageprefix + data
		createForm.value.photo = data
	}
	
	const saveForm = () => {
		PostCompanyPhotoUpload(createForm.value).then(res => {
			successToast('保存成功')
			resetForm();
			getBasic();
		})
	}
	const resetForm = () => {
		createForm.value = reset().dataForm as CompanyBasicType.BasicFormType;
		addOtherForm.value = reset().otherDataForm as CompanyBasicType.OtherFormType;
		state.value = false;
		emit("quitEdit")
	}

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

<style>
</style>

