<template>
  <Navigater :title="t('ipo.title')" />
  <!-- <view class="ul">
    <view class="li">
      <view class="flex items-center">
        <image src="/static/image/index/ipo/icon1.png"></image>
        <text class="text-lg">{{ t('ipo.title1') }}</text>
      </view>
      <van-icon name="arrow" color="#50aff0" />
    </view>
    <view class="li">
      <view class="flex items-center">
        <image src="/static/image/index/ipo/icon2.png"></image>
        <text class="text-lg">{{ t('ipo.title2') }}</text>
      </view>
      <van-icon name="arrow" color="#50aff0" />
    </view>
  </view> -->
  <view class="button_group">
    <view class="button" :class="{ active: pageType === 0 }" @click="changePageType(0)">{{ t('ipo.button1') }}</view>
    <view class="button" :class="{ active: pageType === 1 }" @click="changePageType(1)">{{ t('ipo.button2') }}</view>
  </view>

  <scroll-view scroll-y :style="{ height: 'calc(100vh - 6.25rem)' }">
    <view class="gupiao_list">
      <view v-show="pageType === 0">
        <view v-for="(item, index) in shengoulist" :key="index" class="gupiao" @click="openWindow(item)">
          <view class="flex items-center justify-between pb-[0.4rem]">
            <view>
              <view class="flex items-center">
                <view class="font-bold" style="color: #45d9fd; max-width: 5.625rem; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; word-break: break-all">{{ item?.name }}</view>
                <view class="icon text-base">{{ item?.shuzidaima }}</view>
              </view>
              <view style="display: flex; align-items: center; font-size: 0.75rem; color: #fff; font-weight: 500">
                <view class="label" style="color: #fff; font-size: 0.8125rem">{{ t('ipo.shijian') }}：</view>
                <view class="value" style="color: #fff; font-size: 0.8125rem">{{ item.chouqiandate }}</view>
              </view>
            </view>
            <view class="icon_primary text-lg">{{ t('ipo.button1') }}</view>
          </view>
          <view class="info-box">
            <view class="row">
              <view class="label" style="color: #a6a6a6; font-size: 0.8125rem">{{ t('ipo.jiage') }}：</view>
              <view class="value" style="color: #fff; font-size: 0.8125rem">{{ changeMoney(item.chengxiaojia) }}</view>
            </view>
            <view class="row">
              <view class="label" style="color: #a6a6a6; font-size: 0.8125rem">{{ t('ipo.shuliang') }}：</view>
              <view class="value" style="color: #fff; font-size: 0.8125rem">{{ item.zongshengou }}</view>
            </view>
          </view>
        </view>
        <NotData v-if="shengoulist.length === 0"></NotData>
      </view>
      <view v-show="pageType === 1">
        <!-- <view v-for="(item, index) in jieshulist" :key="index" class="gupiao">
          <view class="flex items-center justify-between pb-[0.4rem]">
            <view class="flex items-center">
              <view class="font-bold text-primary">{{ item?.name }}</view>
              <view class="icon text-base">{{ item?.shuzidaima }}</view>
            </view>
            <view class="icon text-lg">{{ t('ipo.button2') }}</view>
          </view>
          <view class="flex justify-between">
            <view class="half">
              <view class="row">
                <view class="label">{{ t('ipo.jiage') }}</view>
                <view class="value">{{ changeMoney(item.chengxiaojia) }}</view>
              </view>
              <view class="row">
                <view class="label">{{ t('ipo.shuliang') }}</view>
                <view class="value">{{ item.zongshengou }}</view>
              </view>
            </view>
            <view class="half">
              <view class="row">
                <view class="label">{{ t('ipo.shijian') }}</view>
                <view class="value">{{ item.chouqiandate }}</view>
              </view>
            </view>
          </view>
        </view> -->
        <NotData v-if="xinguApply.length === 0"></NotData>
        <view v-for="(item, index) in xinguApply" :key="index" class="gupiao">
          <view class="flex items-center justify-between pb-[0.4rem]">
            <view class="flex items-center">
              <view class="font-bold text-primary" style="color: #fff">{{ item?.product?.name }}</view>
              <view class="icon text-base">{{ item?.product?.shuzidaima }}</view>
            </view>
            <view class="icon text-lg">{{ getText(item) }}</view>
          </view>
          <view class="flex justify-between">
            <view class="half">
              <!-- <view class="row">{{ t('jiaoyi.dingdanxiangqing') }}</view> -->
              <view class="row">
                <view class="label">{{ t('jiaoyi.jiage') }}</view>
                <view class="value">{{ changeMoney(item.price) }}</view>
              </view>
              <view class="row">
                <view class="label">{{ t('jiaoyi.shenqingliang') }}</view>
                <view class="value">{{ item.shengoushuliang }}</view>
              </view>
              <view class="row">
                <view class="label">{{ t('jiaoyi.chouqianri') }}</view>
                <view class="value">{{ item?.product?.chouqiandate }}</view>
              </view>
              <!-- <view class="row">
                <view class="label">{{ t('jiaoyi.faquanri') }}</view>
                <view class="value">{{ item?.product?.faquan_date }}</view>
              </view> -->
            </view>
            <view class="half">
              <!-- <view class="row">{{ t('jiaoyi.xianzaixianqging') }}</view> -->
              <view class="row">
                <view class="label">{{ t('jiaoyi.zhognqianshuliang') }}</view>
                <view class="value">{{ item.zhongqianshu }}</view>
              </view>
              <view class="row">
                <view class="label">{{ t('jiaoyi.renjiaogushu') }}</view>
                <view class="value">{{ item.renjiaonum || 0 }}</view>
              </view>
              <view class="row">
                <view class="label">{{ t('jiaoyi.txt1') }}</view>
                <view class="value">{{ changeMoney(item.yingrenjiao) }}</view>
              </view>
              <view class="row">
                <view class="label">{{ t('jiaoyi.txt2') }}</view>
                <view class="value">{{ changeMoney(item.yirenjiao) }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </scroll-view>

  <van-popup v-model:show="gupiaoWindowShow" class="buy_window" v-bind="windowOptions">
    <view class="title">{{ windowDetail.name }}/{{ windowDetail.shuzidaima }}</view>
    <view class="row">
      <view class="label">{{ t('ipo.windowLabel1') }}</view>
      <view class="value">{{ changeMoney(windowDetail.chengxiaojia) }}</view>
    </view>
    <view class="row">
      <view class="label">{{ t('ipo.windowLabel2') }}</view>
      <!-- <view class="value input_wrap"> -->
      <van-field v-model="shengouParams.shuliang" type="digit" />
      <!-- <span style="margin-left: 0.19rem; font-size: 0.88rem">{{ t('ipo.zang') }}</span> -->
      <!-- <input v-model="shengouParams.number" type="number" /> -->
      <!-- </view> -->
    </view>
    <!-- <view class="row">
      <view class="label">{{ t('ipo.windowLabel3') }}</view>
      <view class="value">{{ changeMoney(windowDetail.chengxiaojia * shengouParams.shuliang * 100) }}</view>
    </view> -->
    <view class="button" @click="shengou">{{ t('gupiaoDetail.submit') }}</view>
  </van-popup>
</template>

<script setup lang="ts">
import { getXinguListApi, shengouApi, getXinguApplyApi } from '@/api/index/index'
import { onHide, onShow } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n'
import { ref } from 'vue'
import { changeMoney } from '@/common/common'
const { t } = useI18n()

let id: any = null

onShow(() => {
  getXinguList()
  getXinguApply()
  id = setInterval(() => {
    getXinguApply()
  }, 10000)
})

onHide(() => {
  clearInterval(id)
})

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

const getText = (item) => {
  if (item.status === '0') {
    return t('jiaoyi.txt3')
  } else if (item.status === '1' && item.is_examine === 0) {
    return t('jiaoyi.txt4')
  } else if (item.status === '1' && item.is_examine === 1) {
    return t('jiaoyi.txt5')
  } else if (item.status === '1' && item.is_examine === 2) {
    return t('jiaoyi.txt6')
  } else if (item.status === '2') {
    return t('jiaoyi.txt6')
  }
}

const shengoulist = ref([])
const jieshulist = ref([])
const xinguApply = ref([])
const getXinguApply = async () => {
  const res = await getXinguApplyApi()
  if (res.code === 1) {
    xinguApply.value = res.data
    console.log(res, 'res')
  }
}

const getXinguList = async () => {
  const res = await getXinguListApi()
  shengoulist.value = res.data.data.filter((item: any) => item.status === '1')
  jieshulist.value = res.data.data.filter((item: any) => item.status === '2')
  console.log(jieshulist.value, 'jieshulist.value')

  console.log(shengoulist.value)
}

const gupiaoWindowShow = ref(false)
const windowOptions = {
  position: 'bottom',
  'close-on-click-overlay': true,
  'safe-area-inset-bottom': true,
  round: true
}

const windowDetail = ref({})
const openWindow = (e: any) => {
  windowDetail.value = e
  console.log(windowDetail.value)
  gupiaoWindowShow.value = true
}

const shengouParams = ref({
  price: '',
  shuliang: 100,
  id: ''
})
const shengou = async () => {
  shengouParams.value.id = windowDetail.value.id
  shengouParams.value.price = windowDetail.value.chengxiaojia
  const res = await shengouApi(shengouParams.value)
  gupiaoWindowShow.value = false
  uni.showToast({
    title: res.data.msg,
    icon: 'none'
  })
}
</script>

<style scoped lang="scss">
.info-box {
  display: flex;
  width: 20.9375rem;
  height: 2.3125rem;
  border-radius: 0.1563rem;
  background: rgb(11, 33, 58, 0.8);
  padding-left: 0.625rem;
  .row {
    flex: 0.8;
    align-items: end;
    justify-content: flex-start;
  }
}

.van-field {
  width: 50%;

  border: 0.03rem solid $uni-color-primary;
  padding: 0.12rem 0.5rem;
  border-radius: 0.16rem;
}
.ul {
  background-color: #fff;
  margin-top: 0.88rem;
  .li {
    height: 2rem;
    padding: 0 1.09rem 0 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    image {
      width: 0.94rem;
      height: 0.94rem;
      margin-right: 0.63rem;
    }
  }
  .li + .li {
    border-top: 0.03rem solid $uni-text-color;
  }
}
.button_group {
  width: 21.5625rem;
  height: 2.75rem;
  background: #08182b;
  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;
  }
}

