<template>
  <AFormPanel
    ref="formPanel"
    :details-loader="detailsLoader"
    :do-submit="doSubmit"
    :rules="rules"
    :title="status === 'add' ? '新建车辆信息' : '修改车辆信息'"
  >
    <template #default="{formData}">
      <div class="form-items">
        <ElFormItem label="归属客户" prop="customerId">
          <ElInput
            v-model="formData.customerId"
            placeholder="归属客户"/>
        </ElFormItem>
        <ElFormItem label="归属企业" prop="orgId">
          <ElInput
            v-model="formData.orgId"
            placeholder="归属企业"/>
        </ElFormItem>
        <ElFormItem label="车牌" prop="licensePlate">
          <ElInput
            v-model="formData.licensePlate"
            placeholder="车牌"/>
        </ElFormItem>
        <ElFormItem label="行驶证图片" prop="truckLicense">
          <ElInput
            v-model="formData.truckLicense"
            placeholder="行驶证图片"/>
        </ElFormItem>
        <ElFormItem label="车架号" prop="vnCode">
          <ElInput
            v-model="formData.vnCode"
            placeholder="车架号"/>
        </ElFormItem>
        <ElFormItem label="合格证图片" prop="qualification">
          <ElInput
            v-model="formData.qualification"
            placeholder="合格证图片"/>
        </ElFormItem>
        <ElFormItem label="最大载重；单位：千克" prop="carryingCapacity">
          <ElInput
            v-model="formData.carryingCapacity"
            placeholder="最大载重；单位：千克"/>
        </ElFormItem>
        <ElFormItem label="皮重；单位：千克" prop="tareWeight">
          <ElInput
            v-model="formData.tareWeight"
            placeholder="皮重；单位：千克"/>
        </ElFormItem>
        <ElFormItem label="行驶证有效期" prop="licenseStartDate">
          <ElInput
            v-model="formData.licenseStartDate"
            placeholder="行驶证有效期"/>
        </ElFormItem>
        <ElFormItem label="行驶证有效期" prop="licenseEndDate">
          <ElInput
            v-model="formData.licenseEndDate"
            placeholder="行驶证有效期"/>
        </ElFormItem>
        <ElFormItem label="合格证有效期" prop="qualificationStartDate">
          <ElInput
            v-model="formData.qualificationStartDate"
            placeholder="合格证有效期"/>
        </ElFormItem>
        <ElFormItem label="合格证有效期" prop="qualificationEndDate">
          <ElInput
            v-model="formData.qualificationEndDate"
            placeholder="合格证有效期"/>
        </ElFormItem>
        <ElFormItem label="车辆类型" prop="truckCategory">
          <ElInput
            v-model="formData.truckCategory"
            placeholder="车辆类型"/>
        </ElFormItem>
        <ElFormItem label="车辆图片" prop="picture">
          <ElInput
            v-model="formData.picture"
            placeholder="车辆图片"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import TruckApi from '@/pages/cst/truck/truck-api.ts'
import Strings from '@/common/utils/strings.ts'
import { type FormRules } from 'element-plus'
import type { ComponentExposed } from 'vue-component-type-helpers'
import AFormPanel from '@/components/a-form-panel/AFormPanel.vue'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const rules = reactive<FormRules<TruckTypes.SearchTruckResult>>({
  id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
  customerId: [ {required: true, message: '请填写归属客户 Id；cst_customer.id', trigger: 'blur'} ],
  orgId: [ {required: true, message: '请填写归属企业', trigger: 'blur'} ],
  licensePlate: [ {required: true, message: '请填写车牌', trigger: 'blur'} ],
  truckLicense: [ {required: true, message: '请填写行驶证图片', trigger: 'blur'} ],
  vnCode: [ {required: true, message: '请填写车架号', trigger: 'blur'} ],
  qualification: [ {required: true, message: '请填写合格证图片', trigger: 'blur'} ],
  carryingCapacity: [ {required: true, message: '请填写最大载重；单位：千克', trigger: 'blur'} ],
  tareWeight: [ {required: true, message: '请填写皮重；单位：千克', trigger: 'blur'} ],
  licenseStartDate: [ {required: true, message: '请填写行驶证有效期', trigger: 'blur'} ],
  licenseEndDate: [ {required: true, message: '请填写行驶证有效期', trigger: 'blur'} ],
  qualificationStartDate: [ {required: true, message: '请填写合格证有效期', trigger: 'blur'} ],
  qualificationEndDate: [ {required: true, message: '请填写合格证有效期', trigger: 'blur'} ],
  truckCategory: [ {required: true, message: '请填写车辆类型', trigger: 'blur'} ],
  picture: [ {required: true, message: '请填写车辆图片', trigger: 'blur'} ],
  busy: [ {required: true, message: '请填写忙碌中', trigger: 'blur'} ],
  creatorId: [ {required: true, message: '请填写创建人 Id； sys_user.id', trigger: 'blur'} ],
  modifierId: [ {required: true, message: '请填写修改人 Id； sys_user.id', trigger: 'blur'} ],
  createTime: [ {required: true, message: '请填写创建时间', trigger: 'blur'} ],
  modifyTime: [ {required: true, message: '请填写修改时间', trigger: 'blur'} ],
  deleted: [ {required: true, message: '请填写是否删除', trigger: 'blur'} ],
})


const formPanelIns = useTemplateRef<ComponentExposed<typeof AFormPanel>>('formPanel')
const status = ref<'add' | 'modify'>('add')

function detailsLoader(id?: string) {
  if (Strings.isBlank(id)) {
    status.value = 'add'
    return Promise.resolve()
  } else {
    status.value = 'modify'
    return TruckApi
      .detail(id!)
      .then(res => res.data)
  }
}

function doSubmit(data: TruckTypes.SearchTruckResult) {
  if (status.value === 'add') {
    return TruckApi.add(data)
      .then(props.research)
  } else {
    return TruckApi.modify(data)
      .then(props.research)
  }
}

defineExpose({
  open(data?: TruckTypes.SearchTruckResult) {
    formPanelIns.value?.open(data?.id)
  },
})
</script>

<style lang="stylus" scoped>
.form-items {
  grid-template-columns: 1fr 1fr;

  :deep(.el-form-item) {
  }
}
</style>
