<template>
  <div class="filter-block-wrapper">
    <div class="custom-form">
      <div class="select-box-chain">
        <ChoseChain></ChoseChain>
        <ClientOnly>
          <div class="Advanced-Features">
            <t-button class="Smart-btn" @click="openSmart">
              <template #icon>
                <SmartSvg class="icon"></SmartSvg>
              </template>
              {{ $t('Customized.SmartAlerts') }}</t-button
            >
            <t-button class="reset-btn" @click="Reset">
              <template #icon>
                <ResetSvg class="icon"></ResetSvg>
              </template>
              {{ $t('Customized.reset') }}</t-button
            >
          </div>
        </ClientOnly>
        <ClientOnly>
          <SmartAlerts
            :visible="visible"
            @close="closeThisDialog"
          ></SmartAlerts>
        </ClientOnly>
      </div>
      <div class="custom-form-item">
        <div class="title">
          <t-tooltip :content="$t('tips.contentvalue1')" placement="left-top">
            <QuestionSvg></QuestionSvg>
          </t-tooltip>
          {{ $t('home.timeRange') }}：
        </div>
        <div class="value">
          <t-radio-group
            v-model="formData.date"
            :options="TIME_RANGE_OPTIONS"
            class="custom-group-type-1"
          >
            <t-radio-button
              v-for="item of TIME_RANGE_OPTIONS"
              :key="item.value"
              :value="item.value"
              class="custom-value-type-1"
              @click="ReverseBtn('date', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
        </div>
      </div>
      <div class="custom-form-item">
        <div class="title">
          <t-tooltip :content="$t('tips.poolSizeValue')" placement="left-top">
            <QuestionSvg></QuestionSvg>
          </t-tooltip>
          {{ $t('home.poolSize') }}：
        </div>
        <div class="value">
          <t-switch
            class="pre-switch"
            v-model="formData.isWbnb"
            size="large"
            :label="PoolSize"
            @change="SwitchChange"
          ></t-switch>
          <t-radio-group
            v-show="formData.isWbnb"
            class="custom-group-type-2"
            v-model="formData.poolSize"
          >
            <t-radio-button
              v-for="item in POOL_SIZE_OPTIONS"
              :value="item.value"
              :key="item"
              @click="ReverseBtn('poolSize', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
          <t-radio-group
            v-show="!formData.isWbnb"
            class="custom-group-type-2"
            v-model="formData.poolSize"
          >
            <t-radio-button
              v-for="item in POOL_SIZE_USD_OPTIONS"
              :value="item.value"
              :key="item"
              @click="ReverseBtn('poolSize', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
        </div>
      </div>
      <div class="custom-form-item">
        <div class="title">
          <t-tooltip :content="$t('tips.PirceRangeValue')" placement="left-top">
            <QuestionSvg></QuestionSvg>
          </t-tooltip>
          {{ $t('home.pirceRange') }}：
        </div>
        <div class="value">
          <t-radio-group class="custom-group-type-3" v-model="formData.range">
            <t-switch
              class="pre-switch2"
              v-model="formData.isRise"
              size="large"
              :label="['+', '-']"
            ></t-switch>
            <ClientOnly
              ><t-select
                class="custom-select-home-range"
                v-model="formData.priceRangeTime"
                :popup-props="{ overlayClassName: 'select_home_range_box' }"
              >
                <t-option
                  v-for="item in Time_price_range_options"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                >
                  <div class="item-box">
                    <div>{{ item.label }}</div>
                  </div>
                </t-option>
              </t-select>
            </ClientOnly>
            <t-radio-button
              v-for="item in PRICE_RANGE_OPTIONS"
              :value="item.value"
              :key="item"
              @click="ReverseBtn('range', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
        </div>
      </div>
      <div class="custom-form-item">
        <div class="title">
          <t-tooltip :content="$t('tips.NewHolderValue')" placement="left-top">
            <QuestionSvg></QuestionSvg>
          </t-tooltip>
          {{ $t('home.newHolder') }}：
        </div>
        <div class="value">
          <t-radio-group
            v-model="formData.newHolder"
            class="custom-group-type-1"
          >
            <t-radio-button
              v-for="item in NEW_HOLDER_OPTIONS"
              :value="item.value"
              :key="item"
              class="custom-button custom-value-type-1"
              @click="ReverseBtn('newHolder', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
        </div>
      </div>
      <div class="custom-form-item">
        <div class="title">
          <t-tooltip :content="$t('tips.VolumeValue')" placement="left-top">
            <QuestionSvg></QuestionSvg>
          </t-tooltip>
          {{ $t('home.volume') }}：
        </div>
        <div class="value">
          <t-radio-group v-model="formData.volume" class="custom-group-type-1">
            <t-radio-button
              v-for="item in VOLUME_OPTIONS"
              :value="item.value"
              :key="item"
              class="custom-button custom-value-type-1"
              @click="ReverseBtn('volume', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
        </div>
      </div>
      <div class="custom-form-item">
        <div class="title">
          <t-tooltip :content="$t('tips.VolumeValue')" placement="left-top">
            <QuestionSvg></QuestionSvg>
          </t-tooltip>
          {{ $t('home.txns') }}：
        </div>
        <div class="value">
          <t-radio-group v-model="formData.txns" class="custom-group-type-1">
            <t-radio-button
              v-for="item in TXNS_OPTIONS"
              :value="item.value"
              :key="item"
              class="custom-button custom-value-type-1"
              @click="ReverseBtn('txns', item.value)"
              >{{ item.label }}</t-radio-button
            >
          </t-radio-group>
        </div>
      </div>
    </div>
    <FilterTips></FilterTips>
  </div>
