<template>
  <view class="big-modal">
    <view class="modal-bg">
      <!-- 图片 -->
      <view v-if="skuGoods" class="have-goods">
        该商品已存在 - ¥{{ skuGoods.unitPrice / 100 }}/{{ skuGoods.unit }}
      </view>
      <view class="modal-content">
        <image class="img" :src="upcGood.imgList[0]" alt="" />
        <view class="original-name">{{ upcGood.originalName }}</view>
        <view class="upc-code">商品码: {{ upcGood.upcCode }}</view>
        <view>
          <input
            class="input"
            v-model="price"
            placeholder="请输入价格"
            type="digit"
          />
        </view>
      </view>
      <view class="big-modal-btn-group">
        <view @click="cancel" class="btn btn-cancel">取消</view>
        <view @click="correct" class="btn btn-correct" v-show="upcGood.isJx"
          >纠正</view
        >
        <view @click="confirm" class="btn btn-create">创建</view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup >
import shopping from '@/api/https/shopping'
import merchant from '@/api/https/merchant'
import { getStorage } from '@/utils/storage'
import toast from '@/utils/toast'
import { onMounted, ref } from 'vue'

interface PropsType {
  scanData: AnyObject
}
const props = defineProps<PropsType>()

const upcGood = ref<AnyObject>({})
upcGood.value = props.scanData
const price = ref<any>(null)
const skuGoods = ref<any>(null)

onMounted(async () => {
  let data = {
    storeIDs: JSON.stringify([getStorage('storeID')]),
    isFocus: true,
    keyword: upcGood.value.upcCode,
    isGetOpRequest: true,
    isBySku: false,
    isAct: false,
    isHighPrice: false,
  }
  let res = await merchant.get_stores_skus(data)
  if (res.data.totalCount == 0) return
  skuGoods.value = res.data.skuNames[0]
})

const emit = defineEmits<{
  (e: 'cancel'): void
  (e: 'confirm', data: AnyObject): void
}>()

// 取消创建
function cancel() {
  emit('cancel')
}

// 确认创建
function confirm() {
  if (!price.value) {
    return toast('请输入商品价格')
  }
  let data = {
    payload: JSON.stringify(upcGood.value),
    price: Math.round(price.value * 100),
  }
  emit('confirm', data)
}

// 纠正重新查询京东商品库
async function correct() {
  let obj: AnyObject = {}
  const res = await shopping.get_jd_upc_code_by_name(upcGood.value.upcCode)
  if (res.data.length > 0) {
    obj = res.data[0]
    obj.isJx = false
    upcGood.value = obj
  } else {
    uni.jxAlert({
      title: '提示',
      content: '未查询到商品，请联系运营创建',
    })
  }
}
</script>

<style lang="scss" scoped>
@import './searchGoods.scss';
</style>