<template>
	<div class="game-maintenance-container">
		<el-card shadow="hover" header="复制账号">
			<template #header>
				复制账号
				<el-button v-show="item.copyShow == false" @click="item.copyShow = true" class="button-caret" type="text">
					<el-icon>
						<ele-CaretBottom />
					</el-icon>
				</el-button>
				<el-button v-show="item.copyShow == true" @click="item.copyShow = false" class="button-caret" type="text">
					<el-icon>
						<ele-CaretTop />
					</el-icon>
				</el-button>
			</template>
			<div v-show="item.copyShow">
				<div class="game-order-search mb15">
					<el-form label-position="right" label-width="300px" style="max-width: 600px">
						<el-form-item label="原账号">
							<el-input v-model="tableData.data.SrcName" placeholder="请填写uid" class="w-50 m-2" clearable />
						</el-form-item>
						<!--						<el-form-item label="原账号服务器">-->
						<!--							<el-select v-model="tableData.data.SrcServer" class="m-2" placeholder="选择服务器" filterable>-->
						<!--								<el-option v-for="item in servers" :key="item.id" :label="item.name" :value="item.id" />-->
						<!--							</el-select>-->
						<!--						</el-form-item>-->
						<el-form-item label="原账号服务器" prop="SrcServer">
							<el-select v-model="serverSwitch" class="m-2" placeholder="选择服务器" size="large" style="width: 80px">
								<el-option v-for="item in serverCategorize" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
							<el-select v-model="tableData.data.SrcServer" class="m-2" placeholder="选择服务器" size="large" filterable>
								<el-option v-for="item in switchServer()" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
						<el-form-item>
							<el-button size="large" type="primary" class="ml10" @click="copyUnit"> 复制</el-button>
						</el-form-item>
						<el-form-item>
							<el-button size="default" type="primary" @click="onDownload(filename)" v-if="!disabled" :loading="loading">
								<div v-if="loading">正在复制账号</div>
								<div v-else>下载账号json数据</div>
							</el-button>
						</el-form-item>
						<el-form-item inlist="true">
							<el-button size="default" type="primary" @click="onDownload(storeFilename)" v-if="!disabled" :loading="loading1">
								<div v-if="loading1">正在复制仓库</div>
								<div v-else>下载仓库json数据</div>
							</el-button>
						</el-form-item>
					</el-form>
				</div>
			</div>
		</el-card>

		<el-card shadow="hover" header="载入账号信息">
			<template #header>
				上载账号信息
				<el-button v-show="item.copyShow == false" @click="item.copyShow = true" class="button-caret" type="text">
					<el-icon>
						<ele-CaretBottom />
					</el-icon>
				</el-button>
				<el-button v-show="item.copyShow == true" @click="item.copyShow = false" class="button-caret" type="text">
					<el-icon>
						<ele-CaretTop />
					</el-icon>
				</el-button>
			</template>
			<div v-show="item.copyShow">
				<div class="game-order-search mb15">
					<el-form label-position="right" label-width="300px" style="max-width: 600px">
						<el-upload
							class="upload-demo"
							:action="baseURL + 'api/v1/pub/upload/singleFile'"
							:on-preview="handlePreview"
							:on-remove="handleRemove"
							:on-change="handleChange"
							:before-remove="beforeRemove"
							multiple
							:limit="2"
							:on-exceed="handleExceed"
							:file-list="fileList"
						>
							<el-button size="small" type="primary">点击上传</el-button>只能上传Json文件
						</el-upload>
						<el-form-item label="文件名称">
							<el-select v-model="tableData.data.filename" class="m-2" placeholder="选择载入文件" style="width: 300px" filterable>
								<el-option v-for="item in tableData.fileNames" :key="item" :label="item" :value="item" />
							</el-select>
						</el-form-item>
						<el-form-item label="仓库文件名称">
							<el-select v-model="tableData.data.storeFilename" class="m-2" placeholder="选择载入文件" style="width: 300px" filterable>
								<el-option v-for="item in tableData.fileNames" :key="item" :label="item" :value="item" />
							</el-select>
						</el-form-item>
						<el-form-item label="目标账号">
							<el-input v-model="tableData.data.DestName" placeholder="请填写uid" class="w-50 m-2" clearable />
						</el-form-item>
						<el-form-item label="目标服务器" prop="DestServer">
							<el-select v-model="serverSwitch" class="m-2" placeholder="选择服务器" size="large" style="width: 80px">
								<el-option v-for="item in serverCategorize" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
							<el-select v-model="tableData.data.DestServer" class="m-2" placeholder="选择服务器" size="large" filterable>
								<el-option v-for="item in switchServer()" :key="item.id + ''" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
						<el-form-item>
							<el-button size="large" type="primary" class="ml10" @click="updateUnit"> 更新</el-button>
						</el-form-item>
					</el-form>
				</div>
			</div>
		</el-card>
		<el-card shadow="hover" header="删除账号">
			<template #header>
				删除账号
				<el-button v-show="item.delShow == false" @click="item.delShow = true" class="button-caret" type="text">
					<el-icon>
						<ele-CaretBottom />
					</el-icon>
				</el-button>
				<el-button v-show="item.delShow == true" @click="item.delShow = false" class="button-caret" type="text">
					<el-icon>
						<ele-CaretTop />
					</el-icon>
				</el-button>
			</template>
			<div v-show="item.delShow">
				<div class="game-order-search mb15">
					<el-form label-position="right" label-width="300px" style="max-width: 600px">
						<el-form-item label="账号">
							<el-input v-model="tableData.del.SrcName" placeholder="" class="w-50 m-2" clearable />
						</el-form-item>
						<el-form-item label="账号服务器">
							<el-select v-model="serverSwitch" class="m-2" placeholder="选择服务器" size="large" style="width: 80px">
								<el-option v-for="item in serverCategorize" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
							<el-select v-model="tableData.del.serverId" class="m-2" placeholder="选择服务器" filterable>
								<el-option v-for="item in switchServer()" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" class="ml10" @click="delAccount"> 删除账号</el-button>
						</el-form-item>
					</el-form>
				</div>
			</div>
		</el-card>
		<el-card shadow="hover">
			<template #header>
				修改游戏登录密码
				<el-button v-show="item.pwdShow == false" @click="item.pwdShow = true" class="button-caret" type="text">
					<el-icon>
						<ele-CaretBottom />
					</el-icon>
				</el-button>
				<el-button v-show="item.pwdShow == true" @click="item.pwdShow = false" class="button-caret" type="text">
					<el-icon>
						<ele-CaretTop />
					</el-icon>
				</el-button>
			</template>
			<div class="game-order-search mb15" v-show="item.pwdShow">
				<el-form label-position="right" label-width="300px" style="max-width: 600px">
					<el-form-item label="account:">
						<el-input v-model="tableData.password.account" placeholder="" class="w-50 m-2" size="large" clearable />
					</el-form-item>
					<el-form-item label="账号服务器">
						<el-select v-model="serverSwitch" class="m-2" placeholder="选择服务器" size="large" style="width: 80px">
							<el-option v-for="item in serverCategorize" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
						<el-select v-model="tableData.password.serverId" class="m-2" placeholder="选择服务器" filterable>
							<el-option v-for="item in switchServer()" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
					<el-form-item label="密码:">
						<el-input v-model="tableData.password.pwd" placeholder="" class="w-50 m-2" size="large" clearable />
					</el-form-item>
					<el-form-item>
						<el-button size="large" type="primary" class="ml10" @click="resetPwd"> 修改登录密码</el-button>
					</el-form-item>
				</el-form>
			</div>
		</el-card>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, defineComponent, warn } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  gameGMClearResource,
  gameGMCopyUnit,
  gameGMUpdateUnit,
  gameGMDelAccount,
  gameResetPwd,
  giftExchangeExists
} from '/@/api/game';
import { gSwitchServer } from '/@/utils/utils';
import { AllServerList, ServerCategorize, serverList } from '/@/utils/game';

