<template>
  <view class="page-wrapper">
    <JHeaderTitle title="拓展员详情" :bgColor="headerBgColor" color="#fff" imgUrl="/static/iconImg/left-white.svg" />
    <image src="/static/iconImg/bg-tuozhanyuan.svg" class="bg-image" mode="scaleToFill" />
    <view class="expand-header">
      <image src="/static/iconImg/expand-img.svg" mode="scaleToFill" />
      <view class="expand-title">{{ expandInfo.realname }}</view>
      <view class="expand-phone">{{ expandInfo.telphone }}</view>
      <view class="expand-edit bgF bdR10" @tap="jumpPage">
        <image src="/static/iconImg/expand-edit.svg" mode="scaleToFill" />
        编辑信息
      </view>
    </view>
    <JMainCard pd="0" bgColor="rgba(0,0,0,0.1)">
      <JInput name="拓展员姓名" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ expandInfo.realname }}</text></JInput
      >
      <JInput name="性别" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ expandInfo.sex == 1 ? "男" : expandInfo.sex == 2 ? "女" : "未知" }}</text></JInput
      >
      <JInput name="编号" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ expandInfo.userNo }}</text></JInput
      >
      <JInput name="所属团队" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ expandInfo.teamName }}</text></JInput
      >
      <JInput name="创建时间" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ expandInfo.createdAt }}</text></JInput
      >
      <JInput name="是否队长" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <view class="dis-wrapper">
          <text v-if="expandInfo.isTeamLeader == 1">是</text>
          <text v-else>否</text>
          <switch
            :checked="expandInfo.isTeamLeader == 1 ? true : false"
            @change="change($event, 'isTeamLeader')"
            style="margin-left: 20rpx; transform: scale(1.2)"
            color="#BF80FF"
          />
        </view>
      </JInput>
      <JInput name="拓展员状态" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <view class="dis-wrapper">
          <text v-if="expandInfo.state == 1">启用</text>
          <text v-else>禁用</text>
          <switch
            :checked="expandInfo.state == 1 ? true : false"
            style="margin-left: 20rpx; transform: scale(1.2)"
            color="#BF80FF"
            @change="change($event, 'state')"
          />
        </view>
      </JInput>
    </JMainCard>
    <view class="page-title">数据统计</view>
    <JMainCard pd="0" bgColor="rgba(0,0,0,0.1)" wrapPd="0 50rpx">
      <ScreenTitle bdR="20rpx 20rpx 0 0" @search="screening" :index="index" />
      <view class="mch-info">
        <view class="mch-many">
          <text>商户交易额（元）</text>
          {{ (mchInfo.payAmount / 100).toFixed(2) }}
        </view>
        <view class="mch-footer">
          <view>
            <text>新增商户数</text>
            {{ mchInfo.mchOnNetCount }}
          </view>
          <view>
            <text>新增入网商户数</text>
            {{ mchInfo.mchOnNetNewCount }}
          </view>
        </view>
      </view>
    </JMainCard>
    <JMainCard pd="0" bgColor="rgba(0,0,0,0.1)">
      <view class="mch-info">
        <view class="mch-many">
          <text>商户交易总额（元）</text>
          {{ (mchInfo.payAllAmount / 100).toFixed(2) }}
        </view>
        <view class="mch-footer">
          <view>
            <text>商户总数</text>
            {{ mchInfo.mchTodayAddCount }}
          </view>
          <view>
            <text>入网商户总数</text>
            {{ mchInfo.mchOnNetNewCount }}
          </view>
        </view>
      </view>
    </JMainCard>
    <JButton
      color="#FF4D6A"
      bgColor="rgba(255,255,255,0.85)"
      pd="0 50rpx 50rpx"
      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, onUnmounted } from "vue"
import { onLoad, onShow, onPageScroll } from "@dcloudio/uni-app"
import { $devDetail, $editDev, $getDevDetail, $delSysUser } 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((options) => {
  console.log("options", options)
  params.sysUserId = options.id
})
onShow(() => {
  if (params.sysUserId) {
    getInfo(params.sysUserId)
    getDevDetail()
  }
})
const expandInfo = ref({})
const mchInfo = ref({})
const deletedTips = ref(null)
const switchTips = ref(null)
const index = ref(0)
const params = {
  queryDateRange: "",
}
const deleted = () => {
  $delSysUser(expandInfo.value.sysUserId).then((res) => {
    uni.showToast({
      title: "删除成功",
      icon: "success|none",
      mask: true,
    })
    uni.navigateBack()
  })
}
const getInfo = (val) => {
  $devDetail(val).then(({ bizData }) => {
    expandInfo.value = bizData
  })
}
const screening = (data) => {
  index.value = data.i
  if (data.val.value != "customer") {
    params.queryDateRange = data?.val.value
    getDevDetail()
  }
}
const getDevDetail = () => {
  $getDevDetail(params).then((res) => {
    mchInfo.value = res.bizData
  })
}

let keys = null
const change = (e, val) => {
  keys = val
  expandInfo.value[val] = e.detail.value ? 1 : 0
  switchTips.value.open("确认修改吗？")
}
const confirm = () => {
  const data = {}
  data[keys] = expandInfo.value[keys]
  keys = null
  $editDev(expandInfo.value.sysUserId, data).then(() => {
    Object.assign(data, expandInfo.value)
    uni.showToast({
      title: "修改成功",
      icon: "success",
    })
  })
}
const cancel = () => {
  if (!keys) return
  expandInfo.value[keys] = Number(!expandInfo.value[keys])
}
const jumpPage = () => {
  uni.navigateTo({ url: "/pageWork/developerManagement/editDeveloper?id=" + expandInfo.value.sysUserId })
}
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;
  width: 100%;
  min-height: 100%;
  background-color: $primaryColor;
  text {
    color: #fff;
    font-weight: 400;
  }
  .bg-image {
    position: absolute;
    top: -40rpx;
    right: 0;
    width: 100%;
    height: 650rpx;
  }
  .expand-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    image {
      width: 93rpx;
      height: 93rpx;
    }
    .expand-title {
      margin-top: 20rpx;
      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;
  }
  .page-title {
    margin-bottom: 30rpx;
    text-align: center;
    font-size: 33rpx;
    color: #fff;
  }
  .mch-info {
    border-top: 1rpx solid rgba(0, 0, 0, 0.1);
    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;
      margin-top: 50rpx;
      view {
        flex: 1;
        font-size: 33rpx;
        font-weight: 700;
        color: #fff;
      }
    }
  }
}
</style>
