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

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { createApp, getAppById, updateApp } from '@/api/apps/appApi'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  appTitle: [{ required: true, message: '请输入appTitle', trigger: 'blur' }],
  appCatTitle: [{ required: true, message: '请输入appCatTitle', trigger: 'blur' }],
  appUrl: [{ required: true, message: '请输入appUrl', trigger: 'blur' }],
  appCode: [{ required: true, message: '请输入app编码', trigger: 'blur' }],
  appDesc: [{ required: true, message: '请输入描述', trigger: 'blur' }],
})

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    appId: 0,
    appCode: '',
    appTitle: '',
    appCatTitle: '',
    appCatId: 0,
    appParam: '',
    appUrl: '',
    appDesc: '',
    appLogo: '',
  },
  actionType: 1,
})

onMounted(() => {})

const getData = async (appId: number) => {
  state.loading = true
  const { data } = await getAppById(appId)
  state.loading = false
  state.formData = data
}

function onSubmit() {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        await createApp(state.formData)
        state.dialogVisible = false
        emit('success')
      }
      else {
        await updateApp(state.formData)
        state.dialogVisible = false
        emit('success')
      }
      ElMessage.success({
        message: '保存成功',
        center: true,
      })
    }
  })
}

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    appId: 0,
    appCode: '',
    appTitle: '',
    appCatTitle: '',
    appCatId: 0,
    appUrl: '',
    appParam: '',
    appDesc: '',
    appLogo: '',
  }
}

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

})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
    <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
      <el-form-item label="标题" prop="appTitle">
        <el-input v-model="state.formData.appTitle" />
      </el-form-item>

      <el-form-item label="分类" prop="appCatTitle">
        <el-input v-model="state.formData.appCatTitle" readonly />
      </el-form-item>

      <el-form-item label="编码" prop="appCode">
        <el-input v-model="state.formData.appCode" />
      </el-form-item>

      <el-form-item label="地址" prop="appUrl">
        <el-input v-model="state.formData.appUrl" />
      </el-form-item>

      <el-form-item label="LOGO">
        <el-image
          style="width: 50px; height: 50px;border-radius:50%;"
          :src="state.formData.appLogo"
          :preview-src-list="[state.formData.appLogo]"
        />
      </el-form-item>
      <el-form-item label="参数" prop="appParam">
        <el-input v-model="state.formData.appParam" />
      </el-form-item>

      <el-form-item label="描述">
        <el-input v-model="state.formData.appDesc" type="textarea" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button size="large" @click="onCancel">
        取消
      </el-button>
      <el-button type="primary" size="large" @click="onSubmit">
        保存
      </el-button>
    </template>
  </el-drawer>
</template>
