<template>
  <view class="skuList">
    <uni-swipe-action>
      <uni-swipe-action-item
        v-for="(sku, i) in skuList"
        :key="i"
        :right-options="options"
        :autoClose="skuList.length > 20 ? false : true"
        :disabled="data.status != 110"
        @click="orderRefund(sku)"
      >
        <view v-if="sku.skuID">
          <view
            class="sku-item-for-orderdetail pd20"
            @tap="
              toGoodDetail(sku.nameID, sku.skuID, sku.comparePrice, sku.skuName)
            "
          >
            <view class="img" @tap.stop="previewImg(sku)">
              <!-- 火 -->
              <view
                v-if="
                  !isNewPriceDisplay &&
                  sku.storeSubID &&
                  storeInfo.payPercentage >= 50
                "
                style="left: 10rpx"
                class="promtion-sku-icon2"
              ></view>
              <!-- 促 -->
              <view
                v-if="
                  !isNewPriceDisplay &&
                  sku.salePrice < sku.shopPrice &&
                  !sku.storeSubID &&
                  storeInfo.payPercentage >= 50
                "
                style="left: 10rpx"
                class="promtion-sku-icon"
              ></view>
              <image mode="scaleToFill" :src="sku.image" />
            </view>

            <view class="right" v-if="data">
              <view class="sku-name">{{ sku.skuName }}</view>
              <view class="sku-spec">
                <!-- 火 -->
                <view
                  class="sku-price earning-price"
                  v-if="
                    !isNewPriceDisplay &&
                    sku.storeSubID &&
                    storeInfo.payPercentage >= 50
                  "
                >
                  <view class="price2"
                    >结算价:
                    <jx-price :price="sku.shopPrice" />
                  </view>
                </view>
                <!-- 有结算价但不是活动品 -->
                <view
                  class="sku-price"
                  v-else-if="
                    !isNewPriceDisplay &&
                    sku.comparePrice * 100 >= sku.earningPrice &&
                    sku.storeSubID &&
                    storeInfo.payPercentage >= 50
                  "
                >
                  <text class="price1">¥{{ sku.shopPrice / 100 }}</text>
                </view>
                <!-- 促 普通 -->
                <view class="sku-price" v-else>
                  <text
                    class="price1"
                    :class="{
                      inLine: realPointStore
                        ? sku.saleSalePrice != sku.comparePrice
                        : sku.earnPrice != sku.comparePrice,
                    }"
                    >¥{{ sku.comparePrice }}</text
                  >
                  <text
                    class="price1"
                    v-if="!realPointStore && sku.earnPrice != sku.comparePrice"
                    >¥{{ sku.earnPrice }}</text
                  >
                  <text
                    class="price1"
                    v-if="
                      realPointStore && sku.saleSalePrice != sku.comparePrice
                    "
                    >¥{{ sku.saleSalePrice }}</text
                  >
                </view>
                <!-- 普通 -->
                <view class="sku-count" :class="{ strong: sku.count > 1 }"
                  >数量: {{ sku.count }}</view
                >
                <jx-icon icon="shuxie" class="icon-modify" color="#999999" />
              </view>
              <view class="sku-info">
                <view class="sku-location-code" v-if="sku.location_code && sku.location_code !== 'EMPTY_VALUE'">货架码：{{ sku.location_code }}</view>
                <view class="sku-stock">现库存：{{ sku.skuStock }}</view>
              </view>
              <view class="totalPrice" v-if="sku.count > 1"
                >总{{ isNewPriceDisplay ? '' : '价' }}:
                <jx-price
                  v-if="!realPointStore && sku.earnPrice != sku.comparePrice"
                  :price="sku.earnPrice * 100 * sku.count"
                  color="#50b335"
                />
                <jx-price
                  v-else-if="
                    realPointStore && sku.saleSalePrice != sku.comparePrice
                  "
                  :price="sku.saleSalePrice * 100 * sku.count"
                  color="#50b335"
                />
                <jx-price
                  v-else
                  :price="sku.comparePrice * 100 * sku.count"
                  color="#50b335"
                />
              </view>
              <view v-if="sku.upc" class="upc"> UPC码:{{ sku.upc }} </view>
            </view>
          </view>
        </view>
      </uni-swipe-action-item>
    </uni-swipe-action>

    <view v-for="(sku, i) in skuList" :key="i">
      <view v-if="!sku.skuID" class="skuListWrap-1">
        <!-- 左边 -->
        <view class="left">
          <image
            class="img"
            src="https://image.jxc4.com/image/5c506c8db41719fe4c433979498eb1e7.png"
            mode="scaleToFill"
          />

          <view class="right">
            <view class="name">{{ sku.skuName }}</view>
            <view class="price price-count" v-if="sku.count > 1">
              总价:
              <jx-price
                v-if="realPointStore"
                color="#4fb433"
                :price="sku.salePrice * sku.count"
              />
              <jx-price
                v-else
                color="#4fb433"
                :price="sku.salePrice * sku.count * 0.7"
              />
            </view>
            <view class="price" v-else>
              <jx-price
                v-if="realPointStore"
                color="#f84a82"
                :price="sku.salePrice"
              />
              <jx-price v-else color="#f84a82" :price="sku.salePrice * 0.7" />
            </view>
          </view>
        </view>

        <!--  -->
        <view class="sku-count-1">数量：{{ sku.count }}</view>
      </view>
    </view>
  </view>

  <goodsRefund ref="refundRef" :data="refundData" @confirm="confirm" />