.gupiao_list {
  view {
  }
  .gupiao {
    // background-color: #fff;
    margin-bottom: 0.31rem;
    padding: 0.62rem 1.25rem;
    .half {
      width: 48%;
    }
    .row {
      font-size: 0.8rem;
      color: #0a0a0a;
      display: flex;
      align-items: center;
      padding: 0.21rem 0;
      justify-content: flex-start;
      gap: 0.63rem;
      .value {
        color: #fff;
      }
      view {
        color: $uni-text-color-gray;
        font-size: $uni-font-size-lg;
      }
    }
  }
  .icon {
    background: #0575e6;
    padding: 0.14rem 0.44rem;
    border-radius: 0.31rem;
    color: #fff;
    font-size: 0.75rem;
    margin-left: 1rem;
    // font-size: $uni-font-size-base;
  }
  .icon_primary {
    width: 7.5rem;
    height: 2.125rem;
    background: $uni-color-primary;
    // padding: 0.18rem 1rem;
    text-align: center;
    line-height: 2.125rem;
    border-radius: 1.5625rem;
    color: #fff;
    font-size: $uni-font-size-lg;
  }
  .icon_o {
    background: #fff;
    padding: 0.18rem 1rem;
    border-radius: 0.31rem;
    color: $uni-color-primary;
    border: 0.03rem solid $uni-color-primary;
    margin-left: 1rem;
    font-size: $uni-font-size-lg;
  }
}

.buy_window {
  background-color: #fff;
  padding: 0 0.81rem 1.25rem 0.81rem;
  .row {
    display: flex;
    justify-content: start;
    align-items: center;
    padding: 0.44rem 0.56rem;
    color: #fff;
    view {
      color: $uni-text-color;
      font-size: 0.81rem;
      flex: 1;
      width: auto;
    }
    .label {
      width: 6rem;
      flex: 2;
    }
    .van-field {
      width: 35%;
    }
  }
  .title {
    text-align: center;
    padding: 1rem 0 0.5rem;
    font-size: 1rem;
  }
  .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;
  }
  .input_wrap {
    border: 0.03rem solid $uni-color-primary;
    padding: 0.12rem 0.5rem;
    border-radius: 0.16rem;
  }
}
</style>
