<template>
    <div class="container">
        <Breadcrumb :items="['menu.settings', 'menu.settings.config']" />
        <a-card class="general-card" :title="$t('menu.settings.config')">
            <a-row class="wrapper">

                <a-col :span="24">
                    <a-spin :loading="loading" style="width:100%">
                        <a-form ref="formRef" :model="formData" class="form" :label-col-props="{ span: 4 }"
                            :wrapper-col-props="{ span: 19 }">


                            <a-tabs default-active-key="1">
                                <a-tab-pane key="1" title="全局配置">


                                    <a-form-item field="pass_salt" label="pass_salt" :rules="[{ required: true }]">
                                        <a-input v-model="formData.pass_salt" />
                                    </a-form-item>


                                    <a-form-item field="fake_click_min" label="补点击次数" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.fake_click_min" :min="0" /> -
                                        <a-input-number v-model="formData.fake_click_max" :min="0" />
                                    </a-form-item>

                                    <a-form-item field="rt_max" label="转链超时" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.rt_max" :min="500"
                                            placeholder="转链API最长超时时间，单位毫秒" />
                                    </a-form-item>


                                    <a-form-item field="blacklist_oaid" label="OAID黑名单">
                                        <a-textarea v-model="formData.blacklist_oaid" placeholder="OAID黑名单"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" />
                                    </a-form-item>

                                    <a-form-item field="webook_xhs_zlong" label="小红书 Zlong 转发">
                                        <a-space>
                                            <a-switch v-model="formData.webook_xhs_zlong" :checked-value="1"
                                                :unchecked-value="0" />
                                        </a-space>
                                    </a-form-item>

                                </a-tab-pane>
                                <a-tab-pane key="cps" title="CPS">

                                    <a-form-item field="cpsIgnorePercentageCity" label="CPS流量控制-城市">
                                        <a-input v-model="formData.cpsIgnorePercentageCity" />
                                    </a-form-item>


                                    <a-form-item field="cps_limit_per_ip_24h" label="(IP) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.cps_limit_per_ip_24h" :min="0"
                                            placeholder="同ip在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="cps_limit_per_oaid_24h" label="(OAID) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.cps_limit_per_oaid_24h" :min="0"
                                            placeholder="同 oaid 在24小时内可转链的次数。" />
                                    </a-form-item>


                                </a-tab-pane>
                                <a-tab-pane key="tb" title="淘宝">

                                    <a-form-item field="similar_timeout" label="搜同款超时" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.similar_timeout" :min="1000"
                                            placeholder="搜同款API最长超时时间，单位毫秒" />
                                    </a-form-item>


                                    <a-form-item field="tk_whitelist_regular" label="淘口令正则">
                                        <a-textarea v-model="formData.tk_whitelist_regular"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" placeholder="淘口令正则" />
                                        <template #extra>
                                            <a-button type="outline"
                                                @click="clickRegularTester(formData.tk_whitelist_regular)">正则测试
                                            </a-button>
                                        </template>
                                    </a-form-item>

                                    <a-form-item field="tk_blacklist_regular" label="淘口令排除正则">
                                        <a-textarea v-model="formData.tk_blacklist_regular"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" placeholder="淘口令排除正则" />
                                        <template #extra>
                                            <a-button type="outline"
                                                @click="clickRegularTester(formData.tk_blacklist_regular)">正则测试
                                            </a-button>
                                        </template>
                                    </a-form-item>

                                    <a-form-item field="multi_token_regular" label="混合口令正则">
                                        <a-textarea v-model="formData.multi_token_regular"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" placeholder="混合口令正则" />
                                        <template #extra>
                                            <a-button type="outline"
                                                @click="clickRegularTester(formData.multi_token_regular)">正则测试
                                            </a-button>
                                        </template>
                                    </a-form-item>

                                    <a-form-item field="ignorePercentage" label="放弃流量" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.ignorePercentage" :min="0" :max="100"
                                            placeholder="主动放弃流量，0-100，举例：10 = 放弃10%" />
                                    </a-form-item>


                                    <a-form-item field="ignorePercentageCity" label="流量控制-城市">
                                        <a-input v-model="formData.ignorePercentageCity" />
                                    </a-form-item>

                                    <a-form-item field="tk_limit_per_ip_24h" label="(IP) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.tk_limit_per_ip_24h" :min="0"
                                            placeholder="同ip在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="tk_limit_per_oaid_24h" label="(OAID) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.tk_limit_per_oaid_24h" :min="0"
                                            placeholder="同 oaid 在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="tk_parse_retry_count" label="转链失败重试"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.tk_parse_retry_count" :min="0"
                                            placeholder="转链失败重试次数" />
                                    </a-form-item>
                                </a-tab-pane>
                                <a-tab-pane key="jd" title="京东">
                                    <a-form-item field="jdIgnorePercentage" label="放弃流量" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.jdIgnorePercentage" :min="0" :max="100"
                                            placeholder="主动放弃流量，0-100，举例：10 = 放弃10%" />
                                    </a-form-item>
                                    <a-form-item field="jdIgnorePercentageCity" label="流量控制-城市">
                                        <a-input v-model="formData.jdIgnorePercentageCity" />
                                    </a-form-item>

                                    <a-form-item field="jd_limit_per_ip_24h" label="(IP) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.jd_limit_per_ip_24h" :min="0"
                                            placeholder="同ip在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="jd_limit_per_oaid_24h" label="(OAID) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.jd_limit_per_oaid_24h" :min="0"
                                            placeholder="同 oaid 在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="jd_blacklist_regular" label="排除正则">
                                        <a-textarea v-model="formData.jd_blacklist_regular"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" placeholder="排除正则" />
                                        <template #extra>
                                            <a-button type="outline"
                                                @click="clickRegularTester(formData.jd_blacklist_regular)">正则测试
                                            </a-button>
                                        </template>
                                    </a-form-item>


                                    <a-divider />

                                    <a-form-item field="jd_h5st_sign_url" label="h5st签名地址">
                                        <a-input v-model="formData.jd_h5st_sign_url" />
                                    </a-form-item>

                                    <a-form-item field="jd_h5st_sign_version" label="h5st版本">
                                        <a-input v-model="formData.jd_h5st_sign_version" />
                                    </a-form-item>

                                    <a-divider />

                                    <a-form-item field="dp_style" label="不可分佣的DP样式">
                                        <a-select v-model="formData.dp_style">
                                            <a-option value="">默认样式</a-option>
                                            <a-option value="clickId">带clickId</a-option>
                                        </a-select>
                                    </a-form-item>
                                    <template v-if="formData.dp_style">
                                        <a-form-item field="dp_style_default_str" label="默认clickId标识">
                                            <a-input v-model="formData.dp_style_default_str" />
                                        </a-form-item>
                                        <a-form-item field="dp_style_percentage" label="新样式的比重">
                                            <a-input-number v-model="formData.dp_style_percentage" :min="0" :max="100"
                                                placeholder="主动放弃流量，0-100，举例：10 = 放弃10%" />
                                        </a-form-item>
                                    </template>

                                </a-tab-pane>
                                <a-tab-pane key="pdd" title="拼多多">
                                    <a-form-item field="pddIgnorePercentage" label="放弃流量" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.pddIgnorePercentage" :min="0" :max="100"
                                            placeholder="主动放弃流量，0-100，举例：10 = 放弃10%" />
                                    </a-form-item>

                                    <a-form-item field="pddIgnorePercentageCity" label="流量控制-城市">
                                        <a-input v-model="formData.pddIgnorePercentageCity" />
                                    </a-form-item>

                                    <a-form-item field="pdd_limit_per_ip_24h" label="(IP) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.pdd_limit_per_ip_24h" :min="0"
                                            placeholder="同 ip 在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="pdd_limit_per_oaid_24h" label="(OAID) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.pdd_limit_per_oaid_24h" :min="0"
                                            placeholder="同 oaid 在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="pdd_blacklist_regular" label="排除正则">
                                        <a-textarea v-model="formData.pdd_blacklist_regular"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" placeholder="排除正则" />
                                        <template #extra>
                                            <a-button type="outline"
                                                @click="clickRegularTester(formData.pdd_blacklist_regular)">正则测试
                                            </a-button>
                                        </template>
                                    </a-form-item>

                                </a-tab-pane>
                                <a-tab-pane key="ks" title="快手">
                                    <a-form-item field="ksIgnorePercentage" label="放弃流量" :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.ksIgnorePercentage" :min="0" :max="100"
                                            placeholder="主动放弃流量，0-100，举例：10 = 放弃10%" />
                                    </a-form-item>

                                    <a-form-item field="ksIgnorePercentageCity" label="流量控制-城市">
                                        <a-input v-model="formData.ksIgnorePercentageCity" />
                                    </a-form-item>

                                    <a-form-item field="ks_limit_per_ip_24h" label="(IP) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.ks_limit_per_ip_24h" :min="0"
                                            placeholder="同ip在24小时内可转链的次数。" />
                                    </a-form-item>

                                    <a-form-item field="ks_limit_per_oaid_24h" label="(OAID) 24小时转链次数"
                                        :rules="[{ required: true }]">
                                        <a-input-number v-model="formData.ks_limit_per_oaid_24h" :min="0"
                                            placeholder="同 oaid 在24小时内可转链的次数。" />
                                    </a-form-item>
                                    <a-form-item field="ks_blacklist_regular" label="排除正则">
                                        <a-textarea v-model="formData.ks_blacklist_regular"
                                            :auto-size="{ minRows: 5, maxRows: 20 }" placeholder="排除正则" />
                                        <template #extra>
                                            <a-button type="outline"
                                                @click="clickRegularTester(formData.ks_blacklist_regular)">正则测试
                                            </a-button>
                                        </template>
                                    </a-form-item>

                                    <a-form-item field="ksTestToken" label="测试token">
                                        <a-input v-model="formData.ksTestToken" />
                                    </a-form-item>

                                </a-tab-pane>
                            </a-tabs>
                            <a-form-item>
                                <a-space>
                                    <a-button type="primary" @click="validate">
                                        {{ $t('form.save') }}
                                    </a-button>
                                    <a-button type="secondary" @click="reset">
                                        {{ $t('form.reset') }}
                                    </a-button>
                                </a-space>
                            </a-form-item>


                        </a-form>
                    </a-spin>
                </a-col>
            </a-row>
        </a-card>
        <RegularTester v-model:visible="showRegularTester" v-model:pattern="RegularTesterPattern" />
    </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';