</template>
<script setup lang="ts">
import {
  TIME_RANGE_OPTIONS,
  POOL_SIZE_OPTIONS,
  POOL_SIZE_USD_OPTIONS,
  PRICE_RANGE_OPTIONS,
  NEW_HOLDER_OPTIONS,
  VOLUME_OPTIONS,
  TXNS_OPTIONS,
  Time_price_range_options,
} from '@/constants/token';
import {
  chain_options,
  filterChainObj,
  SwitchCurrency,
} from '@/constants/UnifiedManagementChain';
import FilterTips from './FilterTips.vue';
import ResetSvg from '/public/images/svg/filter/reset.svg';
import SmartSvg from '/public/images/svg/filter/Smart.svg';
import QuestionSvg from '/public/images/svg/filter/Question.svg';
import { MessagePlugin } from 'tdesign-vue-next';
import SmartAlerts from './SmartAlerts.vue';
import { useI18n } from 'vue-i18n';
import ChoseChain from './choseChain';
const { t } = useI18n();
// 当前链
const chain = useChain();
const router = useRouter();
const route = useRoute();
// 表格要切换的数据
const HeadTokenType = useTableHeadToken();
const formData = reactive({
  date: '5m',
  isWbnb: true,
  isRise: true,
  poolSize: '',
  range: '',
  newHolder: '',
  volume: '',
  txns: '',
  priceRangeTime: '5m',
  isNewchain: '0',
});
// 当前链整个对象数据
const ChainObj = ref();
if (route.params.chain) {
  let Obj = filterChainObj('name', route.params.chain, true);
  if (Obj) {
    ChainObj.value = Obj;
    chain.value = Obj.value;
  }
} else {
  ChainObj.value = filterChainObj('value', chain.value, true);
}

// 弹出框状态
const visible = ref(false);
// 重置
const Reset = () => {
  formData.date = '5m';
  formData.poolSize = '';
  formData.range = '';
  formData.newHolder = '';
  formData.volume = '';
  formData.txns = '';
  MessagePlugin.closeAll();
  MessagePlugin.success(t('MessagePlugin.resetSuccess'));
};
// 反选按钮
const ReverseBtn = (label: string, value: string) => {
  if (formData[label] === value) {
    formData[label] = '';
  }
};
// 打开弹出框
const openSmart = () => {
  visible.value = true;
};
// 关闭弹出框
const closeThisDialog = (value: boolean) => {
  visible.value = value;
};

