<template>
  <NavBar />
  <view class="container">

    <view class="title">
      <PageTitle :title="$t('home.customer.title')" />
    </view>
    <view style="margin-top: 20px">
      <Segmented
          styleType="text"
          fontSize="16px"
          gap="6px"
          :current="current"
          :values="items"
          @clickItem="onClickItem"
          inActiveColor="#999999"
          activeColor="#000"
      ></Segmented>
    </view>

    <view style="margin-top: 20px">
      <view class="search">
        <input class="searchInput" v-model="queryCustomerParams.qcc_fuzzy_search" :placeholder="$t('home.customer.sellPage.name')+'/'+$t('home.customer.sellPage.email')">
        <button class="btn primaryButton" @click="search">
          <image src="/static/search.svg" mode="aspectFit" style="width: 14px;height: 14px"></image>
          <text class="filterText">{{ $t('form.search') }}</text>
        </button>
      </view>
      <view class="search2">
        <button class="btn primaryButton" @click="openFilter">
          <image src="/static/filtrate.svg" mode="aspectFit" style="width: 20px;height: 20px"></image>
          <text class="filterText">{{ $t('common.filter') }}</text>
        </button>
      </view>

      <scroll-view style="height: calc(100vh - 260px);" scroll-y="true" @scrolltolower="scrolltolowerQuery" >
        <view class="item" v-for="user in userList" :key="user.id">
          <view class="top">
            <view class="itemTitle">
<!--              <image class="title-bg" src="/static/titleIcon2.svg" mode="aspectFit"></image>-->
              <view class="title-bg">
                <text class="title-text">{{ user.mt4_login }}</text>
              </view>
            </view>
          </view>
          <uni-collapse class="box" style="margin-top: 12px" >
            <uni-collapse-item title-border="none" :show-arrow="false" :border="false" :open="user.if_show" :disabled="true">
              <template v-slot:title>

                <view class="row" v-show="filterColumns.user_code">
                  <view class="column">{{ filterColumns.user_code?.title }}</view>
                  <view class="value">{{ user.user_code }}</view>
                </view>

                <view class="row" v-show="filterColumns.name">
                  <view class="column">{{ filterColumns.name?.title }}</view>
                  <view class="value">{{ user.name }}</view>
                </view>

                <view class="row" v-show="filterColumns.mt4_type_name">
                  <view class="column">{{ filterColumns.mt4_type_name?.title }}</view>
                  <view class="value">{{ user.mt4_type_name }}</view>
                </view>

                <view class="bottomItem" v-show="!user.if_show" style="padding-bottom: 8px">
                  <text @click="showItem(user)">{{ $t('home.customer.sellPage.open') }}</text>
                  <image @click="showItem(user)" src="/static/openIcon.svg" mode="aspectFit" style="width: 14px;height: 8px"></image>
                </view>
              </template>
              <view class="content" style="padding-bottom: 8px">

                <view class="row" v-show="filterColumns.mt4_group">
                  <view class="column">{{ filterColumns.mt4_group?.title }}</view>
                  <view class="value">{{ user.mt4_group }}</view>
                </view>
                <view class="row" v-show="filterColumns.mt4_balance">
                  <view class="column">{{ filterColumns.mt4_balance?.title }}</view>
                  <view class="value">{{ user.mt4_balance }}</view>
                </view>
                <view class="row" v-show="filterColumns.equity">
                  <view class="column">{{ filterColumns.equity?.title }}</view>
                  <view class="value">{{ user.equity }}</view>
                </view>
                <view class="row" v-show="filterColumns.mt4_credit">
                  <view class="column">{{ filterColumns.mt4_credit?.title }}</view>
                  <view class="value">{{ user.mt4_credit }}</view>
                </view>
                <view class="row" v-show="filterColumns.equity_balance">
                  <view class="column">{{ filterColumns.equity_balance?.title }}</view>
                  <view class="value">{{ user.equity_balance }}</view>
                </view>
                <view class="bottomItem">
                  <text @click="hideItem(user)">{{ $t('home.customer.sellPage.packUp') }}</text>
                  <image @click="hideItem(user)" src="/static/packUpIcon.svg" mode="aspectFit" style="width: 14px;height: 8px"></image>
                </view>
              </view>

            </uni-collapse-item>
          </uni-collapse>
        </view>
        <Spin v-show="loading" />
        <view class="inTheEnd" v-show="inTheEnd">{{ $t('common.inTheEnd') }}</view>
        <view class="inTheEnd" v-show="noData">{{ $t('common.noData') }}</view>
        <view style="height: 20px;width: 100%;"></view>
      </scroll-view>
    </view>

    <uni-popup ref="popup" background-color="#fff" type="bottom">
      <view style="padding: 37px 0;">
        <scroll-view style="max-height: 70vh;" scroll-y="true">

          <view class="filterRow" v-show="filterColumns.user_code?.ifFilter">
            <view class="filterTitle">
              <text>{{ filterColumns.user_code?.title }}</text>
            </view>
            <input class="filterInput" v-model="queryCustomerParams.user_code" :placeholder="$t('form.pleaseEnter')">
          </view>
          <view class="filterRow" v-show="filterColumns.mt4_login?.ifFilter">
            <view class="filterTitle">
              <text>{{ filterColumns.mt4_login?.title }}</text>
            </view>
            <input type="number" class="filterInput" v-model="queryCustomerParams.mt4_login" :placeholder="$t('form.pleaseEnter')">
          </view>
          <view class="filterRow" v-show="filterColumns.mt4_server?.ifFilter">
            <view class="filterTitle">
              <text>{{ filterColumns.mt4_server?.title }}</text>
            </view>
            <uni-data-select
                :placeholder="$t('form.pleaseSelect')"
                :localdata="mtServerList"
                v-model="queryCustomerParams.mt4_server"
                placement="top"
            ></uni-data-select>
          </view>
          <view class="filterRow" v-show="filterColumns.mt4_group?.ifFilter">
            <view class="filterTitle">
              <text>{{ filterColumns.mt4_group?.title }}</text>
            </view>
            <input class="filterInput" v-model="queryCustomerParams.mt4_group" :placeholder="$t('form.pleaseEnter')">
          </view>

        </scroll-view>
        <view style="display: flex;gap: 12px;padding: 14px 16px;">
          <buttom class="btn secondaryButton" @click="reset">
            {{ $t('home.customer.sellPage.reset') }}
          </buttom>
          <buttom class="btn primaryButton" @click="search">
            {{ $t('home.customer.sellPage.confirm') }}
          </buttom>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import Segmented from '@/pages/components/segmented/index.vue';
