// 许可⻚
<template>
	<div class="license-wrapper">
		<!-- 许可信息 -->
		<div class="license-info-box">
			<a-descriptions v-if="!validatenull(licenseData)" />
			<div v-else class="license-no-info">
				{{
					getI18n(
						'system',
						'SYSTEM_License_Product_Not_Configured_With_License'
					)
				}}
			</div>
		</div>

		<!-- 许可配置 -->
		<a-divider orientation="left" :plain="true">{{
			getI18n('system', 'SYSTEM_License_Activation_Update')
		}}</a-divider>
		<div class="license-config-box">
			<a-form
				:model="formState"
				:label-col="{ span: 6 }"
				:wrapper-col="{ span: 18 }"
				style="height: 270px"
				ref="formRef"
			>
				<a-row>
					<a-col :span="24">
						<!-- 许可类型 -->
						<a-form-item
							:label="getI18n('system', 'SYSTEM_License_Type')"
							name="type"
						>
							<a-radio-group v-model:value="formState.type">
								<a-radio
									:style="radioStyle"
									v-for="i in Object.values(
										LicenseConstatnt.licenseType
									)"
									:key="i.value"
									:value="i.value"
								>
									{{ i.label }}
								</a-radio>
							</a-radio-group>
						</a-form-item>
					</a-col>

					<a-col :span="24">
						<!-- 许可类型描述 -->
						<a-form-item
							:label="
								getI18n(
									'system',
									'SYSTEM_License_Type_Description'
								)
							"
							name="desc"
						>
							{{
								getI18n(
									'system',
									'SYSTEM_License_Type_Description_Content'
								)
							}}
							<span
								v-if="
									formState.type ==
									LicenseConstatnt.licenseType.fixed.value
								"
							>
								{{
									getI18n(
										'system',
										'SYSTEM_License_Type_Description_Content1'
									)
								}}
							</span>
							<!-- 产品试用许可申请 地址 -->
							<a
								v-if="
									formState.type ==
									LicenseConstatnt.licenseType.fixed.value
								"
								:href="licenseUrl"
								target="_blank"
								style="display: inline-block; padding: 0 3px"
								>{{
									getI18n(
										'system',
										'SYSTEM_License_Product_Trial_License_Application'
									)
								}}</a
							>

							{{
								getI18n(
									'system',
									'SYSTEM_License_Type_Description_Content2'
								)
							}}

							<!-- 产品支持人员 -->
							<a-popover title="" trigger="hover">
								<template #content>
									<a-image
										:width="200"
										:height="200"
										:src="'/src/assets/images/wx-support.png'"
										fallback="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMIAAADDCAYAAADQvc6UAAABRWlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSSwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAwSDCIMogwMCcmFxc4BgQ4ANUwgCjUcG3awyMIPqyLsis7PPOq3QdDFcvjV3jOD1boQVTPQrgSkktTgbSf4A4LbmgqISBgTEFyFYuLykAsTuAbJEioKOA7DkgdjqEvQHEToKwj4DVhAQ5A9k3gGyB5IxEoBmML4BsnSQk8XQkNtReEOBxcfXxUQg1Mjc0dyHgXNJBSWpFCYh2zi+oLMpMzyhRcASGUqqCZ16yno6CkYGRAQMDKMwhqj/fAIcloxgHQqxAjIHBEugw5sUIsSQpBobtQPdLciLEVJYzMPBHMDBsayhILEqEO4DxG0txmrERhM29nYGBddr//5/DGRjYNRkY/l7////39v///y4Dmn+LgeHANwDrkl1AuO+pmgAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAwqADAAQAAAABAAAAwwAAAAD9b/HnAAAHlklEQVR4Ae3dP3PTWBSGcbGzM6GCKqlIBRV0dHRJFarQ0eUT8LH4BnRU0NHR0UEFVdIlFRV7TzRksomPY8uykTk/zewQfKw/9znv4yvJynLv4uLiV2dBoDiBf4qP3/ARuCRABEFAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnY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TAyu4kDYB2YxUzwg0gi/41ztHnfQG26HbGel/crVrm7tNY+/1btkOEAZ2M05r4FB7r9GbAIdxaZYrHdOsgJ/wCEQY0J74TmOKnbxxT9n3FgGGWWsVdowHtjt9Nnvf7yQM2aZU/TIAIAxrw6dOnAWtZZcoEnBpNuTuObWMEiLAx1HY0ZQJEmHJ3HNvGCBBhY6jtaMoEiJB0Z29vL6ls58vxPcO8/zfrdo5qvKO+d3Fx8Wu8zf1dW4p/cPzLly/dtv9Ts/EbcvGAHhHyfBIhZ6NSiIBTo0LNNtScABFyNiqFCBChULMNNSdAhJyNSiECRCjUbEPNCRAhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiECRCjUbEPNCRAhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiECRCjUbEPNCRAhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiEC/wGgKKC4YMA4TAAAAABJRU5ErkJggg=="
									/>
								</template>
								<span
									style="
										display: inline-block;
										padding: 0 3px;
										color: #1890ff;
										cursor: pointer;
									"
									>{{
										getI18n(
											'system',
											'SYSTEM_License_Product_Support_Personnel'
										)
									}}</span
								>
							</a-popover>

							{{
								getI18n(
									'system',
									'SYSTEM_License_Type_Description_Content3'
								)
							}}
						</a-form-item>
					</a-col>

					<template
						v-if="
							formState.type ==
							LicenseConstatnt.licenseType.fixed.value
						"
					>
						<a-col :span="24">
							<!-- 设备信息 -->
							<a-form-item
								:label="
									getI18n(
										'system',
										'SYSTEM_License_Device_Information'
									)
								"
								name="deviceInfo"
							>
								<a-input
									v-model:value="formState.deviceInfo"
									readonly
									desabled
								/>
								<a-button
									type="primary"
									style="margin-top: 5px"
									@click="onCopyClick"
									>{{
										getI18n('system', 'SYSTEM_License_Copy')
									}}</a-button
								>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<!-- 许可文件 -->
							<a-form-item
								:label="
									getI18n('system', 'SYSTEM_License_File')
								"
								name="file"
							>
								<a-upload
									v-if="customFileList.length == 0"
									name="file"
									:beforeUpload="onImportClick"
								>
									<a-button>{{
										getI18n(
											'system',
											'SYSTEM_License_Upload_'
										)
									}}</a-button>
								</a-upload>
								<show-attachment-list
									v-else
									:file-list="customFileList"
									:isShowDownload="false"
									@on-delete-click="onDelFileClick"
								/>
							</a-form-item>
						</a-col>
					</template>
					<template v-else>
						<!-- 许可服务连接密文 -->
						<a-col :span="24">
							<a-form-item
								:label="
									getI18n(
										'system',
										'SYSTEM_License_Service_Connection_Ciphertext'
									)
								"
								name="licenseString"
							>
								<a-input-password
									readonly
									desabled
									v-model:value="formState.licenseString"
									:placeholder="
										getI18n(
											'system',
											'SYSTEM_License_Service_Connection_Ciphertext'
										)
									"
								/>
							</a-form-item>
						</a-col>
					</template>
				</a-row>
			</a-form>

			<!-- 按钮 -->
			<div class="license-button-wrapper">
				<!-- 导入许可,导入连接 -->
				<a-button
					type="primary"
					style="margin-right: 1em"
					@click="onUploadLicenseClick"
					>{{
						getI18n(
							'system',
							`${
								formState.type ==
								LicenseConstatnt.licenseType.fixed.value
									? 'SYSTEM_License_Import_File'
									: 'SYSTEM_License_Import_Connection'
							}`
						)
					}}</a-button
				>
				<!-- 继续访问 -->
				<a-button @click="onContinueVisitClick">{{
					getI18n('system', 'SYSTEM_License_Continue_Visit')
				}}</a-button>
			</div>
		</div>
	</div>
