<template>
  <Navigater :title="t('dadan.title')" />

  <view class="gupiao_list">
    <view class="button_group">
      <view class="button" :class="{ active: pageType === 0 }" @click="changePageType(0)">{{ t('hongli.button1') }}</view>
      <view class="button" :class="{ active: pageType === 1 }" @click="changePageType(1)">{{ t('hongli.button2') }}</view>
    </view>
    <view v-show="pageType === 0" class="column flex">
      <view class="text-center">{{ t('dadan.column_name') }}</view>
      <view class="text-center">{{ t('dadan.column_daima') }}</view>
      <view class="text-center">{{ t('dadan.column_xianjia') }}</view>
      <view class="text-center">{{ t('dadan.column_jiage') }}</view>
      <!-- <view class="text-right">{{ t('dadan.column_zhangshu') }}</view>+ -->
    </view>
    <view v-show="pageType === 1" class="column flex">
      <view class="text-center">{{ t('hongli.column_name2') }}</view>
      <view class="text-center">{{ t('hongli.column_jiage2') }}</view>
      <view class="text-center">{{ t('hongli.column_chengjiaoe') }}</view>
      <view class="text-center">{{ t('hongli.column_zhuangtai') }}</view>
    </view>
    <scroll-view scroll-y :style="{ height: 'calc(100vh - 9.375rem)' }">
      <view v-for="(item, index) in dadanList" v-show="pageType === 0" :key="index" class="gupiao flex" @click="openWindow(item)">
        <view class="text_ellipsis">{{ item.product_name }}</view>
        <view class="text-center">{{ item.product_code }}</view>
        <view class="text-center">{{ changeMoney(item.now_price) }}</view>
        <view class="text-center">{{ changeMoney(item.hedging_price) }}</view>
        <!-- <view class="text-right">{{ item.last_num }}</view> -->
      </view>
      <view v-show="pageType === 1">
        <!-- <NotData v-if="shenqingList.length === 0" /> -->
        <!-- <view v-if="shenqingList.length === 0" class="nodata">{{ t('common.nodata') }}</view> -->
        <view v-for="(item, index) in shenqingList" :key="index" class="gupiao flex">
          <view class="text_ellipsis flex1 flex justify-center items-center flex_direction">
            <van-text-ellipsis :content="item?.product_name" style="color: #fff" />
            <!-- <view>{{ item?.product_name }}</view> -->
            <view style="color: #a6a6a6">{{ item?.product_code }}</view>
          </view>
          <view class="flex1 flex justify-center items-center flex_direction">
            <view style="color: #ff0021">{{ changeMoney(item.buy_in_amount) }}</view>
            <view v-if="item.status == '2' || item.status == '3' || item.status == '4'" style="color: #ff0021">
              {{ Math.floor(item.buy_in_num * 100) }}
            </view>
            <view v-else-if="item.status == '1'" style="color: #ff0021">
              {{ Math.floor(item.buy_in_num * 100) }}
            </view>
          </view>
          <view class="flex1 flex justify-center items-center text-red" style="color: #ff0021">{{ changeMoney(Math.floor(item.buy_in_amount)) }}</view>
          <view class="flex justify-center items-center flex1">
            <view v-if="item.status == '2'" style="color: #0cd17c"> {{ t('dadan.tip1') }} </view>
            <view v-if="item.status == '3'" class="text-red">{{ t('dadan.tip2') }} </view>
            <view v-if="item.status == '1'" style="color: #fff">{{ t('dadan.tip3') }}</view>
          </view>
        </view>
      </view>
      <NotData v-if="(pageType === 0 && dadanList.length === 0) || (pageType === 1 && shenqingList.length === 0)" />
    </scroll-view>
  </view>

  <van-popup v-model:show="gupiaoWindowShow" class="buy_window" v-bind="windowOptions">
    <view class="title">{{ t('dadan.title2') }}</view>
    <view class="row_wrap">
      <view class="row" style="width: 100%">
        <view class="label">{{ t('dadan.column_name') }}：</view>
        <view class="value text_ellipsis">{{ windowDetail.product_name }}</view>
      </view>
      <view class="row">
        <view class="label">{{ t('dadan.column_daima') }}：</view>
        <view class="value">{{ windowDetail.product_code }}</view>
      </view>
      <view class="row">
        <view class="label">{{ t('dadan.column_jiage') }}：</view>
        <view class="value">{{ windowDetail.hedging_price }}</view>
      </view>
      <!-- <view class="row">
        <view class="label">{{ t('dadan.column_zhangshu') }}：</view>
        <view class="value">{{ windowDetail.last_num }}</view>
      </view> -->
      <!-- <view class="row">
        <view class="label">{{ t('jiaoyi.shouxufei') }}：</view>
        <view class="value">{{ shouxufei.toFixed(1) }}</view>
      </view> -->
      <view class="row">
        <view class="label">{{ t('dadan.heji') }}：</view>
        <view class="value">{{ changeMoney(totalPrice) }}</view>
      </view>
      <view class="row">
        <view class="label" style="color: red">{{ t('dadan.zhuyi') }}：</view>
        <view class="value" style="color: red">{{ `1${t('dadan.unit')}=100${t('dadan.unit2')}` }}</view>
      </view>
      <view class="row" style="width: 100%">
        <view class="label">{{ t('ipo.windowLabel2') }}：</view>
        <view class="value"
          ><view class="flex justify-center my-[.8rem]"> <van-stepper v-model="buyParams.num" input-width="100" button-size="2rem" integer /> </view
        ></view>
      </view>
    </view>

    <view class="button" @click="buy">{{ t('gupiaoDetail.submit') }}</view>
  </van-popup>
