<template>
  <view class="page-wrapper global-wrapper" :class="{ 'page-show': agentInfo.state }">
    <JHeaderTitle title="代理商详情" :bgColor="headerBgColor" color="#fff" imgUrl="/static/iconImg/left-white.svg" />
    <image src="/static/iconImg/agent-bg-img .svg" class="bg-image" mode="scaleToFill" />
    <view class="expand-header">
      <image src="/static/iconImg/icon-agent-img.svg" mode="scaleToFill" />
      <view class="expand-title">{{ agentInfo.agentName }}</view>
      <view class="expand-phone">{{ agentInfo.agentNo }}</view>
      <view class="expand-edit bgF bdR10" @tap="editAgent">
        <image src="/static/iconImg/expand-edit.svg" mode="scaleToFill" />
        编辑信息
      </view>
    </view>
    <JMainCard pd="0" bgColor="rgba(0,0,0,0.1)">
      <JInput name="代理商全称" pd="30rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.agentName }}</text></JInput
      >
      <JInput name="代理商简称" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.agentShortName }}</text></JInput
      >
      <JInput name="代理商号" pd="30rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.agentNo }}</text></JInput
      >
      <JInput name="联系人姓名" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.contactName }}</text></JInput
      >
      <JInput name="联系人手机号" pd="30rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.contactTel }}</text></JInput
      >
      <JInput name="联系人邮箱" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.contactEmail }}</text></JInput
      >
      <JInput name="创建时间" pd="30rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ agentInfo.createdAt?.split("-").join("/") }}</text></JInput
      >
      <JInput name="代理商状态" pd="0 40rpx 30rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <view v-if="agentInfo.state == 1 || agentInfo.state == 0" class="dis-wrapper">
          <text v-if="agentInfo.state == 1">启用</text>
          <text v-else>禁用</text>
          <switch
            :checked="agentInfo.state == 1 ? true : false"
            style="margin-left: 20rpx; transform: scale(1.2)"
            color="#BF80FF"
            @change="change"
          />
        </view>
        <view class="agent-tips" v-else
          >{{ tipsList[agentInfo.state]?.text }}
          <text class="tips-color" :style="{ backgroundColor: tipsList[agentInfo.state]?.bgColor }"></text>
        </view>
      </JInput>
    </JMainCard>
    <template v-if="agentInfo.state == 1 || agentInfo.state == 0">
      <view class="page-title">经营统计</view>
      <JMainCard pd="0" wrapPd="0 50rpx" bgColor="rgba(0,0,0,0.1)">
        <ScreenTitle bdR="20rpx 20rpx 0 0" @search="searchTime" :index="index" />
        <view class="mch-info">
          <view class="mch-many">
            <text>收款金额（元）</text>
            {{ (orderCount.payAmount / 100).toFixed(2) }}
          </view>
          <view class="mch-footer payCount">
            <view>
              <text>收款笔数（笔）</text>
              {{ orderCount.payCount }}
            </view>
          </view>
          <view class="mch-footer">
            <view>
              <text>退款金额（元）</text>
              {{ (orderCount.refundAmount / 100).toFixed(2) }}
            </view>
            <view>
              <text>退款笔数（笔）</text>
              {{ orderCount.refundCount }}
            </view>
          </view>
        </view>
      </JMainCard>
      <JMainCard pd="8rpx" wrapPd="30rpx 50rpx " bgColor="rgba(0,0,0,0.1)">
        <view class="mch-info">
          <view class="mch-footer">
            <view>
              <text>商户总数</text>
              {{ orderCount.mchAllCount }}
            </view>
            <view>
              <text>新增商户数</text>
              {{ orderCount.refundCount }}
            </view>
          </view>
        </view>
      </JMainCard>
    </template>
    <JButton color="#FF4D6A" bgColor="rgba(255,255,255,0.85)" size="medium" pdTop="0" @HandleTouch="deletedTips.open()"
      >删除代理商</JButton
    >
  </view>
  <JDeletedTips ref="deletedTips" @confirm="deleted" />
  <JDeletedTips ref="switchTips" @confirm="confirm" @cancel="cancel" />
</template>

