<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
        <el-form-item label="栏舍id" prop="fence_house_id">
          <el-input v-model="formData.fence_house_id" clearable placeholder="请输入栏舍id" />
        </el-form-item>
        <el-form-item label="耳号" prop="sn">
          <el-input v-model="formData.sn" clearable placeholder="请输入耳号" />
        </el-form-item>
        <el-form-item label="品类" prop="animal_type">
          <el-select class="flex-1" v-model="formData.animal_type" clearable placeholder="请选择品类">
            <el-option
                v-for="(item, index) in dictData.animal_type"
                :key="index"
                :label="item.name"
                :value="parseInt(item.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="品种" prop="brand">
          <el-input v-model="formData.brand" clearable placeholder="请输入品种" />
        </el-form-item>
        <el-form-item label="生理阶段" prop="physi_stage">
          <el-input v-model="formData.physi_stage" clearable placeholder="请输入生理阶段" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="formData.gender" clearable placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="血统纯度" prop="blood_purity">
          <el-input v-model="formData.blood_purity" clearable placeholder="请输入血统纯度" />
        </el-form-item>
        <el-form-item label="来源" prop="animal_source">
          <el-input v-model="formData.animal_source" clearable placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="现估量" prop="current_estimation">
          <el-input v-model="formData.current_estimation" clearable placeholder="请输入现估量" />
        </el-form-item>
        <el-form-item label="代数" prop="algebra">
          <el-input v-model="formData.algebra" clearable placeholder="请输入代数" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-input v-model="formData.birth" clearable placeholder="请输入出生日期" />
        </el-form-item>
        <el-form-item label="入场日期" prop="entry_date">
          <el-input v-model="formData.entry_date" clearable placeholder="请输入入场日期" />
        </el-form-item>
        <el-form-item label="出生体重" prop="birth_estimation">
          <el-input v-model="formData.birth_estimation" clearable placeholder="请输入出生体重" />
        </el-form-item>
        <el-form-item label="健康状况" prop="health_condition">
          <el-input v-model="formData.health_condition" clearable placeholder="请输入健康状况" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <el-input v-model="formData.image" clearable placeholder="请输入图片" />
        </el-form-item>

      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="animalInfoEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiAnimalInfoAdd, apiAnimalInfoEdit, apiAnimalInfoDetail } from '@/api/animal_info'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑动物档案' : '新增动物档案'
})

// 表单数据
const formData = reactive({
  id: '',
  fence_house_id: '',
  sn: '',
  animal_type: '',
  brand: '',
  physi_stage: '',
  gender: '',
  blood_purity: '',
  animal_source: '',
  current_estimation: '',
  algebra: '',
  birth: '',
  entry_date: '',
  birth_estimation: '',
  health_condition: '',
  image: '',

})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }


}

const getDetail = async (row: Record<string, any>) => {
  const data = await apiAnimalInfoDetail({
    id: row.id
  })
  setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate()
  const data = { ...formData,  }
  mode.value == 'edit'
      ? await apiAnimalInfoEdit(data)
      : await apiAnimalInfoAdd(data)
  popupRef.value?.close()
  emit('success')
}

//打开弹窗
const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
  emit('close')
}



defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