const baseURL: string | undefined | boolean = import.meta.env.VITE_API_URL;

interface TableData {
	filename: string;
	storeFilename: string;
	SrcName: number;
	SrcServer: number;
	DestName: string;
	DestServer: number;
}

interface DelData {
	SrcName: string;
	serverId: number;
  password: string;
}

interface TableDataState {
	item: { copyShow: boolean; delShow: boolean; pwdShow: boolean; resourceShow: boolean };
	tableData: {
		data: TableData;
		del: DelData;
		password: {
			account: string;
			serverId: number;
			pwd: string;
		};
		resource: {
			account: string;
			serverId: number;
		};
		fileNames: string[];
	};
	servers: object[];
	disabled: boolean;
	loading: boolean;
	loading1: boolean;
	fileList: object[];
	filename: string;
	storeFilename: string;
	serverSwitch: number;
	serverCategorize: object;
}

var timeInterval;
var timeInterval1;
export default defineComponent({
	name: 'apiV1GameMangeCopyUnit',
	setup() {
		const state = reactive<TableDataState>({
			serverSwitch: 0,
			fileList: [],
			baseURL: baseURL,
			item: { copyShow: true, delShow: false, pwdShow: false, resourceShow: false },
			tableData: {
				data: {
					SrcName: '',
					SrcServer: '',
				},
				del: {
					SrcName: '',
					serverId: '',
          password: '',
				},
				password: {
					account: '',
					serverId: '',
					pwd: '',
				},
				resource: {
					account: '',
				},
				fileNames: [],
			},
			servers: AllServerList,
			disabled: true,
			loading: false,
			loading1: false,
			filename: '',
			storeFilename: '',
			serverCategorize: ServerCategorize,
		});
		const handleRemove = (file, fileList) => {
			console.log('handleRemove: ', file, fileList);
		};
		const handlePreview = (file) => {
			console.log('handlePreview: ', file);
		};
		const handleExceed = (files, fileList) => {
			console.log('handleExceed: ', files, fileList);
			warn(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
		};
		const handleChange = (files, fileList) => {
			console.log('handleChange: ', files, fileList);
			if (state.tableData.fileNames.indexOf(files.name) >= 0) {
				return;
			}
			warn(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
			state.tableData.data.filename = files.name;
			state.tableData.fileNames.push(files.name);
		};
		const beforeRemove = (file, fileList) => {
			console.log('beforeRemove: ', file, fileList);
			return confirm(`确定移除 ${file.name}？ ${fileList.length}`);
		};
		const copyUnit = () => {
			console.log('state.tableData.data: ', state.tableData.data);
			// ElMessage.success('暂停使用');
			// return
			gameGMCopyUnit({
				SrcName: state.tableData.data.SrcName,
				SrcServer: state.tableData.data.SrcServer,
			})
				.then((res) => {
					ElMessage.success('修改成功');
					console.log('copy: ', res);
					state.tableData.data.filename = res.data.filename;
					state.tableData.data.storeFilename = res.data.storeFilename;
					state.tableData.fileNames.push(res.data.filename);
					state.tableData.fileNames.push(res.data.storeFilename);
					if (res.data.filename != '') {
						state.filename = res.data.filename;
						state.storeFilename = res.data.storeFilename;
						state.loading = true;
						state.loading1 = true;
						state.disabled = false;
						console.log('gameRandomGenerateCode: ', res, state.filename, state.storeFilename);
						timeInterval = setInterval(function () {
							checkGiftExchangeExists();
						}, 1000);
						timeInterval1 = setInterval(function () {
							checkGiftExchangeExists1();
						}, 1000);
					}
				})
				.finally(() => {});
		};

		const updateUnit = () => {
			console.log('state.tableData.data: ', state.tableData.data);
			// ElMessage.success('暂停使用');
			// return
			gameGMUpdateUnit({
				filename: state.tableData.data.filename,
				storeFilename: state.tableData.data.storeFilename,
				DestName: state.tableData.data.DestName,
        DestServer: state.tableData.data.DestServer,
			})
				.then((res) => {
					ElMessage.success('修改成功');
					console.log('update: ', res);
				})
				.finally(() => {});
		};
		const delAccount = () => {
			ElMessageBox.prompt(`此操作将删除账号：“${state.tableData.del.SrcName}”，是否继续? 请输入密码`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
        inputErrorMessage: 'Invalid password',
				// type: 'warning',
			}).then(({value}) => {
        if(value == 0){
          return
        }
        state.tableData.del.password = value
        console.log('state.tableData.data: ', state.tableData.del);
				gameGMDelAccount(state.tableData.del)
					.then(() => {
						ElMessage.success('删除成功');
					})
					.finally(() => {});
			});
		};
		const resetPwd = () => {
			gameResetPwd(state.tableData.password)
				.then(() => {
					ElMessage.success('修改密码成功！');
				})
				.finally(() => {});
		};
		const clearResource = () => {
			console.log('state.tableData.resource: ', state.tableData.resource);
			gameGMClearResource(state.tableData.resource)
				.then(() => {
					ElMessage.success('删除成功');
				})
				.finally(() => {});
		};
		onMounted(() => {
			//	initTableData();
			serverList().then((res) => {
				state.servers = res;
				// console.log(" server : ", state.servers);
			});
		});
		const checkGiftExchangeExists = () => {
			giftExchangeExists({ file: state.filename }).then((response) => {
				console.log('checkGiftExchangeExists', state.filename, response);
				if (response.data.state == 1) {
					clearInterval(timeInterval);
					state.loading = false;
				}
			});
		};
		const checkGiftExchangeExists1 = () => {
			giftExchangeExists({ file: state.storeFilename }).then((response) => {
				console.log('checkGiftExchangeExists1', state.storeFilename, response);
				if (response.data.state == 1) {
					clearInterval(timeInterval1);
					state.loading1 = false;
				}
			});
		};
		const onDownload = (file: string) => {
			if (file) {
				window.open(baseURL + 'upload_file?filename=' + file);
			}

			// state.disabled = true;
		};
		// 打开修改字典弹窗
		const switchServer = () => {
			return gSwitchServer(state.serverSwitch, state.servers);
		};
		return {
			switchServer,
			onDownload,
			handleRemove,
			handleChange,
			handlePreview,
			handleExceed,
			beforeRemove,
			resetPwd,
			copyUnit,
			updateUnit,
			delAccount,
			clearResource,
			...toRefs(state),
		};
	},
});
</script>
