<template>
  <ay-container>
    <ayb-station type="2" :stationInfo="data.stationInfo" />
    <div class="section">本次加气驾驶员</div>
    <div class="chunk">
      <div class="center justify-start">
        <image class="personal" :src="store.user.userInfo.head_url" mode="scaleToFill" />
        <div>
          <div class="p-fz">{{ store.user.userInfo.user_name }}</div>
          <div class="mt-spacd2 s-fz">{{ store.user.userInfo.mobileDes }}</div>
        </div>
      </div>
    </div>
    <div class="station">
      <div class="section">本次加气量（公斤）</div>
      <uni-easyinput
        v-model="data.gasQty"
        type="digit"
        placeholder="请输入"
        focus
        @input="methods.input"
        @confirm="methods.confirm"
      />
    </div>
    <div class="btns">
      <button
        type="primary"
        @click="methods.confirm"
        :loading="store.webapi.pay.add.ing"
        :disabled="store.webapi.pay.add.ios"
      >
        确定
      </button>
    </div>
  </ay-container>
</template>

<script lang="ts" setup>
const data = ay.initData({
  stationInfo: {},
  gasQty: '',
})
const gasQty = computed(() => Number(data.gasQty))
const user = computed(() => store.user.userInfo)
const pageConfig = computed(() => store.curPage.pageConfig)
const methods = {
  input() {
    nextTick(() => {
      data.gasQty = data.gasQty.replace(/^\D*(\d*\.{0,1}\d{0,2}).*/, '$1')
    })
  },
  confirm() {
    if (gasQty.value <= 0) {
      func.native.showToast('加气量需大于零')
      return false
    }
    if (gasQty.value > store.common.data.preferWeight) {
      func.native.showToast('加气量不能超过' + store.common.data.preferWeight)
      return
    }

    webapi.pay
      .add_v2({
        cashierId: pageConfig.value.params.cashierId,
        driverId: user.value.user_id,
        gasQty: gasQty.value,
        gasstationId: pageConfig.value.params.gasstationId,
        createRole: 'driver', // 1 平台管理员 2 平台运营人员 filler 加气站企业管理者 cashier 收银员 carrier 物流公司管理者 driver 司机
        creater: user.value.user_id,
        createrName: user.value.user_name,
      })
      .then((res) => {
        ay.goPage(config.pages.order_prePay, {
          goPageType: enums.GoPageType.redirectTo,
          params: { orderId: res.orderId },
        })
      })
  },
}

ay.entrance(async (args) => {
  const location = await func.native.getLocation()
  data.stationInfo = await webapi.strategy.find_for_mini({
    gasstationId: pageConfig.value.params.gasstationId,
    longitude: location.longitude.toString(),
    latitude: location.latitude.toString(),
  })
})
onUnload(() => {
  store.webapi.pay.add.set()
})
</script>

<style lang="scss" scoped>
.personal {
  width: 100rpx;
  height: 100rpx;
  margin-right: $p-spac;
  border-radius: $p-spac;
}
</style>
