<route lang="json5" type="page">
{
  style: { navigationBarTitleText: '购牛录入' },
}
</route>

<template>
  <view class="container center">
    <wd-form ref="form" :model="model" class="px-1">
      <wd-cell-group custom-class="group" title="采购" border>
        <wd-input
          prop="earTag"
          clearable
          label="电子耳标号"
          label-width="100px"
          :disabled="model.earTag"
          suffix-icon="scan"
          @clicksuffixicon="scanQRCode"
          v-model="model.earTag"
          placeholder="请输入电子耳标号"
          required
        />

        <wd-input
          label="牛号"
          label-width="100px"
          prop="earTag"
          clearable
          v-model="model.earTag"
          placeholder="请输入牛号"
        />

        <wd-cell title="采购体重" title-width="100px" prop="value1" right>
          <view style="text-align: left">
            <wd-input-number
              v-model="model.weight"
              @change="handleChange"
              :precision="2"
              :step="0.1"
            />
            <text class="pl-2 mt-2">KG</text>
          </view>
        </wd-cell>

        <!-- <wd-input label="操作人" label-width="100px" prop="value2" disabled v-model="model.user" /> -->
        <wd-calendar
          label="购买时间"
          prop="date"
          label-width="100px"
          type="datetime"
          hide-second
          v-model="model.date"
          :min-date="minDate"
          :max-date="maxDate"
          :disabled-date="disableFutureDates"
        />
        <wd-cell title="图片上传" title-width="100px" prop="value1">
          <wd-upload
            :file-list="fileList"
            image-mode="aspectFill"
            :action="action"
            :limit="6"
            @change="handleUpload"
            accept="all"
            multiple
          ></wd-upload>
        </wd-cell>
        <wd-textarea
          label="备注信息"
          label-width="100px"
          v-model="model.remark"
          :maxlength="200"
          clearable
          show-word-limit
          placeholder="请输入备注"
          auto-height
        />
        <wd-input
          label="当前位置"
          label-width="100px"
          prop="location"
          disabled
          v-model="location"
        />
      </wd-cell-group>
      <view class="footer mx-4 mt-10">
        <wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
      </view>
    </wd-form>
  </view>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import QQMapWX from '../../utils/qqmap-wx-jssdk.min.js'
var qqmapsdk

defineOptions({
  name: 'neckRing',
})

// form信息
const model = reactive<{
  value1: string
  value2: string
  date: number
  earTag: string | null
  user: string
  weight: string
  remark: string
}>({
  value1: '',
  value2: '',
  date: Date.now(),
  earTag: '',
  user: 'hss',
  weight: '321.2',
  remark: '',
})

// 日期选择器
const today = new Date()
const minDate = ref(new Date(2000, 0, 1)) // 可选择的最早日期
const maxDate = ref(today)

const disableFutureDates = (date: Date) => {
  return date > today
}
const fileList = ref<any[]>([
  {
    url: 'https://img12.360buyimg.com//n0/jfs/t1/29118/6/4823/55969/5c35c16bE7c262192/c9fdecec4b419355.jpg',
  },
])

const action: string =
  'https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86eb04dc1e8fd64865cb5/upload'

function handleUpload({ fileList: files }) {
  fileList.value = files
}

async function scanQRCode() {
  try {
    const res = await uni.scanCode()
    model.earTag = res.result // 假设二维码中的数据是耳标编号
  } catch (e) {
    console.error('扫描失败:', e)
  }
}
const form = ref()

function handleChange({ value }) {
  // console.log(value)
}

function handleSubmit() {
  form.value
    .validate()
    .then(({ valid, errors }) => {
      if (valid) {
        // showSuccess({
        //   msg: '校验通过',
        // })
      }
    })
    .catch((error) => {
      console.log(error, 'error')
    })
}

// 获取用户地理位置
const location = ref<string>('')
function getUserAdreess() {
  uni.authorize({
    scope: 'scope.userFuzzyLocation',
    success(res) {
      if (res.errMsg == 'authorize:ok') {
        uni.getFuzzyLocation({
          type: 'wgs84',
          success(res) {
            console.log(res, '获取的经纬度')
            qqmapsdk = new QQMapWX({
              key: 'GNDBZ-VYLCG-PJYQA-I44CW-RUT7Q-YUFG5',
            })

            qqmapsdk.reverseGeocoder({
              location: {
                latitude: res.latitude,
                longitude: res.longitude,
              },

              success(addressRes) {
                console.log(addressRes, '======hss')
                location.value = addressRes?.result?.address || ''
              },
              fail(error) {
                console.log(error, 'error')
                uni.showToast({
                  title: '获取定位失败,请先开启手机定位！',
                  icon: 'none',
                  duration: 2000,
                })
              },
            })
          },
        })
      }
    },
    fail(err) {
      console.log(err, '获取定位失败')
    },
  })
}
onLoad(() => {
  getUserAdreess()
})
</script>

<style lang="scss" scoped>
.container {
  padding-bottom: 10rem;
  background: #fff;
}
</style>