import useLoading from '@/hooks/loading';
import { Message } from '@arco-design/web-vue';
import { FormInstance } from '@arco-design/web-vue/es/form';
import {
    UpdateConfigModel, updateConfig,
    getConfig, UpdateConfigRes
} from '@/api/taobao';
import { useUserStore } from '@/store';
import RegularTester from './components/regular-tester.vue';

const userStore = useUserStore();
const userInfo = computed(() => {
    return {
        name: userStore.name,
    };
});
console.log(userInfo.value)

const { loading, setLoading } = useLoading(true);

const router = useRouter();

const formRef = ref<FormInstance>();
const formData = ref<UpdateConfigModel>({
    cookie: '',
    blacklist_oaid: '',
    ignorePercentage: 0,
    pass_salt: '',
    fake_click_min: 0,
    fake_click_max: 0,
    fake_click_ttl: 0,
    rt_max: 0,
    similar_timeout: 0,
    tk_whitelist_regular: '',
    tk_blacklist_regular: '',
    multi_token_regular: '',
    tk_limit_per_ip_24h: 0,
    tk_limit_per_oaid_24h: 0,
    tk_parse_retry_count: 0,

    cpsIgnorePercentageCity: '',
    cps_limit_per_ip_24h: 0,
    cps_limit_per_oaid_24h: 0,

    jdIgnorePercentage: 0,
    ignorePercentageCity: '',
    jdIgnorePercentageCity: '',
    jd_limit_per_ip_24h: 0,
    jd_blacklist_regular: '',

    jd_limit_per_oaid_24h: 0,
    jd_h5st_sign_url: '',
    jd_h5st_sign_version: '',

    dp_style: '',
    dp_style_percentage: 0,
    dp_style_default_str: '',


    pdd_limit_per_ip_24h: 0,
    pdd_limit_per_oaid_24h: 0,
    pddIgnorePercentageCity: '',
    pddIgnorePercentage: 0,
    pdd_blacklist_regular: '',

    ks_limit_per_ip_24h: 0,
    ksIgnorePercentageCity: '',
    ksIgnorePercentage: 0,
    ks_blacklist_regular: '',
    ksTestToken: '',
    ks_limit_per_oaid_24h: 0,
    webook_xhs_zlong: 0

});

