<template>
  <view class="page-wrapper">
    <JHeaderTitle title="门店详情" :bgColor="headerBgColor" color="#fff" imgUrl="/static/iconImg/left-white.svg" />
    <image src="/static/iconImg/store-bg-img.svg" class="bg-image" mode="scaleToFill" />
    <view class="expand-header">
      <image src="/static/iconImg/icon-store-img.svg" mode="scaleToFill" />
      <view class="expand-title">{{ storeInfo.storeName }}</view>
      <view class="expand-phone">{{ storeInfo.storeId }}</view>
      <view class="expand-edit bgF bdR10" @tap="editStore">
        <image src="/static/iconImg/expand-edit.svg" mode="scaleToFill" />
        编辑信息
      </view>
    </view>
    <JMainCard pd="0" wrapPd="0 50rpx" bgColor="rgba(0,0,0,0.1)">
      <JUpLoad
        name="门店logo"
        pdLeft="0"
        pd="40rpx"
        :imgUrl="storeInfo.storeLogo || imgUrl"
        borderNone
        textColor="rgba(255,255,255,0.6)"
      />
      <JInput name="门店名称" pd="0 40rpx 40rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.storeName }}</text></JInput
      >
      <JInput name="门店ID" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.storeId }}</text></JInput
      >
      <JInput name="所属商户" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.mchName }}</text></JInput
      >
      <JInput name="用户号" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.mchNo }}</text></JInput
      >
      <JInput name="联系人手机号" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.contactPhone }}</text></JInput
      >
      <JInput name="门店地址" pd="0 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.address }}</text></JInput
      >
      <JInput name="经纬度" pd="40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.lng }}/{{ storeInfo.lat }}</text></JInput
      >
      <JUpLoad
        name="门头照"
        pd="0 40rpx"
        pdLeft="0"
        :imgUrl="storeInfo.storeOuterImg || imgUrl"
        borderNone
        textColor="rgba(255,255,255,0.6)"
      />
      <JUpLoad
        name="门店内景照"
        pd="40rpx"
        pdLeft="0"
        :imgUrl="storeInfo.storeInnerImg || imgUrl"
        borderNone
        textColor="rgba(255,255,255,0.6)"
      />
      <JInput name="创建时间" pd="0 40rpx 40rpx 40rpx" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <text class="right-color">{{ storeInfo.createdAt?.split("-").join("/") }}</text></JInput
      >
    </JMainCard>

    <JMainCard pd="0" wrapPd="30rpx 50rpx 0 50rpx" bgColor="rgba(0,0,0,0.1)">
      <JInput name="是否默认门店" textColor="rgba(255,255,255,0.6)" :isBorder="true">
        <view class="tips" v-if="storeInfo.defaultFlag == 1"
          >是<text
            class="tips-color"
            :style="{ backgroundColor: storeInfo.defaultFlag == 1 ? '#FFCA80' : 'rgba(255, 255, 255, 0.3)' }"
          ></text
        ></view>
        <view v-else>
          <switch
            :checked="storeInfo.defaultFlag"
            style="margin-left: 20rpx; transform: scale(1.2)"
            color="#BF80FF"
            @change="change"
          />
        </view>
      </JInput>
    </JMainCard>

    <JButton color="#FF4D6A" bgColor="rgba(255,255,255,0.85)" size="max" pdTop="0" @HandleTouch="deletedTips.open()"
      >删除门店</JButton
    >
  </view>
  <JDeletedTips ref="deletedTips" @confirm="deleted" />
  <JDeletedTips ref="switchState" @confirm="confirm" @cancel="cancel" />
  <!-- 更改状态对话框 -->
</template>

<script setup>
import { nextTick, ref } from "vue"
import { onLoad, onShow, onPageScroll } from "@dcloudio/uni-app"
import { $storeDetails, $delStore, $updateMchStore } from "@/http/apiManager.js"
import { formatData } from "@/hooks/formatData"
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 JUpLoad from "@/components/newComponents/JUpLoad/JUpLoad"
import JDeletedTips from "@/components/newComponents/JDeletedTips/JDeletedTips"
onLoad((option) => {
  storeId = option?.id || storeId
})
onShow(() => {
  getStoreInfo(storeId)
})
let storeId = undefined
const storeInfo = ref({})
const switchState = ref(null)
const imgUrl = ref("/static/iconImg/defaultImg.svg")
const deletedTips = ref(null)
const deleted = () => {
  $delStore(storeInfo.value.storeId).then((res) => {
    uni.showToast({
      title: "删除成功",
      icon: "success",
    })
    uni.navigateBack()
  })
}
const getStoreInfo = (val) => {
  $storeDetails(val).then(({ bizData }) => {
    formatData(bizData)
    storeInfo.value = bizData
  })
}
let flag = undefined
const change = (e) => {
  flag = true
  storeInfo.value.defaultFlag = Number(e.detail.value)
  switchState.value.open("是否修改门店为默认门店？")
}
const confirm = () => {
  flag = false
  $updateMchStore(storeInfo.value.storeId, { defaultFlag: storeInfo.value.defaultFlag }).then((res) => {
    uni.showToast({
      title: "修改成功",
      icon: "none",
    })
  })
}
const cancel = () => {
  if (!flag) return
  storeInfo.value.defaultFlag = Number(!storeInfo.value.defaultFlag)
}
const editStore = () => {
  uni.navigateTo({
    url: "./addOrEditStore?id=" + storeInfo.value.storeId,
  })
}
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;
    margin-top: 50rpx;
    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: 40;
      padding: 20rpx 41rpx;
      margin-bottom: 48rpx;
      font-size: 28rpx;
      color: $primaryColor;
      image {
        width: 26.25rpx;
        height: 26.25rpx;
      }
    }
  }
  .right-color {
    color: #fff;
    font-size: 30rpx;
  }
  .page-title {
    margin: 50rpx 0 30rpx 0;
    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;
      }
    }
  }
}
.tips {
  color: #fff;
  .tips-color {
    display: inline-block;
    width: 35rpx;
    height: 35rpx;
    margin-left: 10rpx;
    vertical-align: middle;
    border-radius: 50%;
  }
}
</style>
