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

<template>
  <view class="container">
    <wd-form ref="form" :model="model" class="px-2">
      <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
          required
          v-model="model.earTag"
          placeholder="请输入牛号"
        />
        <wd-cell title="性别" title-width="100px" prop="value1" right required>
          <view style="text-align: left">
            <wd-radio-group v-model="model.type" shape="dot" inline @change="change">
              <wd-radio :value="1">公牛</wd-radio>
              <wd-radio :value="2">母女</wd-radio>
            </wd-radio-group>
          </view>
        </wd-cell>

        <wd-input
          label="代次"
          label-width="100px"
          prop="earTag"
          clearable
          required
          v-model="model.earTag"
          placeholder="请输入牛号"
        />
        <wd-input
          label="月龄"
          label-width="100px"
          prop="earTag"
          clearable
          required
          v-model="model.earTag"
          placeholder="请输入月龄"
        />
        <wd-picker
          :columns="columns"
          label="品种"
          v-model="model.value1"
          @confirm="handleConfirm"
          required
          placeholder="请选择品种"
        />

        <wd-cell title="进场方式" title-width="100px" prop="value1" right required>
          <view style="text-align: left">
            <wd-radio-group v-model="model.type" shape="button" @change="change">
              <wd-radio :value="1">购入</wd-radio>
              <wd-radio :value="2">产犊</wd-radio>
              <wd-radio :value="3">调入</wd-radio>
            </wd-radio-group>
          </view>
        </wd-cell>
        <wd-cell title="进场体重" title-width="100px" prop="value1" right required>
          <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="earTag"
          clearable
          required
          v-model="model.earTag"
          placeholder="请输入价格"
        />

        <!-- <wd-input label="操作人" label-width="100px" prop="value2" disabled v-model="model.user" /> -->
        <wd-picker
          :columns="columns"
          label="栏舍"
          v-model="model.value1"
          @confirm="handleConfirm"
          placeholder="请选择栏舍"
        />

        <wd-picker
          :columns="columns"
          label="牛群类型"
          v-model="model.value1"
          @confirm="handleConfirm"
          placeholder="请选择牛群类型"
        />
        <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-cell-group>
      <view class="footer mt-5 center">
        <wd-button type="info" size="medium" class="mr-4" @click="goBack">取消</wd-button>
        <wd-button type="primary" size="medium" @click="handleSubmit">提交</wd-button>
      </view>
    </wd-form>
  </view>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'

defineOptions({
  name: 'neckRing',
})

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

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

const disableFutureDates = (date: Date) => {
  return date > today
}

// 取消返回上一级
function goBack() {
  uni.navigateBack({
    delta: 1,
  })
}

// 品种选择
const columns = ref(['选项1', '选项2', '选项3', '选项4', '选项5', '选项6', '选项7'])

function handleConfirm({ value }) {
  value.value = value
}

// 图片上传
const action: string =
  'https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86eb04dc1e8fd64865cb5/upload'
const fileList = ref<any[]>([
  {
    url: 'https://img12.360buyimg.com//n0/jfs/t1/29118/6/4823/55969/5c35c16bE7c262192/c9fdecec4b419355.jpg',
  },
])
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')
    })
}

onLoad(() => {})
</script>

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

.wd-radio,
.wd-radio[data-v-5706ad24] {
  line-height: none;
}
</style>
