<route lang="yaml">
meta:
  enabled: false
</route>

<script lang="ts" setup name="ModelObject">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { createObject, getObjectById, updateObject } from '@/api/model/modelObjectApi'
const emit = defineEmits(['success'])

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    modelId: 0,
    objectId: 0,
    objectCode: '',
    objectTitle: '',
    comment: '',
  },
  actionType: 1,
})

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  objectTitle: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  objectCode: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  comment: [
    { required: false, message: '请输入描述', trigger: 'blur' },
  ],
})

// 查询数据
const getData = async (modelId: number, objectId: number) => {
  state.loading = true
  const { data } = await getObjectById(modelId, objectId)
  state.loading = false
  state.formData = data
}

onMounted(() => {
  nextTick(() => {
  })
})

// 保存
function onSubmit() {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        await createObject(state.formData)
        state.dialogVisible = false
        ElMessage.success('新增成功')
        emit('success')
      }
      else {
        await updateObject(state.formData)
        state.dialogVisible = false
        ElMessage.success('修改成功')
        emit('success')
      }
    }
  })
}

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    modelId: 0,
    objectId: 0,
    objectCode: '',
    objectTitle: '',
    comment: '',
  }
}

defineExpose({
  showAddModel(modelId: number) {
    resetFormData()
    state.title = '新增对象'
    state.actionType = 1
    state.dialogVisible = true
    state.formData.modelId = modelId
  },
  showEditModel(modelId: number, objectId: number) {
    resetFormData()
    state.title = '编辑对象'
    state.actionType = 2
    state.dialogVisible = true
    getData(modelId, objectId)
  },

})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%" direction="rtl">
    <el-scrollbar>
      <div class="form-content">
        <el-form ref="formRef" :model="state.formData" :rules="formRules" label-position="top">
          <el-form-item label="对象名称" prop="objectTitle">
            <el-input v-model="state.formData.objectTitle" placeholder="请输入标题" />
          </el-form-item>
          <el-form-item label="对象代码" prop="objectCode">
            <el-input v-model="state.formData.objectCode" placeholder="请输入代码" />
          </el-form-item>
          <el-form-item label="简要说明" prop="comment">
            <el-input v-model="state.formData.comment" type="textarea" rows="10" placeholder="请输入标题" />
          </el-form-item>
        </el-form>
      </div>
    </el-scrollbar>

    <template #footer>
      <div style="flex: auto">
        <el-button type="primary" size="large" @click="onSubmit">
          提交
        </el-button>
        <el-button size="large" @click="onCancel">
          取消
        </el-button>
      </div>
    </template>
  </el-drawer>
</template>

<style lang="scss" scoped>
    .form-right-side {
      padding: var(--container-padding);
      border-left: 1px dashed var(--el-disabled-border-color);
      background-color: var(--g-app-bg);
      transition: background-color 0.3s;

      :deep(.el-table) {
        height: 100%;
        margin-top: 15px;

        .el-table__row {
          .index {
            display: inline-block;
          }

          .delete {
            display: none;
          }

          &:hover {
            .index {
              display: none;
            }

            .delete {
              display: inline-block;
            }
          }

          .el-tag.sortable,
          .el-tag.sortable .el-icon {
            cursor: s-resize;
          }
        }
      }
    }
</style>