</template>
  
<script lang="ts" setup>
import order from '@/api/https/order'
import useGlobalFunc from '@/composables/useGlobalFunc'
import goodsRefund from './goodsRefund.vue'
import { store } from '@/store'
import toast from '@/utils/toast'
import { computed, ref } from 'vue'
import { setStorage } from '@/utils/storage'
const { previewImage } = useGlobalFunc()

/*************************************************
 * 预览图片
 */
 function previewImg(skuName:AnyObject) {
  let arr = [skuName.image,skuName.image2,skuName.image3,skuName.image4,skuName.image5].filter(item => { return item && item.length > 0 })
  previewImage(arr)
}

/*************************************************
 * 滑动数据
 */
const options = ref<Array<AnyObject>>([
  {
    text: '退款',
    style: {
      backgroundColor: '#ff0000',
    },
  },
])

/*************************************************
 * 接收参数
 */
type goodsListType = {
  skuList: Array<AnyObject>
  data: AnyObject
  realPointStore: boolean
}
const props = defineProps<goodsListType>()

/*************************************************
 * 是否显示价格
 * 获取门店数据
 */
const isNewPriceDisplay: any = computed(() => {
  store.getters['storeInfo/isNewPriceDisplay']
})
const storeInfo = ref<AnyObject>(store.state.storeInfo.allStoreInfo)

/*************************************************
 * 一个商品退款
 */
function orderRefund(sku: AnyObject) {
  if (sku.count === 1) {
    uni.jxConfirm({
      title: '提示',
      content: '是否对该订单创建售后单\r\n进行【退款】处理',
      success: () => {
        if(props.data.buyerComment === '支付方式：扫码枪'){
          refundOnlineOrder(sku,1)
        }else{
          let arr = [
            {
              skuID: sku.skuID,
              count: sku.count,
              storeSubID: sku.storeSubID,
            },
          ]
          PartRefundOrder(arr)
        }
      },
    })
  } else {
    refundData.value = {
      goodsName: sku.skuName,
      goodsCount: sku.count,
      skuID: sku.skuID,
      storeSubID: sku.storeSubID,
    }
    refundRef.value.open()
  }
}
function confirm(data: AnyObject) {
  uni.jxConfirm({
    title: '提示',
    content: '是否对该订单创建售后单\r\n进行【退款】处理',
    success: () => {
      if(props.data.buyerComment === '支付方式：扫码枪'){
        refundOnlineOrder(data,data.goodsCount)
      }else{
        let arr = [
          {
            skuID: data.skuID,
            count: data.goodsCount,
            storeSubID: data.storeSubID,
          },
        ]
        PartRefundOrder(arr)
      }
    },
  })
}

/**
 * 扫码枪扫码退款    
 */
 async function refundOnlineOrder(sku:AnyObject,count?:Number){
  try {
    let myMap = new Map()
    myMap.set(sku.skuID,sku.count)
    let  objNew = Object.fromEntries(myMap);
    await order.refund_online_order({vendorOrderID:props.data.vendorOrderID,skuIds:JSON.stringify(objNew),reason:'协商一致'})
    toast('创建成功', 1)
  } catch (error) {
    console.log('error',error)
    throw error
  }
 
}

/*************************************************
 * 退款处理
 */
async function PartRefundOrder(arr: Array<AnyObject>) {
  let data = {
    vendorOrderID: props.data.vendorOrderID,
    vendorID: props.data.vendorID,
    refundSkuList: JSON.stringify(arr),
    reason: '商家同意退款',
  }
  let res = await order.part_refund_order(data)
  if (res.code == 0) {
    toast('退款成功', 1)
    setTimeout(() => {
      uni.navigateBack()
    }, 800)
  } else {
    uni.jxAlert({
      title: '退款失败',
      content: res.desc || res.data,
    })
  }
}

/*************************************************
 * 跳转到商品详情
 */
function toGoodDetail(
  nameID: number,
  skuID: number,
  price: number,
  goodsName: string
) {
  uni.navigateTo({
    url: `/subPages/merchantChild/waitGoodsDetaile/waitGoodsDetaile?nameID=${nameID}&skuID=${skuID}&price=${price}&goodsName=${goodsName}`,
  })
  setStorage("reLoadOrderDetail", false )
}

/*************************************************
 * 打开多份商品退款
 */
