<template>
  <ElDialog v-model="showDialog"
    :close-on-click-modal="false"
    destroy-on-close
    width="fit-content"
    @close="dialogCloseHandler">
    <ElForm :model="formData"
      :rules="rules"
      ref="driverForm"
      class="form-panel"
      label-width="auto">
       <ElFormItem label="Id" prop="id">
        <ElInput
          v-model="formData.id"
          :disabled="status === 'view'"
          placeholder="Id"/>
      </ElFormItem>
       <ElFormItem label="归属用户 Id；sys_user.id" prop="userId">
        <ElInput
          v-model="formData.userId"
          :disabled="status === 'view'"
          placeholder="归属用户 Id；sys_user.id"/>
      </ElFormItem>
       <ElFormItem label="归属客户 Id；cst_customer.id" prop="customerId">
        <ElInput
          v-model="formData.customerId"
          :disabled="status === 'view'"
          placeholder="归属客户 Id；cst_customer.id"/>
      </ElFormItem>
       <ElFormItem label="归属公司 Id；cst_org.id" prop="orgId">
        <ElInput
          v-model="formData.orgId"
          :disabled="status === 'view'"
          placeholder="归属公司 Id；cst_org.id"/>
      </ElFormItem>
       <ElFormItem label="驾驶证编号" prop="drivingLicenceNo">
        <ElInput
          v-model="formData.drivingLicenceNo"
          :disabled="status === 'view'"
          placeholder="驾驶证编号"/>
      </ElFormItem>
       <ElFormItem label="司机姓名" prop="driverName">
        <ElInput
          v-model="formData.driverName"
          :disabled="status === 'view'"
          placeholder="司机姓名"/>
      </ElFormItem>
       <ElFormItem label="手机号" prop="phone">
        <ElInput
          v-model="formData.phone"
          :disabled="status === 'view'"
          placeholder="手机号"/>
      </ElFormItem>
       <ElFormItem label="驾驶证图片" prop="drivingLicence">
        <ElInput
          v-model="formData.drivingLicence"
          :disabled="status === 'view'"
          placeholder="驾驶证图片"/>
      </ElFormItem>
       <ElFormItem label="驾驶证有效期" prop="licenceStartTime">
        <ElInput
          v-model="formData.licenceStartTime"
          :disabled="status === 'view'"
          placeholder="驾驶证有效期"/>
      </ElFormItem>
       <ElFormItem label="驾驶证有效期" prop="licenceEndTime">
        <ElInput
          v-model="formData.licenceEndTime"
          :disabled="status === 'view'"
          placeholder="驾驶证有效期"/>
      </ElFormItem>
       <ElFormItem label="忙碌中" prop="busy">
        <ElInput
          v-model="formData.busy"
          :disabled="status === 'view'"
          placeholder="忙碌中"/>
      </ElFormItem>
       <ElFormItem label="创建人 Id；sys_user.id" prop="creatorId">
        <ElInput
          v-model="formData.creatorId"
          :disabled="status === 'view'"
          placeholder="创建人 Id；sys_user.id"/>
      </ElFormItem>
       <ElFormItem label="修改人 Id； sys_user.id" prop="modifierId">
        <ElInput
          v-model="formData.modifierId"
          :disabled="status === 'view'"
          placeholder="修改人 Id； sys_user.id"/>
      </ElFormItem>
       <ElFormItem label="创建时间" prop="createTime">
        <ElInput
          v-model="formData.createTime"
          :disabled="status === 'view'"
          placeholder="创建时间"/>
      </ElFormItem>
       <ElFormItem label="修改时间" prop="modifyTime">
        <ElInput
          v-model="formData.modifyTime"
          :disabled="status === 'view'"
          placeholder="修改时间"/>
      </ElFormItem>
       <ElFormItem label="是否删除； 0-->未删除、1-->已删除" prop="deleted">
        <ElInput
          v-model="formData.deleted"
          :disabled="status === 'view'"
          placeholder="是否删除； 0-->未删除、1-->已删除"/>
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton @click="showDialog = false">{{ status === 'view' ? '关闭' : '取消' }}</ElButton>
      <ElButton v-if="status !== 'view'" :loading="submiting" type="primary" @click="submitHandler">提交</ElButton>
    </template>
  </ElDialog>
</template>

<script lang="ts" setup>
import DriverApi from '@/pages/cst/driver/driver-api.ts'
import Strings from '@/common/utils/strings.ts'
import FormUtil from '@/common/utils/formUtil.ts'
import {
  ElMessage,
  type FormInstance,
  type FormRules,
} from 'element-plus'

const emits = defineEmits(['editSucc'])
const showDialog = ref(false)
const submiting = ref(false)
const status = ref<'add' | 'view' | 'modify'>('add')

const driverFormIns = useTemplateRef<FormInstance>('driverForm')

const formData = ref<DriverTypes.SearchDriverResult>({})
const rules = reactive<FormRules<DriverTypes.SearchDriverResult>>({
id: [{ required: true, message: '请填写Id', trigger: 'blur' }],
userId: [{ required: true, message: '请填写归属用户 Id；sys_user.id', trigger: 'blur' }],
customerId: [{ required: true, message: '请填写归属客户 Id；cst_customer.id', trigger: 'blur' }],
orgId: [{ required: true, message: '请填写归属公司 Id；cst_org.id', trigger: 'blur' }],
drivingLicenceNo: [{ required: true, message: '请填写驾驶证编号', trigger: 'blur' }],
driverName: [{ required: true, message: '请填写司机姓名', trigger: 'blur' }],
phone: [{ required: true, message: '请填写手机号', trigger: 'blur' }],
drivingLicence: [{ required: true, message: '请填写驾驶证图片', trigger: 'blur' }],
licenceStartTime: [{ required: true, message: '请填写驾驶证有效期', trigger: 'blur' }],
licenceEndTime: [{ 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: '请填写是否删除； 0-->未删除、1-->已删除', trigger: 'blur' }],
})

function dialogCloseHandler() {
  formData.value = {}
}

function submitHandler() {
  if (status.value === 'view') return
  submiting.value = true
  if (formData.value.id != null) {
  FormUtil.submit(driverFormIns, () => DriverApi.modify(formData.value))
      .then(() => {
        ElMessage.success('修改成功')
        emits('editSucc')
        showDialog.value = false
      })
      .finally(() => {
        submiting.value = false
      })
  } else {
  FormUtil.submit(driverFormIns, () => DriverApi.add(formData.value))
      .then(() => {
        ElMessage.success('添加成功')
        emits('editSucc')
        showDialog.value = false
      })
      .finally(() => {
        submiting.value = false
      })
  }
}

defineExpose({
  open(data: DriverTypes.SearchDriverResult = {}) {
    showDialog.value = true
    if (!Strings.isBlank(data.id)) {
      status.value = 'modify'
      DriverApi.detail(data.id!)
        .then(res => {
          formData.value = res.data
        })
    } else {
      status.value = 'add'
      formData.value = data
    }
  }
})
</script>

<style lang="stylus" scoped>
.form-panel {
  padding 20px
}
</style>