// 币种--SwitchCurrency过滤出默认的币种
const PoolSize = ref(SwitchCurrency(chain.value));

// 筛选条件
const GlbFilter = useCurFilter();
// 通知表格变化
const ChangeTable = useChangeTable();

// 监听链变化，修改代币种类
watch(
  () => chain.value,
  (v) => {
    if (v) {
      ChainObj.value = filterChainObj('value', v, true);
      PoolSize.value = SwitchCurrency(v);
    }
  }
);

const SwitchChange = (v: any) => {
  // 过滤出当前需要展示的代币名称
  let Obj = filterChainObj('value', chain.value);
  let bname = [];
  if (Obj) {
    bname = Obj.Currency;
  }
  v ? (HeadTokenType.value = bname[0]) : (HeadTokenType.value = bname[1]);
};

// 条件过滤方法
const filterFn = () => {
  let params: any = {
    pool_column: formData.isWbnb ? 'wb' : 'ub',
    nt: parseInt(formData.isNewchain),
    range_date: formData.priceRangeTime,
  };
  if (formData.date) {
    params.date = formData.date;
  }
  if (formData.poolSize) {
    params.pool_range = formData.poolSize;
  }
  // 加
  if (formData.isRise) {
    if (formData.range) {
      params.rise_or_fall = 'up';
      params.range = formData.range;
    } else {
      params.rise_or_fall = 'up';
    }
  } else {
    // 减
    if (formData.range) {
      params.rise_or_fall = 'down';
      params.range = formData.range;
    } else {
      params.rise_or_fall = 'down';
    }
  }
  if (formData.newHolder) {
    params.new_holder = formData.newHolder;
  }
  if (formData.volume) {
    params.volume = formData.volume;
  }
  if (formData.txns) {
    params.txn = formData.txns;
  }
  return params;
};

