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

<template>
  <view class="container">
    <wd-form ref="form" :model="model" class="px-2">
      <wd-cell-group border custom-class="group" title="发病">
        <wd-input
          label="牛号"
          label-width="100px"
          prop="cowId"
          clearable
          v-model="model.cowId"
          placeholder="请输入牛号"
          :rules="[{ required: true, message: '请填写牛号' }]"
        />
        <wd-cell title="是否诊断" title-width="100px" prop="isDiagnosed" right required>
          <wd-switch v-model="model.isDiagnosed" />
        </wd-cell>
        <view v-if="model.isDiagnosed == 1">
          <wd-cell title="诊断" title-width="100px" prop="diagnosticState" right required>
            <view>
              <wd-radio-group v-model="model.diagnosticState" shape="dot" inline @change="change">
                <wd-radio :value="item.value" v-for="item in baseList" :key="item.value">
                  {{ item.label }}
                </wd-radio>
              </wd-radio-group>
            </view>
          </wd-cell>
          <wd-cell title="疾病" title-width="100px" prop="value1" right required>
            <text v-for="child in diseaseChoseList">{{ child.name }}</text>
            <wd-icon name="arrow-right" size="18px" color="#BFBFBF" @click="handleRoute"></wd-icon>
          </wd-cell>
          <wd-cell title="是否治疗" title-width="100px" prop="value1" right required>
            <view>
              <wd-radio-group v-model="model.isTreatment" shape="button" inline @change="change">
                <wd-radio :value="1">是</wd-radio>
                <wd-radio :value="2">否</wd-radio>
              </wd-radio-group>
            </view>
          </wd-cell>
          <wd-cell
            title="治疗方案"
            title-width="100px"
            prop="value1"
            right
            required
            v-if="model.isTreatment == 1"
          >
            <text></text>
            <wd-icon
              name="arrow-right"
              size="18px"
              color="#BFBFBF"
              @click="handleCaseRoute"
            ></wd-icon>
          </wd-cell>
        </view>
        <wd-calendar
          label="日期"
          prop="date"
          label-width="100px"
          type="datetime"
          :hide-second="true"
          v-model="model.date"
          :min-date="minDate"
          :max-date="maxDate"
          :disabled-date="disableFutureDates"
          @confirm="handleConfirm"
        />

        <wd-textarea
          label="备注信息"
          label-width="100px"
          v-model="model.remarks"
          :maxlength="200"
          clearable
          show-word-limit
          placeholder="请输入备注"
          auto-height
        />
      </wd-cell-group>
      <view class="footer mt-5 pb-20 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'
import { getConfigSys, postModel } from '@/service/index/common'

defineOptions({
  name: 'neckRing',
})

// 请求诊断类型
let baseList = reactive<[]>
async function getConfigBase() {
  const { data } = await getConfigSys({
    option_name: 'diagnosticState',
  })
  baseList = data
  console.log(data, '事件列表')
}
function handleConfirm({ value }) {
  console.log(new Date(value).toString(), '8888')
}

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

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

const model = reactive<{
  isDiagnosed: number // 是否诊断
  cowId: number // 牛号
  date: number
  operatorAt: string | number // 日期
  remarks: string // 备注
  operatorId: number // 备用字段
  operatorName: string
  diagnosticState: number // 诊断状态
  isTreatment: number // 1是 治疗 2 否
}>({
  date: Date.now(),
  isDiagnosed: 0,
  cowId: 4,
  operatorAt: '',
  remarks: 'test',
  operatorId: 1,
  operatorName: '郝闪闪',
  diagnosticState: 1,
  isTreatment: 2,
})

// 疾病页面
function handleRoute() {
  uni.navigateTo({ url: '/pages/disease/disease' })
}

// 方案页面
function handleCaseRoute() {
  uni.navigateTo({ url: '/pages/treatMent/treatMent' })
}
const form = ref()

function handleSubmit() {
  form.value
    .validate()
    .then(({ valid, errors }) => {
      if (valid) {
        console.log(model, '表单数据')
        model.operatorAt = parseInt(model.date / 1000)
        model.isDiagnosed = model.isDiagnosed ? 1 : 0
        const formObj = {
          eventSource: 1,
          eventType: 9,
          cowId: model.cowId,
          timestamp: parseInt(new Date().getTime() / 1000),
          meta: JSON.stringify(model),
        }
        postModel(formObj).then((res) => {
          console.log(res, '122')
        })
      }
    })
    .catch((error) => {
      console.log(error, 'error')
    })
}
// 取消
function goBack() {
  uni.navigateBack({
    delta: 1,
  })
}

onMounted(() => {
  getConfigBase()
})
</script>

<style lang="scss" scoped>
.container {
  background: #fff;
}
</style>