</template>

<script setup lang="ts">
import { getDadanListApi, buyDadanApi, getUserMoneyApi, getUserMonitoringApi } from '@/api/index/index'
import { onHide, onLoad, onShow } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n'
import { ref, computed } from 'vue'
import { showToast } from 'vant'
import { changeMoney } from '@/common/common'
const { t } = useI18n()

onLoad(() => {
  getUserMoney()
})
let id: any = null
onShow(() => {
  getUserMonitoringFn()
  getXinguList()
  id = setInterval(() => {
    getXinguList()
  }, 20000)
})
onHide(() => {
  clearInterval(id)
})

const pageType = ref(0)
const changePageType = async (e: number) => {
  pageType.value = e
}

// 获取申请列表数据
const shenqingList = ref([])
const getUserMonitoringFn = async () => {
  const res = await getUserMonitoringApi()
  if (res.code === 1) {
    shenqingList.value = res.data.data
  }
  console.log(shenqingList.value, 'shenqingList.value')
}

// 獲取新股列表
const dadanList = ref([])
const getXinguList = async () => {
  const res = await getDadanListApi()
  console.log(res)
  dadanList.value = res.data
}

// 獲取手續費
const userMoneyInfo = ref({})
const getUserMoney = async () => {
  const res = await getUserMoneyApi()
  res.data.sxf = Number(res.data.sxf)
  res.data.sxfzdi = Number(res.data.sxfzdi)
  userMoneyInfo.value = res.data
}

// 彈窗
const gupiaoWindowShow = ref(false)
const windowOptions = {
  position: 'bottom',
  'close-on-click-overlay': true,
  'safe-area-inset-bottom': true,
  round: true
}
const windowDetail = ref({
  hedging_price: 0
})
const openWindow = (e: any) => {
  windowDetail.value = e
  gupiaoWindowShow.value = true
}
const totalPrice = computed(() => {
  return buyParams.value.num * 100 * Number(windowDetail.value.hedging_price)
})
// const shouxufei = computed(() => {
//   const sxf = totalPrice.value * userMoneyInfo.value.sxf
//   if (sxf > userMoneyInfo.value.sxfzdi) {
//     return sxf
//   }
//   return userMoneyInfo.value.sxfzdi
// })

// 購買
const buyParams = ref({
  monitoring_id: '',
  num: 1
})
const buy = async () => {
  buyParams.value.monitoring_id = windowDetail.value.id
  const res = await buyDadanApi(buyParams.value)
  gupiaoWindowShow.value = false
  if (res.code === 1) {
    getUserMonitoringFn()
    showToast(res.msg)
  }
}
</script>

<style scoped lang="scss">
uni-view {
  text-align: center;
}
.flex_direction {
  flex-direction: column;
}
.text-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
.flex1 {
  width: 25%;
}
.text_ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.gupiao_list {
  margin-bottom: 0.31rem;
  .button_group {
    width: 21.5625rem;
    height: 2.75rem;
    background: rgb(8, 24, 43, 0.7);
    border-radius: 1.5625rem;
    margin: 0 auto;
    padding: 0.3125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .button {
      width: 10.3125rem;
      height: 2.125rem;
      line-height: 2.125rem;
      border-radius: 1.25rem;
      text-align: center;
      font-size: 0.875rem;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .active {
      background: #45d9fd;
      color: $uni-color-primary;
      font-weight: 500;
      font-size: 0.875rem;
    }
  }
  // padding: 0.62rem;
  .column,
  .gupiao {
    padding: 0.52rem 0.9rem;
    display: flex;
    background-color: rgb(11, 33, 59, 0.7);
    border-radius: 0.3125rem;
    margin-bottom: 0.625rem;
    > view {
      flex: 1;
      color: #a0a5b0;
      font-size: $uni-font-size-lg;
    }
  }
  .column {
    text-align: center;

    // background-color: $uni-color-gray;
  }
}

.buy_window {
  background-color: #fff;
  padding: 0 0.81rem 1.25rem 0.81rem;
  .title {
    text-align: center;
    padding: 0.8rem 0 0.5rem;
    font-size: 1rem;
  }
  .row_wrap {
    display: flex;
    flex-wrap: wrap;
    .row {
      display: flex;
      align-items: center;
      width: 50%;
      padding: 0.24rem 0.56rem;
      color: #fff;
      view {
        color: $uni-text-color;
        font-size: 0.775rem;
      }
    }
  }
  .button {
    height: 2.19rem;
    line-height: 2.19rem;
    width: 100%;
    text-align: center;
    border-radius: 1.03rem;
    background-color: #6a69e9;
    color: #fff;
    font-size: 0.81rem;
    margin-top: 1.25rem;
  }
}
</style>
