<template>
    <el-dialog :title="t('游戏管理设置')" v-model="open" width="800px" append-to-body align-center>
        <el-form ref="formRef" :model="formAll" :rules="rules" label-width="260px" class="add-form">
            <el-form-item :label="t('返回大厅按钮')" prop="returnHome">
                <el-radio-group v-model="formAll.returnHome">
                    <el-radio :value="1">{{ t('二次弹窗确认(默认)') }}</el-radio>
                    <el-radio :value="2">{{ t('直接返回') }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('已首充才能进入游戏(未充值无法进入)')" prop="needRecharge">
                <el-radio-group v-model="formAll.needRecharge">
                    <el-radio :value="false">{{ t('不限制(默认)') }}</el-radio>
                    <el-radio :value="true">{{ t('开启') }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('强制要求下载APP')" prop="forceDownload">
                <el-radio-group v-model="formAll.forceDownload">
                    <el-radio :value="false">{{ t('不限制(默认)') }}</el-radio>
                    <el-radio :value="true">{{ t('开启(只能在APP游戏)') }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('弹窗提示语')" prop="forceType">
                <el-radio-group v-model="formAll.forceType">
                    <el-radio :value="1">{{ t('系统自带') }}</el-radio>
                    <el-radio :value="2">{{ t('自定义') }}</el-radio>
                </el-radio-group>
                <div v-if="formAll.forceType == 1" style="width: 100%;">
                    <div style="width: 100%;">
                        <el-tabs v-model="forcePopNameLang" class="demo-tabs" style="width: 90%;">
                            <el-tab-pane v-for="tab in langList" :name="tab?.value">
                                <template #label>
                                    {{ tab?.label }}
                                    <!-- <el-icon v-if="isDotVisible(tab)" style="margin-left: 4px;" color="rgb(245, 108, 108)"><WarningFilled /></el-icon> -->
                                </template>
                            </el-tab-pane>
                        </el-tabs>
                    </div>
                    <div v-for="(item,index) in formAll.forcePopPromptDefault" style="width: 100%;">
                        <div v-if="forcePopNameLang == item.lang" :contenteditable="false" class="textarea-like" :disabled="true"  v-html="item.msg" :placeholder="t('请输入规则说明')"></div>
                    </div>
                </div>
                <div v-if="formAll.forceType == 2" style="width: 100%;margin-top: 10px;margin-right: 10px;">
                    <div style="width: 100%;">
                        <el-tabs v-model="forcePopNameLang" class="demo-tabs" style="width: 90%;">
                            <el-tab-pane v-for="tab in langList" :name="tab?.value">
                                <template #label>
                                    {{ tab?.label }}
                                    <!-- <el-icon v-if="isDotVisible(tab)" style="margin-left: 4px;" color="rgb(245, 108, 108)"><WarningFilled /></el-icon> -->
                                </template>
                            </el-tab-pane>
                        </el-tabs>
                    </div>
                    <div v-for="(item,index) in formAll.forcePopPrompt" style="width: 100%;">
                        <el-input v-if="forcePopNameLang == item.lang" v-model="item.msg" type="textarea" style="margin-top: 10px;" :rows="5" :placeholder="t('请输入')" maxlength="1000" show-word-limit />
                    </div>
                 </div>
            </el-form-item>
            <!-- <el-form-item :label="t('仅限已首充会员强制下载APP')" prop="gameRechargeDownloadApp">
                <el-radio-group v-model="formAll.gameRechargeDownloadApp">
                    <el-radio value="2">{{ t('开启(仅限已首充会员)') }}</el-radio>
                    <el-radio value="1">{{ t('关闭(全部限制)') }}</el-radio>
                </el-radio-group>
            </el-form-item> -->
            <!-- <el-form-item :label="t('仅限Android设备强制下载APP')" prop="gameAndroidDownloadApp">
                <el-radio-group v-model="formAll.gameAndroidDownloadApp">
                    <el-radio value="2">{{ t('开启(推荐)') }}</el-radio>
                    <el-radio value="1">{{ t('关闭(所有设备都限制)') }}</el-radio>
                </el-radio-group>
            </el-form-item> -->
            <el-form-item :label="t('充值次数触发强制转原生APP')" prop="rechargeCnt">
               <NumberInput v-model="formAll.rechargeCnt" style="width: 120px;"></NumberInput><span style="margin-left: 5px;">{{ t('次') }}</span>
            </el-form-item>
            <el-form-item :label="t('转原生提示语')" prop="rechargeType">
                <el-radio-group v-model="formAll.rechargeType">
                    <el-radio :value="1">{{ t('系统自带') }}</el-radio>
                    <el-radio :value="2">{{ t('自定义') }}</el-radio>
                </el-radio-group>
                <div v-if="formAll.rechargeType == 1" style="width: 100%;">
                    <div style="width: 100%;">
                        <el-tabs v-model="activeNameLang" class="demo-tabs" style="width: 90%;">
                            <el-tab-pane v-for="tab in langList" :name="tab?.value">
                                <template #label>
                                    {{ tab?.label }}
                                    <!-- <el-icon v-if="isDotVisible(tab)" style="margin-left: 4px;" color="rgb(245, 108, 108)"><WarningFilled /></el-icon> -->
                                </template>
                            </el-tab-pane>
                        </el-tabs>
                    </div>
                    <div style="width: 100%;" v-for="(item,index) in formAll.rechargePopPromptDefault">
                        <div v-if="activeNameLang == item.lang" :contenteditable="false" class="textarea-like" :disabled="true"  v-html="item.msg" :placeholder="t('请输入规则说明')"></div>
                    </div>
                </div>
                <div v-if="formAll.rechargeType == 2" style="width: 100%;margin-top: 10px;margin-right: 10px;">
                    <div style="width: 100%;">
                        <el-tabs v-model="activeNameLang" class="demo-tabs" style="width: 90%;">
                            <el-tab-pane v-for="tab in langList" :name="tab?.value">
                                <template #label>
                                    {{ tab?.label }}
                                    <!-- <el-icon v-if="isDotVisible(tab)" style="margin-left: 4px;" color="rgb(245, 108, 108)"><WarningFilled /></el-icon> -->
                                </template>
                            </el-tab-pane>
                        </el-tabs>
                    </div>
                    <div v-for="(item,index) in formAll.rechargePopPrompt" style="width: 100%;">
                        <el-input v-if="activeNameLang == item.lang" v-model="item.msg" type="textarea" style="margin-top: 10px;" :rows="5" :placeholder="t('请输入')" maxlength="1000" show-word-limit />
                    </div>
                 </div>
            </el-form-item>
            <el-form-item :label="t('游戏只显示一行,超过使用省略号展示')" prop="nameExceeds">
                <el-radio-group v-model="formAll.nameExceeds">
                    <el-radio :value="false">{{ t('关闭(默认)') }}</el-radio>
                    <el-radio :value="true">{{ t('开启') }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-text>
                <el-icon color="#ff9900">
                    <InfoFilled />
                </el-icon>
                {{ t("功能说明：此功能将进行全局限制，虽然可以提高马甲包用户的留存率，但可能影响用户体验；与渠道链接的限制重复，建议可设置在渠道链接，按渠道设置马甲包来源，并限制防止用户流失。") }}
            </el-text>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" v-h="['game:config:add']" @click="submitForm" :loading="loadingButton">{{ t('确 定') }}</el-button>
                <el-button @click="cancel">{{ t('取 消') }}</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup>
import * as platform from "@/api/game/platform";
import NumberInput from "@/components/NumberInput"; // 数字输入框
import CustomSelect from "@/components/CustomSelect"; // 下拉框
const { proxy } = getCurrentInstance();
import { getLocalStorage } from "@/utils/auth";

const langListRaw = getLocalStorage('langList') || []

const langList = Array.from(
  new Map(
    langListRaw.map(item => [item.countryLang, {
      label: item.name,
      value: item.countryLang
    }])
  ).values()
)   // 语种选项
const emits = defineEmits([])
const open = ref(false);
const activeNameLang = ref(localStorage.getItem('lang')|| 'zh-CN');
const forcePopNameLang = ref(localStorage.getItem('lang')|| 'zh-CN');
const loadingButton = ref(false);
const oldForm = shallowRef({  });
const formAll = reactive({
    forceType:1,
    forcePopPromptDefault: [
        {
            lang:'',
            msg:'',
        }
    ],
    forcePopPrompt:[
        {
            lang:'',
            msg:''
        }
    ],
    rechargeType:1,
    rechargePopPromptDefault: [
        {
            lang:'',
            msg:'',
        }
    ],
    rechargePopPrompt:[
        {
            lang:'',
            msg:''
        }
    ],
})
const rules = {
    memberAccount: [{ required: true, message: proxy.t('请选择是否开启'), trigger: 'change' }],
    password: [{ required: true, message: proxy.t('请选择是否开启'), trigger: 'change' }],
    rechargeCnt: [{ required: true, message: proxy.t('请输入数字'), trigger: 'change' }],
}

const formRef = ref(null)
const submitForm = () => {
    proxy.$refs["formRef"].validate(valid => {
        if (valid) {
            loadingButton.value = true;
            if (JSON.stringify(formAll) != oldForm.value) {
                platform.postGameConfig(formAll).then(response => {
                    loadingButton.value = false;
                    proxy.$modal.msgSuccess(proxy.t('修改成功'));
                    cancel()
                }).catch(() => {
                    loadingButton.value = false;
                });
            }else{
                loadingButton.value = false;
                cancel()
            }
        }
    });
}

const showModal = () => {
    getConfig()
    open.value = true
}
const cancel = () => {
    open.value = false
}

const getConfig = async () => {
    await platform.gameConfig().then(res => {
        Object.assign(formAll, res.data);
        setTimeout(() => {
            let objForm =  { ...formAll }
            oldForm.value = JSON.stringify(objForm);
        }, 500);
    })
}
defineExpose({ showModal })
</script>
<style scoped lang="scss">
.textarea-like {
  width: 90%;
  height: 100px;
  min-height: 100px; /* 设置最小高度 */
  border: 1px solid #ccc;
  padding: 5px;
  overflow-y: auto; /* 添加滚动条 */
  resize: vertical; /* 允许垂直方向上拖动调整高度 */
}

.textarea-like[disabled] {
  background-color: #f5f5f5; /* 设置禁用时的背景颜色为灰色 */
  color: #aaa; /* 设置禁用时的文本颜色为灰色 */
  cursor: not-allowed; /* 设置禁用时的鼠标指针为禁止图标 */
}
</style>