</template>
<script lang="ts">
import router from '@/router'
import { message } from 'ant-design-vue'
import { defineComponent, ref, onMounted, reactive } from 'vue'
import { validatenull } from '@/utils/tool/translateValue'
import { getI18n } from '@/utils/i18n'
import { LicenseConstatnt } from '@/utils/constant'
import useClipboard from 'vue-clipboard3'

import {
	getMachineCodeApi,
	uploadLicenseApi,
	uploadCiphertextApi,
	getLicenseTnformationApi,
} from '@/api/license'

import Storage from 'web-storage-cache'
const localStorage = new Storage()
// 复制
interface copyModel {
	action?: string // 'copy'
	text?: string // 复制的文本
}

import { ShowAttachmentList } from '@/views/components'
interface FileItem {
	uid: string
	name?: string
	status?: string
	response?: string
	url?: string

	lastModified?: number
	lastModifiedDate?: any
	size?: number
	type?: string
	webkitRelativePath?: string
}

export default defineComponent({
	name: 'License40399',
	components: {
		ShowAttachmentList,
	},
	props: {},
	emits: [],
	setup() {
		const formState = reactive<{
			type: string
			deviceInfo: string
			licenseString?: string
		}>({
			type: LicenseConstatnt.licenseType.fixed.value,
			deviceInfo: '',
			licenseString:
				's/LJdzR/d5/rJJQ9kIDxlWpDFIfFl2PDOpLLdZqDKFC/3RFDR7ArBAxePLOYAcE0P+nkIbbgXlEdlf+PxK1xuTmxm4g5897uxiKVCU7kFFakD91pc8p4NJb970Q16vUrVmYbvJ4YpOAoO6V5/lHs4A==',
		})
		const licenseData = ref()

		// 复制机器码
		const { toClipboard } = useClipboard()
		const onCopyClick = async () => {
			try {
				await toClipboard(formState.deviceInfo).then(
					(res: copyModel) => {
						if (!validatenull(res) && res.text) {
							message.success(
								getI18n('system', 'SYSTEM_License_Copy_Success')
							)
						}
					}
				)
			} catch (e: any) {
				e && message.warning(e)
			}
		}

		// 获取机器码
		const getMachineCode = async () => {
			const res = await getMachineCodeApi()
			if (!validatenull(res.data)) {
				formState.deviceInfo = res.data
			}
		}

		const customFileList = ref<FileItem[]>([])
		const importDataTemp = ref<string | Blob>('')
		// 上传
		const onImportClick = async (data: any | Blob) => {
			const reg = new RegExp(/^lic$/gi)
			if (
				data.name &&
				reg.test(data.name.substring(data.name.lastIndexOf('.') + 1))
			) {
				importDataTemp.value = data
				customFileList.value = [
					...[
						{
							uid: data.uid,
							name: data.name,
							status: 'done',
							response: '{"status": "success"}', // 服务端响应内容
							url: '',
							lastModified: data.lastModified,
							lastModifiedDate: data.lastModifiedDate,
							size: data.size,
							type: data.type,
							webkitRelativePath: data.webkitRelativePath,
						},
					],
				]
				message.success(getI18n('common', 'Common_Success'))
			} else {
				message.warning('请导入.lic类型的许可文件！')
			}
		}

		// 附件删除
		const onDelFileClick = () => {
			customFileList.value = []
			importDataTemp.value = ''
		}

		// 导入许可
		const onUploadLicenseClick = async () => {
			if (!importDataTemp.value) {
				message.warning(
					getI18n(
						'system',
						'SYSTEM_License_Upload_License_File_And_Import'
					)
				)
				return
			}
			// 许可类型： 固定许可
			if (formState.type == LicenseConstatnt.licenseType.fixed.value) {
				await uploadLicenseApi(importDataTemp.value)
				message.success(getI18n('common', 'Common_Success'))
				// .then( res => {
				// 	if( !validatenull(res) && res.code == 200 ){
				// 		message.success(getI18n('common', 'Common_Success'))
				// 	}
				// })
			} else {
				await uploadCiphertextApi(formState.licenseString || '')
				message.success(getI18n('common', 'Common_Success'))
				// .then( res => {
				// 	if( !validatenull(res) && res.code == 200 ){
				// 		message.success(getI18n('common', 'Common_Success'))
				// 	}
				// })
			}
		}

		// 继续访问
		const onContinueVisitClick = () => {
			const url = localStorage.get('continueVisitUrl')
			if (url) {
				router.replace({ path: url })
				localStorage.delete('continueVisitUrl')
			} else {
				router.push({ name: 'ProcessManagementList' })
			}
		}

		// 获取许可信息
		const getLicenseTnformation = async () => {
			await getLicenseTnformationApi().then(res => {
				res
			})
		}

		const licenseUrl = ref<string>('')
		onMounted(() => {
			getMachineCode()
			getLicenseTnformation()

			licenseUrl.value = window['License_APPLICATION_URL']
		})

		return {
			formState,
			LicenseConstatnt,
			licenseData,
			validatenull,
			getI18n,
			radioStyle: {
				display: 'inline-block',
				height: '30px',
				lineHeight: '30px',
			},

			customFileList,
			onDelFileClick,
			onImportClick,
			onCopyClick,
			onUploadLicenseClick,
			onContinueVisitClick,

			licenseUrl,
		}
	},
})
</script>
