<template>
  <el-dialog title="载具类别表单" v-model="visible" width="600px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="carrierCategoryRef" class="master-container" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别名称" prop="name">
          <el-input v-model="form.name" placeholder="类别名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请填写备注" />
        </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 { getCategory, saveCategory } from '@/api/business/carrier'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['handleSaveSuccess'])
/** 父组件传参 */
const props = defineProps({
  getList: {
    type: Function,
    default: () => {}
  }
})
const { getList } = toRefs(props)
/** 字典数组区 */
/** 表单抽屉 页变量 */
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) {
    getCategory(id).then((response) => {
      form.value = response.data
    })
  }
}

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

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    parentId: 0,
    code: '',
    name: '',
    remark: '',
    sort: 1
  }
  proxy.resetForm('carrierCategoryRef')
}

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

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