<script setup>
import { reactive, ref } from "vue"
import { onLoad, onShow, onPageScroll } from "@dcloudio/uni-app"
import { formatData } from "@/hooks/formatData"
import { $findAgent, $getStatic, $editAgent, $delAgent } from "@/http/apiManager.js"
import JHeaderTitle from "@/components/newComponents/JHeaderTitle/JHeaderTitle"
import JMainCard from "@/components/newComponents/JMainCard/JMainCard"
import JInput from "@/components/newComponents/JInput/JInput"
import JButton from "@/components/newComponents/JButton/JButton"
import JDeletedTips from "@/components/newComponents/JDeletedTips/JDeletedTips"
import ScreenTitle from "@/components/newComponents/ScreenTitle/ScreenTitle"
onLoad((option) => {
  params.agentNo = option.agentNo
})
onShow(() => {
  getAgentInfo()
})
const tipsList = reactive([
  {},
  {},
  {
    text: "认证中",
    bgColor: "#FFCA80",
  },
  {
    text: "认证驳回",
    bgColor: "#FF4D6A",
  },
  {
    text: "待认证",
    bgColor: "rgba(255, 255, 255, 0.3)",
  },
])
const switchTips = ref(null)
const agentInfo = ref({})
const orderCount = ref({})
const index = ref(0)
const params = {
  queryDateRange: "",
  countType: 3,
  agentNo: "",
}
const deletedTips = ref(null)
const deleted = () => {
  $delAgent(params.agentNo).then((res) => {
    uni.showToast({
      title: "删除成功",
      icon: "success",
    })
    uni.navigateBack()
  })
}
const getAgentInfo = () => {
  getStatic()
  $findAgent(params.agentNo).then(({ bizData }) => {
    formatData(bizData)
    agentInfo.value = bizData
  })
}
const getStatic = (data) => {
  $getStatic(params).then(({ bizData }) => {
    orderCount.value = bizData.orderCount
    orderCount.value.mchAllCount = bizData.mchCount.mchAllCount
    orderCount.value.mchOnNetCount = bizData.mchCount.mchOnNetCount
  })
}
const searchTime = (data) => {
  index.value = data.i
  if (data.val.value != "customer") {
    params.queryDateRange = data?.val.value
    getStatic()
  }
}
let flag = undefined
const change = (e) => {
  flag = true
  agentInfo.value.state = e.detail.value ? 1 : 0
  switchTips.value.open("确认修改吗？")
}
const confirm = () => {
  flag = false
  $editAgent(agentInfo.value.agentNo, { state: agentInfo.value.state }).then(() => {
    uni.showToast({
      title: "修改成功",
      icon: "success",
    })
  })
}
const cancel = (val) => {
  if (!flag) return
  agentInfo.value.state = Number(!agentInfo.value.state)
}
function editAgent() {
  uni.navigateTo({
    url: "./editAgent?agentNo=" + params.agentNo,
  })
}
const headerBgColor = ref("transparent")
onPageScroll((data) => {
  if (data.scrollTop > 20) {
    headerBgColor.value = "$primaryColor"
  } else {
    headerBgColor.value = "transparent"
  }
})
</script>

<style lang="scss" scoped>
.page-wrapper {
  position: relative;
  background-color: $primaryColor;
  opacity: 0;
  text {
    color: #fff;
  }
  .bg-image {
    position: absolute;
    top: -40rpx;
    right: 0;
    width: 100%;
    height: 650rpx;
  }
  .expand-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 50rpx;
    image {
      width: 93rpx;
      height: 93rpx;
    }
    .expand-title {
      width: 500rpx;
      margin: 20rpx auto 0;
      word-break: break-all;
      text-align: center;
      font-size: 33rpx;
      font-weight: 700;
      color: #fff;
    }

    .expand-phone {
      margin: 30rpx 0;
      color: rgba(255, 255, 255, 0.6);
      font-size: 25rpx;
    }
    .expand-edit {
      position: relative;
      z-index: 30;
      padding: 20rpx 41rpx;
      font-size: 28rpx;
      color: $primaryColor;
      image {
        width: 26.25rpx;
        height: 26.25rpx;
      }
    }
  }
  .right-color {
    color: #fff;
    font-size: 30rpx;
  }
  .title-name {
    display: block;
    width: 400rpx;
  }
  .page-title {
    padding-bottom: 30rpx;
    text-align: center;
    font-size: 33rpx;
    color: #fff;
  }
  .mch-info {
    padding: 60rpx;
    view {
      display: flex;
      flex-direction: column;
    }
    text {
      margin-bottom: 20rpx;
      color: rgba(255, 255, 255, 0.6);
      font-size: 25rpx;
      font-weight: 500;
    }
    .mch-many {
      color: #fff;
      font-size: 56rpx;
      font-weight: 700;
    }
    .mch-footer {
      display: flex;
      flex-direction: row;
      view {
        flex: 1;
        font-size: 33rpx;
        font-weight: 700;
        color: #fff;
      }
    }
    .payCount {
      margin: 48rpx 0;
    }
  }
}

.agent-tips {
  font-size: 30rpx;
  color: #fff;
  .tips-color {
    display: inline-block;
    width: 35rpx;
    height: 35rpx;
    margin-left: 10rpx;
    vertical-align: sub;
    border-radius: 50%;
    background-color: #ffca80;
  }
}
.page-show {
  opacity: 1;
  transition: opacity 0.3s linear;
}
</style>