const showRegularTester = ref<boolean>(false);
const RegularTesterPattern = ref<string>('');



const clickRegularTester = (pattern: string) => {
    showRegularTester.value = true
    RegularTesterPattern.value = pattern
};

const goXhsZlongReport = () => {
    router.push({ name: 'xhsZlongReport' });
};

const validate = async () => {
    const res = await formRef.value?.validate();
    if (!res) {
        try {
            const data: any = await updateConfig(formData.value);
            console.log(data);
            const msg = data.msg || '更新成功!';
            if (data.success) {
                Message.success(msg);
            } else {
                Message.error(msg);
            }


        } catch (err) {
            console.error(err);
        }
    }
};
const restoreJson = (e: string): string => {
    e = e.replace(/\\\\/g, "\\").replace(/\\"/g, '"');
    return e;
};


const reset = async () => {
    setLoading(true);
    // await formRef.value?.resetFields();
    const { data } = await getConfig()
    const config = data
    console.log(config)
    if (config) {
        config.tk_whitelist_regular = restoreJson(config.tk_whitelist_regular)
        config.tk_blacklist_regular = restoreJson(config.tk_blacklist_regular)
        config.multi_token_regular = restoreJson(config.multi_token_regular)
        formData.value = config
    }
    setLoading(false);
};
reset();


</script>

<style scoped lang="less">
.container {
    padding: 0 20px 20px 20px;
}

.wrapper {
    padding: 20px 0 0 20px;
    min-height: 580px;
    background-color: var(--color-bg-2);
    border-radius: 4px;
}

:deep(.section-title) {
    margin-top: 0;
    margin-bottom: 16px;
    font-size: 14px;
}

.form {
    margin: 0 auto;
}

.form .cookie {
    height: 100%;
    min-height: 500px;
}
</style>
