<template>
  <!-- 添加或修改项目信息对话框 -->
  <el-dialog title="载具信息表单" v-model="visible" width="600px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="vehicleRef" class="master-container" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="载具编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入载具编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="是否废弃" prop="remark">
          <el-input v-model="form.isAbandoned" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="废弃原因" prop="remark">
          <el-input v-model="form.abandonmentReason" placeholder="请输入废弃原因" required />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" @click="handleSave">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { getCarrier, saveCarrier } from '@/api/business/carrier'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['handleSaveSuccess'])

const loading = ref(false)
const visible = ref(false)
const data = reactive({
  form: {},
  rules: {
    code: [{ required: true, message: '载具编号不能为空', trigger: 'blur' }],
    sort: [{ required: true, message: '载具排序不能为空', trigger: 'blur' }]
  }
})
const { form, rules } = toRefs(data)
/***********************  方法区  ****************************/
/** 打开抽屉 */
function open(id) {
  reset()
  visible.value = true
  if (id) {
    getCarrier(id).then((response) => {
      form.value = response.data
    })
  }
}

/** 取消按钮 */
function handleCancel() {
  visible.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    deptId: null,
    companyId: null,
    parentId: null,
    code: null,
    name: null,
    remark: null,
    sort: null,
    createTime: null,
    creatorId: null,
    updateTime: null,
    updaterId: null,
    deleted: null,
    version: null
  }
  proxy.resetForm('vehicleRef')
}

/** 提交按钮 */
function handleSave() {
  proxy.$refs['vehicleRef'].validate((valid) => {
    if (valid) {
      saveCarrier(form.value).then((res) => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('操作成功')
          emit('handleSaveSuccess')
        }
        visible.value = false
      })
    }
  })
}

/** 暴露给父组件的方法 */
defineExpose({
  open
})
</script>