import UniCollapse from "../../../uni_modules/uni-collapse/components/uni-collapse/uni-collapse.vue";
import UniDataSelect from "../../../uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
import { queryList } from '@/services/customer/mt.ts';
import { getMtServer } from '@/services/home/home.ts';
import { UserLanguage, customerMenu } from '@/utils/const';
import { getFilterColumns, queryMenuList } from '@/services/home/home.ts';

export default {
  data() {
    return{
      qcc_page_id:640,
      filterColumns:{},
      loading:false,
      current: 3,
      items: [],
      itemsLink: [],
      inTheEnd:false,
      noData:false,
      queryCustomerParams:{
        app:"md",
        qcc_fuzzy_search:null,
        curr_page:1,
        page_size:5,
        user_code:null,
        mt4_login:null,
        mt4_server:null,
        mt4_group:null,
      },
      userList:[],
      totalRecords:0,
      mtServerList:[]
    }
  },
  methods:{
    async getMenuListData() {
      const res = await queryMenuList();
      if (res && res.code === 0) {
        const menuList = [];
        for (var i = 0; i < res.data.length; i++) {
          const { saleRouteKey } = res.data[i]
          if (saleRouteKey == 'customer') {
            if (res.data[i].children.length>0){
              let index = 0;
              for (let j = 0; j < res.data[i].children.length; j++) {
                if (res.data[i].children[j].saleRouteKey !=null && res.data[i].children[j].saleRouteKey !=''){
                  this.items.push(res.data[i].children[j].name)
                  this.itemsLink.push(customerMenu[res.data[i].children[j].saleRouteKey])
                  if (res.data[i].children[j].saleRouteKey == 'mt'){
                    this.current = index
                  }
                  index++
                }
              }
            }
          }
        }
      }
    },
    onClickItem(e) {
      if (this.current != e.currentIndex) {
        uni.navigateTo({url: this.itemsLink[e.currentIndex]});
      }
    },
    search(){
      this.queryCustomerParams.curr_page = 1
      this.queryCustomerParams.page_size = 5
      this.queryUserList()
    },
    async queryUserList() {
      this.$refs.popup.close();
      this.userList = []
      this.loading=true
      const res = await queryList(this.queryCustomerParams);
      if (res && res.code === 0) {
        let data = res.data;
        this.userList = data.records
        this.totalRecords = data.totalRecords
      }
      if (this.userList == null || this.userList.length == 0){
        this.noData = true
      }else {
        this.noData = false
      }
      this.loading=false
    },
    async getFilterColumns(){
      const res = await getFilterColumns({
        qcc_page_id: this.qcc_page_id,
        qcc_language: UserLanguage
      });
      if (res && res.code === 0) {
        let data = res.data
        for (let i = 0; i < data.length; i++) {
          this.filterColumns[data[i].field]=data[i]
        }
      }
    },
    showItem(user) {
      user.if_show = true;
    },
    hideItem(user) {
      user.if_show = false;
    },
    openFilter() {
      this.$refs.popup.open()
    },
    async scrolltolowerQuery(){
      if ((this.queryCustomerParams.page_size + 5)<(this.totalRecords + 5)){
        this.inTheEnd = false;
        this.queryCustomerParams.page_size = this.queryCustomerParams.page_size +5
        this.queryUserList()
      }else {
        this.inTheEnd = true;
      }
    },
    async getMtServerList(){
      const res = await getMtServer({
        qcc_language:UserLanguage
      });
      if (res && res.code === 0) {
        let data = res.data
        this.mtServerList = data.map((item) => ({
          text: item.server_name,
          value: item.id,
        }));
      }
    },
    reset(){
      this.queryCustomerParams.user_code = null
      this.queryCustomerParams.mt4_login = null
      this.queryCustomerParams.mt4_server = null
      this.queryCustomerParams.mt4_group = null
    }
  },
  mounted() {
    this.getMenuListData();
    this.getFilterColumns();
    this.queryUserList()
    this.getMtServerList()
  },
  components: {
    Segmented,
    UniCollapse,
    UniDataSelect
  }

};
</script>

<style lang="scss" scoped>
@import './index.scss';
.is-input-border {
  border: none;
}
</style>
