<template>
  <div class="prizeDrawBox">
    <div class="d-flex flex-column align-center">
      <div class="numberBox mb-5">房券抽奖</div>
      <gridPage v-if="props.type === '1'" :lotteryId="props.lotteryId" :disabled="disabled" @end="endCallback"></gridPage>
      <slotMachinePage v-if="props.type === '2'" :lotteryId="props.lotteryId" height="120" :disabled="disabled" @end="endCallback"></slotMachinePage>

      <v-card min-height="300" width="600" class="mt-5 pa-5">
        <div v-if="showDialog">
          <p v-for="(k, i) in prizeData" :key="i">{{ k.prize.prompt }}</p>
          <p>凭此房券在规定有效期内可享受免费住宿一晚。</p>
          <p class="mb-5">请提供收货地址，以便安排房券派送。</p>

          <!-- 收货地址 -->
          <div>
            <v-radio-group v-model="addressSelect" color="primary">
              <v-radio v-for="val in address" :key="val.id" :label="val.id === 9999 ? val.label : (val.name + '，' + val.mobile + '，' + val.areaName + val.detailAddress)" :value="val.id"></v-radio>
            </v-radio-group>
            <v-form v-if="addressSelect === 9999" ref="addressFormRef">
              <v-row>
                <v-col :cols="6">
                  <v-text-field v-model="newAddress.name" label="收货人名称 *" color="primary" density="compact" variant="outlined"></v-text-field>
                </v-col>
                <v-col :cols="6">
                  <v-text-field v-model="newAddress.mobile" :rules="phoneRules" label="收货人手机号码 *" color="primary" density="compact" variant="outlined"></v-text-field>
                </v-col>
              </v-row>
              <div class="d-flex" style="width: 100%;">
                <div class="mt-2" style="color: #777; width: 100px;">省市区 *</div>
                <el-cascader
                  ref="cascaderAddr"
                  v-model="newAddress.areaId"
                  size="large"
                  clearable
                  class="mb-5"
                  placeholder="省市区 *"
                  style="flex: 1;"
                  :props="{ value: 'id', label: 'name', emitPath: false }"
                  :options="areaTreeData"
                  @change="handleChangeArea(item)"
                ></el-cascader>
              </div>
              <v-text-field v-model="newAddress.detailAddress" label="详情地址 *" color="primary" density="compact" variant="outlined"></v-text-field>
            </v-form>
            <div class="text-center mt-3">
              <v-btn color="primary" class="elevation-5" width="180" @click.stop="handleSubmit">确 认</v-btn>
            </div>
          </div>
        </div>
      </v-card>
    </div>
  </div>
</template>

<script setup>
defineOptions({ name: 'prizeDraw'})
import gridPage from './prizeDraw/grid.vue'
import slotMachinePage from './prizeDraw/slotMachine.vue'
import { ref } from 'vue'
import { getLuckLotteryRecordByOrderId } from '@/api/mall/prize'
import { getMallUserAddressList } from '@/api/mall/address'
import Snackbar from '@/plugins/snackbar'
import { luckyLotteryRecordReceive } from '@/api/mall/prize'
import { useRouter } from 'vue-router'
import { getDict } from '@/hooks/web/useDictionaries'

const emit = defineEmits(['success'])
const props = defineProps({
  lotteryId: [Number, String],
  orderId: [Number, String],
  type: {
    type: String,
    default: '1'
  }
})

const router = useRouter()
const newAddress = ref({
  name: '',
  areaId: '',
  areaName: '',
  mobile: '',
  detailAddress: ''
})
const addressSelect = ref()
const disabled = ref(false)

const addressFormRef = ref()
const cascaderAddr = ref()
const areaTreeData = ref([])
getDict('areaTreeData', null, 'areaTreeData').then(({ data }) => {
  data = data?.length && data || []
  areaTreeData.value = data
})
const phoneRules = ref([
  value => {
    if (value) return true
    return t('login.mobileNumberPlaceholder')
  },
  value => {
    if (value?.length <= 11 && /^1[3456789]\d{9}$/.test(value)) return true
    return t('login.correctPhoneNumber')
  }
])

// 地区选择
const handleChangeArea = () => {
  const node = cascaderAddr.value.getCheckedNodes() ? cascaderAddr.value.getCheckedNodes()[0] : null
  if (!node) return
  newAddress.value.areaName = node.pathLabels.join(' ')
}

// 获取中奖记录、收货地址
const address = ref([])
const prizeData = ref({})
const getRecord = async () => {
  const data = await getLuckLotteryRecordByOrderId(props.orderId)
  prizeData.value = data || []
  if (!data || !data.length) disabled.value = true

  const addressData = await getMallUserAddressList()
  address.value = [...addressData, { id: 9999, label: '使用新地址' }] || []
  if (addressData && addressData.length) addressSelect.value = addressData[0].id
}
if (props.orderId) getRecord()

const showDialog = ref(false)
const endCallback = () => {
  if (!prizeData.value.length) return Snackbar.warning('您已经抽过奖了哦')
  showDialog.value = true
  disabled.value = true
}

function checkValue(obj) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key) && !obj[key]) {
      return false
    }
  }
  return true
}

// 领取
const handleSubmit = async () => {
  let query = {}
  if (addressSelect.value === 9999) {
    if (!checkValue(newAddress.value)) return Snackbar.warning('请完善收货信息')
    query = newAddress.value
  } else query = address.value.find(item => item.id === addressSelect.value)
  await luckyLotteryRecordReceive({ id: prizeData.value[0].record.id, receiveInfo: JSON.stringify(query) })
  Snackbar.success('领取成功，待商家发货')
  router.push('/recruit/personal/personalCenter/tradeOrder?key=1')
}
</script>

<style scoped lang="scss">
.prizeDrawBox {
  padding: 20px 40px;
  background-color: var(--default-bgc);
}
.prizeDraw {
  margin: 0 auto; 
}
.numberBox {
  font-size: 20px;
  font-weight: bold;
  padding: 2px 38px;
  border-bottom: 3px solid var(--v-primary-base);
}
.colorBase {
  color: var(--v-primary-base);
  margin: 0 6px;
  font-size: 22px;
}
</style>