watch(formData, (v) => {
  let params = filterFn();
  // 筛选条件改变，永远请求第一页
  params.page = 1;
  params.page_size = 100;
  if (Object.keys(params).length) {
    // 提交全局筛选条件
    GlbFilter.value = params;
    // 还要通知list页面请求数据
    ChangeTable.value += 1;
    router.push({
      path: `/${ChainObj.value.name}/1`,
    });
  }
});
</script>
<style lang="less">
@import '@/style/flex.less';
.select_chain_box {
  width: 100px;
  user-select: none;
  .t-popup__content {
    max-height: none !important;
  }
  .t-select-option {
    height: 40px;
    .dja();
    width: 100%;
    span {
      width: 100%;
      .item-box {
        width: 100%;
        .da();
        font-weight: 1000;
        :nth-child(2) {
          margin-left: 12px;
        }
        .img-box {
          .da();
          img {
            display: block;
          }
        }
      }
    }
  }
}
.select_home_range_box {
  .t-select__list {
    .t-select-option {
      .dja();
      font-size: 12px;
    }
  }
}
.t-form__controls .t-input__help {
  display: none;
}
</style>
<style lang="less">
// flex布局
@import '@/style/flex.less';
@import '@/style/variables.less';
.filter-block-wrapper {
  display: flex;
  width: 100%;
  box-sizing: border-box;
  .custom-form {
    padding-right: 12px;
    background: var(--main-theme-color-2);
    .select-box-chain {
      height: 53px;
      background: var(--td--right-back-color-2);
      border-radius: @border-radius @border-radius 0 0;
      .dja(flex-start,center);
      position: relative;
      box-sizing: border-box;
      &::before {
        content: '';
        height: 1px;
        width: 200%;
        position: absolute;
        bottom: 0;
        border-bottom: var(--new-border-2);
        transform: scale(0.5);
        -webkit-transform: scale(0.5);
        -o-transform: scale(0.5);
        -ms-transform: scale(0.5);
        -moz-transform: scale(0.5);
        //
        transform-origin: 0 0;
        -webkit-transform-origin: 0 0;
        -o-transform-origin: 0 0;
        -ms-transform-origin: 0 0;
        -moz-transform-origin: 0 0;
      }
      .t-select__wrap {
        width: 15.3%;
        .t-input {
          height: 40px !important;
          .t-input__inner {
            margin-left: 6px;
            font-weight: 1000;
          }
        }
      }
      .t-select {
        margin-left: 12px;
        width: 110px;
      }
      .custom-group2 {
        .t-radio-button {
          border: none;
          margin: 0 4px;
          width: 90px;
          .dj();
          font-size: var(--font-size-base);
          font-weight: bold;
          .t-radio-button__former {
            width: 0 !important;
          }
          &.t-is-checked {
            background-color: var(--td--main-btn-color-1);
            color: #fff;
            border-radius: 16px;
          }
        }
        :nth-child(1) {
          width: 63px !important;
          height: 24px;
        }
        label:nth-child(2) {
          margin-left: 72px;
        }
      }
      .Advanced-Features {
        position: absolute;
        right: 12px;
        .t-button {
          background: var(--td-brand-color-btn-1);
          border: none;
          --ripple-color: rgb(230, 230, 244);
          .icon {
            margin-right: 6px;
            fill: var(--td-brand-color-icon-1);
          }
        }
        .Smart-btn {
          color: var(--td--main-btn-color-1);
          margin-right: 6px;
        }
        .reset-btn {
          color: var(--td-Search-info-color-1);
        }
      }
    }
    .custom-form-item {
      min-height: auto;
      padding: 0;
      height: 42px;
      background: var(--td--right-back-color-2);
      .da();
      position: relative;
      &::before {
        content: '';
        height: 1px;
        width: 200%;
        position: absolute;
        bottom: 0;
        transform-origin: 0 0;
        -webkit-transform-origin: 0 0;
        -o-transform-origin: 0 0;
        -ms-transform-origin: 0 0;
        -moz-transform-origin: 0 0;
        border-bottom: var(--new-border-2);
        transform: scale(0.5);
        -webkit-transform: scale(0.5);
        -o-transform: scale(0.5);
        -ms-transform: scale(0.5);
        -moz-transform: scale(0.5);
      }
      &:last-of-type::before {
        border-bottom: 1px solid var(--td--right-back-color-2);
      }
      &:last-of-type {
        border-radius: 0 0 @border-radius @border-radius;
      }
      .title {
        width: 110px;
        font-size: 12px;
        padding-left: 8px;
        white-space: nowrap;
        .da();
        & > :nth-child(1) {
          padding-right: 12px;
          cursor: pointer;
        }
      }
      .value {
        .da();
        .t-radio-group {
          .t-radio-button {
            border: none;
            border-radius: 16px;
            font-size: 12px;
            width: 90px;
            height: 24px;
            .dja();
          }
          .custom-value-type-1:nth-child(1) {
            width: 63px;
          }
          .custom-value-type-1:nth-child(2) {
            margin-left: 74px;
          }
          .custom-button:nth-child(1) {
            width: 63px;
          }

          .t-is-checked {
            background: var(--td--main-btn-color-1);
            color: white;
          }
          .t-select__wrap {
            width: 60px;
            height: 28px;
            margin-left: 12px;
            .t-input__wrap {
              height: 100%;
              .t-input {
                height: 100%;
              }
            }
            .t-input__inner {
              text-align: center;
            }
          }
          .custom-select-home-range {
            .t-input__inner {
              font-size: 12px;
            }
          }
        }
        .custom-group-type-1 {
          margin-left: -2px;
        }
        .custom-group-type-2 {
          margin-left: 72px;
        }
        .custom-group-type-3 {
          .pre-switch2 {
            background-color: var(--td--main-btn-color-1);
            width: 63px;
            .t-switch__content {
              padding-left: 20px;
            }
            .t-size-l {
              text-align: center;
            }
          }
        }
        .pre-switch {
          width: 63px;
          background-color: var(--td--main-btn-color-1);
        }
        .t-radio-group__outline {
          flex-wrap: nowrap;
        }
      }
    }
  }
}
</style>
