<template>
	<div>
		<el-dialog :title="(isNew ? '新增' : '修改') + setName.label + '模式:'" v-model="isShowDialog">
			<el-form label-width="130px" label-position="right">
				<el-form-item label="渠道:">
					<el-select v-model="tableData.param.channel" class="m-2">
						<el-option v-for="item in channels" :key="item.id + ''" :label="item.label" :value="item.value + ''" />
					</el-select>
				</el-form-item>
				<el-form-item label="版本号:">
					<el-input v-model="tableData.param.version" class="m-2"></el-input>
				</el-form-item>
				<el-form-item :label="setName.label + '模式:'">
					<el-radio-group v-model="tableData.param.auditMode">
						<el-radio label="1">是</el-radio>
						<el-radio label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button size="default" type="primary" class="ml10" @click="addVersion">
						<el-icon>
							<ele-Promotion />
						</el-icon>
						发送
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent } from 'vue';
import { allChannelList } from '/src/utils/game';
import { ElMessage, ElMessageBox } from 'element-plus';
import { gameSetAuditMode } from '/src/api/game/set';

interface TableData {
	name: string;
	channel: string;
	version: string;
	auditMode: number;
}

interface TableDataState {
	channels: Array<{ label: string; value: string }>;
	isShowDialog: boolean;
	tableData: {
		param: TableData;
	};
	setName: object;
	isNew: boolean;
}

export default defineComponent({
	name: 'gameEditClientBug',
	setup(prop, { emit }) {
		const state = reactive<TableDataState>({
			channels: [],
			isShowDialog: false,
			tableData: {
				param: {
					name: '',
					channel: '',
					version: '',
					auditMode: '1',
				},
			},
			setName: {},
			isNew: false,
		});

		// 打开弹窗
		const openDialog = (row: TableData, setName, isNew) => {
			console.log(row);
			state.setName = setName;
			state.isNew = isNew;
			allChannelList().then((value) => {
				state.channels = value;
				state.tableData.param = row;
				state.isShowDialog = true;
			});
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const addVersion = () => {
			closeDialog();
			ElMessageBox.confirm(`此操作将修改审核模式，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					gameSetAuditMode(state.tableData.param).then(() => {
						ElMessage.success('修改成功');
						emit('mailList');
					});
				})
				.catch(() => {});
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			addVersion,
			...toRefs(state),
		};
	},
});
</script>
