<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import ClassicEditor from "ckeditor5-custom-build/build/ckeditor.js";


const route = useRoute();
const router = useRouter();

import { getConfig, updateConfig } from "@/api/config";

const formType = ref("insert");

const loading = ref(false);
const formRef = ref(null);
const form = ref({});

// 初始化編輯器
const editor = ClassicEditor;
const editorConfig = ref({
  simpleUpload: {
    // The URL that the images are uploaded to.
    uploadUrl: '/adminapi/article/uploadImage',
    // // Headers sent along with the XMLHttpRequest to the upload server.
    headers: {
      'X-CSRF-TOKEN': 'CSFR-Token',
      'Authorization' : `${sessionStorage.getItem('token')}`
    }
  },
  image: {
    resize: true,
    toolbar: [
      "imageTextAlternative",
      "|",
      "imageStyle:alignLeft",
      "imageStyle:alignRight",
      "|",
      "imageStyle:alignBlockLeft",
      "imageStyle:block",
      "imageStyle:alignBlockRight",
    ],
    styles: [
      'full',
      'alignLeft',
      'alignRight',
    ],
  },
});

onMounted(async () => {
    let res = await getConfig();
    if (res.code === 200) {
      form.value = res.data;
      formType.value = "update";
    }else{
      ElMessage({
        message: "取得資料失敗.",
        type: 'error',
      });
    }
});

const rules = reactive({
})


const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      let res;
      let message;

      res = await updateConfig(form.value);

      message = "修改成功.";

      if (res.code === 200) {
        ElMessage({
          message: message,
          type: 'success',
        });
      }
    } else {
      ElMessage({
        message: "操作失敗.",
        type: 'error',
      });
      return false
    }
  })
};
</script>

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <div class="breadcrumb-section">
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> 站台設定 </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
      </div>
    </div>
    <!-- Breadcrumb: End -->

    <el-card>
    <el-form 
      ref="formRef" 
      :model="form" 
      status-icon 
      :rules="rules" 
      label-width="120px">
      <el-form-item label="公司名稱" prop="company_name">
          <el-input v-model="form.company_name" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="Email" prop="email">
          <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item label="電話" prop="tel">
          <el-input v-model="form.tel" />
      </el-form-item>
      <el-form-item label="傳真" prop="fax">
          <el-input v-model="form.fax" />
      </el-form-item>
      <el-form-item label="產品與服務" prop="products">
          <el-input type="textarea" :rows="3" v-model="form.products" />
      </el-form-item>
      <el-form-item label="首頁下方區塊" prop="development">
          <ckeditor :editor="editor" v-model="form.development" :config="editorConfig"></ckeditor>
      </el-form-item>
    </el-form>
    <div class="demo-drawer__footer" style="text-align: right;">
      <el-button type="primary" :loading="loading" @click="submitForm(formRef)">{{
        loading ? '提交中 ...' : '修改'
      }}</el-button>
    </div>
  </el-card>
  </div>
</template>

<style lang="less" scoped>

:deep(.ck-editor__editable_inline) {
  width: 70vw;
  height: 111px;
}
</style>