<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-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane label="有效类型有效投注设置" name="bettingSettings"></el-tab-pane>
                <el-tab-pane label="不计入有效投注的有效(既排除)" name="validBets"></el-tab-pane>
            </el-tabs>
            <div v-if="activeName == 'bettingSettings'">
                <div class="formBetting-box">
                    <el-form-item :label="t('体育默认比例(0-100)')" prop="sportsRatio">
                        <NumberInput v-model="formAll.sportsRatio" style="width: 180px;">
                            <template #append>%</template>
                        </NumberInput>
                    </el-form-item>
                    <el-form-item label-width="0" class="clt-item">
                            <span slot="label" class="label-box">
                                {{ t('输赢绝对值计算有效投注') }}
                                <icon-tips width="300">  {{ t('开启后，以会员的输赢分绝对值作为有效投注，例如: 以香港盘，赔率0.7的盘口为例，A投注100，注单结果为输，输赢值-100， 则A的有效投注额为100。B投注100，注单结果为赢， 输赢值70，则B的有效投注额为70。') }}</icon-tips>
                            </span>
                        <div class="item-right">
                            <el-switch :active-value="true" @change="changeSwitch('sportsAbs')" :inactive-value="false"  style="margin-right: 10px; --el-switch-on-color: #13ce66;"
                            inline-prompt
                            active-text="开"
                            inactive-text="关"
                            v-model="formAll.sportsAbs" />
                        </div>
                    </el-form-item> 
                    <el-form-item label-width="0" class="clt-item">
                            <span slot="label" class="label-box">
                                {{ t('取最小值作为有效投注') }}
                                <icon-tips width="300">  {{ t('开启后，有效投注将在投注金额和输赢金额绝对值之间取小的值，举例: 以香港盘，赔率0.2盘口及赔率1.5盘口为例， D投注赔率0.2盘口500，注单结果为赢，输点值100，则D的有效投注额为100(即取输赢金额作为有效投注)。 E投注赔率15盘口500，注单结果为赢，输赢值750。则E的有效投注额为500(即取投注金额作为有效投注)。') }}</icon-tips>
                            </span>
                        <div class="item-right">
                            <el-switch :active-value="true" @change="changeSwitch('sportsMin')" :inactive-value="false" style="margin-right: 10px; --el-switch-on-color: #13ce66;"
                            inline-prompt
                            active-text="开"
                            inactive-text="关"
                            v-model="formAll.sportsMin" />
                        </div>
                    </el-form-item> 
                    <!-- <el-form-item label-width="0" class="clt-item" prop="sportsMaxOddsValue">
                            <span slot="label" class="label-box">
                                {{ t('按最大赔率计算有效投注') }}
                                <icon-tips width="300">  {{ t(`开启后，需要设定最大赔率，若会员为赢，且所赢的赔率大于倍率封顶值，则按封顶最大值作为有效投注。举例:
以香港盘，设定最大倍率封顶值为1.0，最终开奖赔率1.5的盘口为例，
C投注200，注单结果为赢，输赢值300，则有效投注额为200*1.0=200。
[按最大倍率计算有效投注」及「低赔率不计算有效投注】皆开启时，最大赔率设定值不可小于最小赔率的设定值。`) }}</icon-tips>
                            </span>
                        <div class="item-right">
                            <el-switch :active-value="true" :inactive-value="false" style="margin-right: 10px; --el-switch-on-color: #13ce66;"
                            inline-prompt
                            active-text="开"
                            inactive-text="关"
                            v-model="formAll.sportsMaxOdds" />
                            <NumberInput v-if="formAll.sportsMaxOdds" v-model="formAll.sportsMaxOddsValue" style="width: 180px;">
                                <template #append>%</template>
                            </NumberInput>
                        </div>
                    </el-form-item> 
                    <el-form-item label-width="0" class="clt-item" prop="sportsMinOddsValue">
                        <span slot="label" class="label-box">
                            {{ t('低赔率不计算有效投注') }}
                            <icon-tips width="300">  {{ t(`开启后，需要设定不计算有效投注的最小赔率，若会员为赢，且赔率小于等于此不计算有效投注的最小赔率，则该局有效投注直接按0计，以免通过低赔率刷流水。
举例:以香港盘，不计算有效投注的最小赔率设置为0.5，赔率为0.4的盘口为例，
F投注300，注单结果为赢，输赢值120，因赔率0.4小于所设置的最小值05，所以该局的有效投注为0(即不计入)。
*「按最大倍率计算有效投注」及「低赔率不计算有效投注」皆开启时，最大赔率设定值不可小于最小赔率的设定值。`) }}</icon-tips>
                        </span>
                        <div class="item-right">
                            <el-switch :active-value="true" :inactive-value="false" style="margin-right: 10px; --el-switch-on-color: #13ce66;"
                            inline-prompt
                            active-text="开"
                            inactive-text="关"
                            v-model="formAll.sportsMinOdds" />
                            <NumberInput v-if="formAll.sportsMinOdds" v-model="formAll.sportsMinOddsValue" style="width: 180px;">
                                <template #append>%</template>
                            </NumberInput>
                        </div>
                    </el-form-item>  -->
                </div>
                <el-form-item :label="t('棋牌默认比例(0-100)')" prop="cardRatio">
                    <NumberInput v-model="formAll.cardRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
                <el-form-item :label="t('捕鱼默认比例(0-100)')" prop="fishingRatio">
                    <NumberInput v-model="formAll.fishingRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
                <el-form-item :label="t('电子默认比例(0-100)')" prop="electronicRatio">
                    <NumberInput v-model="formAll.electronicRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
                <el-form-item :label="t('真人默认比例(0-100)')" prop="videoRatio">
                    <NumberInput v-model="formAll.videoRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
                <el-form-item :label="t('斗鸡默认比例(0-100)')" prop="cockfightingRatio">
                    <NumberInput v-model="formAll.cockfightingRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
                <el-form-item :label="t('电竞默认比例(0-100)')" prop="esportsRatio">
                    <NumberInput v-model="formAll.esportsRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
                <el-form-item :label="t('彩票默认比例(0-100)')" prop="lotteryRatio">
                    <NumberInput v-model="formAll.lotteryRatio" style="width: 180px;">
                        <template #append>%</template>
                    </NumberInput>
                </el-form-item>
            </div>
            <div v-if="activeName == 'validBets'">
                <platform-selection-tree style="width: 100%; margin-top: 5px;"
                    @selectPlatform="selectPlatform('auditPlatform',$event)"
                    :auditPlatform="formAll.auditPlatform"></platform-selection-tree>
            </div>
            <!-- <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>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" v-hasPermi="['game:turnover: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"; // 下拉框
import PlatformSelection from "@/components/PlatformSelection";     // 游戏平台选择
import PlatformSelectionTree from "@/components/PlatformSelectionTree";
import IconTips from "@/components/IconTips";  // 小图标提示
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 activeName = ref('bettingSettings');
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({
})
const validateNumberInRange = (rule, value, callback) => {
    const num = Number(value);
    if (isNaN(num)) {
        callback(new Error(proxy.t('请输入数字')));
    } else if (num < 0 || num > 100) {
        callback(new Error(proxy.t('请输入 0 到 100 之间的数字')));
    } else {
        callback();
    }
}
const rules = {
    sportsRatio: [
        { validator: validateNumberInRange, trigger: 'change' }
    ],
    cardRatio:  [
        { validator: validateNumberInRange, trigger: 'change' }
    ],
    fishingRatio:  [
        { validator: validateNumberInRange, trigger: 'change' }
    ],
    electronicRatio:  [
         { validator: validateNumberInRange, trigger: 'change' } 
    ],
    videoRatio:  [
         { validator: validateNumberInRange, trigger: 'change' } 
    ],
    cockfightingRatio:  [
         { validator: validateNumberInRange, trigger: 'change' } 
    ],
    esportsRatio:  [
         { validator: validateNumberInRange, trigger: 'change' }
    ],
    lotteryRatio:  [
         { validator: validateNumberInRange, trigger: 'change' } 
    ],
    sportsMaxOddsValue: [
     { validator: validateNumberInRange, trigger: 'change' } 
    ],
    sportsMinOddsValue: [
     { validator: validateNumberInRange, trigger: 'change' } 
    ]
}

const formRef = ref(null)
const submitForm = () => {
    proxy.$refs["formRef"].validate(valid => {
        if (valid) {
            loadingButton.value = true;
            let dataArr = JSON.parse(formAll.auditPlatform);
            let obj = { 
                ...formAll,
                gamePlatformTreeResultDTO:dataArr
            }
            delete obj.auditPlatform;
            if (JSON.stringify(obj) != oldForm.value) {
                platform.postGameTurnoverConfig(obj).then(response => {
                    loadingButton.value = false;
                    proxy.$modal.msgSuccess(proxy.t('修改成功'));
                    cancel()
                }).catch(() => {
                    loadingButton.value = false;
                });
            }else{
                loadingButton.value = false;
                cancel()
            }
        }
    });
}
const handleClick = (tab, event) => {
  
}
const changeSwitch = (val) => {
    if (val == 'sportsAbs'){
        if (formAll.sportsAbs) {
            formAll.sportsMin = false 
        }
    }else if (val == 'sportsMin'){ 
        if (formAll.sportsMin) {
            formAll.sportsAbs = false 
        }  
    }

}
const showModal = () => {
    getConfig()
    open.value = true
}
// 勾选的游戏平台
const selectPlatform = (type,val) => {
    formAll.auditPlatform = val;
}
const cancel = () => {
    open.value = false
}

const getConfig = async () => {
    await platform.gameTurnoverConfig().then(res => {
        Object.assign(formAll, res.data);
        formAll.auditPlatform = JSON.stringify(res.data.gamePlatformTreeResultDTO);
        setTimeout(() => {
        let dataArr = JSON.parse(formAll.auditPlatform);
        let obj = { 
            ...formAll,
            gamePlatformTreeResultDTO:dataArr
        }
        delete obj.auditPlatform;
        oldForm.value = JSON.stringify(obj);
    }, 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; /* 设置禁用时的鼠标指针为禁止图标 */
}
.formBetting-box{
    border:1px solid #ccc;
    border-radius: 5px;
    padding-top: 10px;
}
.clt-item {
   .label-box {
      vertical-align: middle;
      width: 260px;
      text-align: right;
      padding-right: 12px;
      word-break: break-all;
      line-height: 20px;
      box-sizing: border-box;
   }

   :deep(.el-form-item__error) {
      padding-left: 260px;
   }

   .item-right {
      width: calc(100% - 260px);
   }
}
</style>