const refundRef = ref<any>(null)
type refundDataType = {
  goodsName: string
  goodsCount: number
  skuID: number
  storeSubID: number
}
const refundData = ref<refundDataType>({
  goodsName: '',
  goodsCount: 1,
  skuID: 0,
  storeSubID: 0,
})
</script>
  
<style lang="scss" scoped>
.skuList {
  border-bottom: 1rpx solid #e7e7e7;
  background: rgb(255, 255, 255);
  padding-top: 10rpx;
  .sku-item-for-orderdetail + .sku-item-for-orderdetail {
    border-top: 1rpx solid rgba(204, 204, 204, 0.4);
  }

  // sku-item
  .sku-item-for-orderdetail {
    padding: 20rpx 0;
    display: flex;
    align-items: center;
    .img {
      position: relative;
      img {
        width: 180rpx;
        height: 180rpx;
        border: 1px solid #efefef;
      }
    }
    .right {
      margin-left: 20rpx;
      .sku-name {
        font-size: 30rpx;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);
        margin-right: 40rpx;
      }
      .sku-spec {
        margin-top: 10rpx;
        display: flex;
        width: 510rpx;
        justify-content: space-between;
        align-items: center;
        position: relative;
        .sku-price {
          font-size: 32rpx;
          font-weight: 400;
          color: rgba(246, 13, 88, 1);
        }
        .icon-modify {
          position: absolute;
          right: 0;
          top: -40rpx;
          width: 40rpx;
          height: 40rpx;
          background-repeat: no-repeat;
          background-size: 100%;
          background-position: center;
        }
        .earning-price {
          .price1 {
            font-size: 30rpx;
            text-decoration: line-through;
            color: #999;
          }
          .price2 {
            font-size: 32rpx;
            color: #999;
            margin-left: 10rpx;
            text {
              color: rgba(246, 13, 88, 1);
            }
          }
        }
      }

      .sku-info{
        display: flex;
        justify-content: space-between;
        margin-top: 10rpx;
      }

      .sku-location-code,.sku-stock{
        font-size: 28rpx;
        color:#999;
      }

      .totalPrice {
        font-size: 30rpx;
        font-weight: bold;
        color: $jx-primary;
      }
    }
    .sale-btns {
      margin-top: 20rpx;
    }
  }

  .pd20 {
    padding: 0 20rpx;
  }

  .img {
    position: relative;

    image {
      width: 180rpx;
      height: 180rpx;
      border: 1px solid #efefef;
      border-radius: 10rpx;
    }
  }

  .right {
    margin-left: 20rpx;
    .sku-name {
      font-size: 30rpx;
      font-weight: 500;
      color: rgba(51, 51, 51, 1);
      margin-right: 40rpx;
    }
    .sku-spec {
      margin-top: 10rpx;
      display: flex;
      width: 510rpx;
      justify-content: space-between;
      align-items: center;
      position: relative;
      .sku-price {
        font-size: 32rpx;
        font-weight: 400;
        color: rgba(246, 13, 88, 1);
      }
      .icon-modify {
        position: absolute;
        right: 0;
        top: -40rpx;
        width: 40rpx;
        height: 40rpx;
        background-repeat: no-repeat;
        background-size: 100%;
        background-position: center;
      }
      .earning-price {
        .price1 {
          font-size: 30rpx;
          text-decoration: line-through;
          color: #999;
        }
        .price2 {
          font-size: 32rpx;
          color: #999;
          margin-left: 10rpx;
          text {
            color: rgba(246, 13, 88, 1);
          }
        }
      }
    }

    .sku-info{
      display: flex;
      justify-content: space-between;
      margin-top: 10rpx;
    }

    .sku-location-code,.sku-stock{
      font-size: 28rpx;
      color:#999;
    }

    .totalPrice {
      font-size: 30rpx;
      font-weight: bold;
      color: $jx-primary;
    }
  }
}

.inLine {
  text-decoration: line-through;
  color: #000 !important;
}

text {
  display: inline-block;
}

.upc {
  border-top: 1px solid #efefef;
  padding: 10px 0;
  font-size: 15px;
  color: $jx-primary;
  font-weight: bold;
}

.sku-count {
  font-size: 28rpx;
  color: #999999;
  font-weight: 400;
}

.skuListWrap-1 {
  box-sizing: border-box;
  border-top: 1rpx solid #efefef;
  padding: 20rpx;
  font-size: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .left {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .img {
      width: 180rpx;
      height: 180rpx;
      border-radius: 10rpx;
    }

    .right {
      width: 370rpx;
      .name {
        font-weight: none;
        color: #000;
      }

      .price {
        color: #f84a82;
      }

      .price-count {
        font-weight: bold;
        color: $jx-primary;
      }
    }
  }

  .sku-count-1 {
    text-align: right;
    font-size: 28rpx;
    color: #999999;
    font-weight: 400;
    width: 140rpx;
    margin-top: 15rpx;
  }
}

.strong {
  font-weight: bold !important;
  color: $jx-primary !important;
}
</style>