<template>
  <div style="padding: 40px;">
    <el-form ref="settingForm" :model="form" label-width="80px">
      <el-form-item label="后台访问地址" required prop="adminWebUri">
        <el-input v-model="form.adminWebUri"></el-input>
      </el-form-item>
      <el-form-item label="后台api" required prop="api">
        <el-input v-model="form.api" @change="apiChange"></el-input>
      </el-form-item>
      <el-form-item label="文件上传地址" required prop="uploadApi">
        <el-input v-model="form.uploadApi"></el-input>
      </el-form-item>
      <el-form-item label="通知开启">
        <el-switch v-model="form.isNotice"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saving">保存</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { Notification } from "element-ui"
import { ipcRenderer } from 'electron';
import { ref } from 'vue'
import { useRouter } from "@/hooks/use-router"

const settingForm = ref()
const form = ref({
  adminWebUri: '',
  api: '',
  uploadApi: '',
  isNotice: ''
})

form.value = JSON.parse(JSON.stringify(window.DEFAULTSETTING))
console.log(form.value)
const reset = () => {
  form.value = JSON.parse(JSON.stringify(window.DEFAULTSETTING))
}
const apiChange = e => {
  if (e) {
    form.value.uploadApi = e + 'api/admin/upload-file'
  }
}
const saving = () => {
  settingForm.value.validate(valid => {
    if (valid) {
      ipcRenderer.invoke("write-default-setting",JSON.stringify(form.value)).then(res => {
        Notification({
          title: "提示",
          message: "配置生效需重启应用！",
          duration: 3000
        })
      })
    }
  })
}
</script>

<style scoped lang="scss">
</style>
