<template>
  <form @submit.prevent="handleSubmit" class="modern-form">
    <h2 class="form-title">新建项目通知</h2>

    <div class="form-grid">
      <!-- 左列 - 手机预览区域 -->
      <div class="form-column phone-preview-container">
        <div class="phone-frame">
          <div class="phone-header">
            <div class="phone-camera"></div>
            <div class="phone-speaker"></div>
          </div>

          <div class="phone-screen">
            <div class="phone-content">
              <!-- 通知预览 -->
              <div class="phone-notification">
                <h2 class="notification-title">{{ formData.notificationTitle }}</h2>
                <div class="notification-content" v-html="formData.notificationContent"></div>
              </div>
            </div>
          </div>

          <div class="phone-home-button"></div>
        </div>
      </div>

      <!-- 右列 - 编辑区域 -->
      <div class="form-column">
        <div class="input-group">
          <label class="input-label">
            <span class="label-text">通知标题</span>
            <input
                v-model="formData.notificationTitle"
                type="text"
                class="input-field"
                required
                placeholder="请输入通知标题"
            >
          </label>
        </div>

        <div class="input-group">
          <label class="input-label">
            <span class="label-text">关联项目ID</span>
            <input
                v-model.number="formData.projectId"
                type="number"
                min="1"
                class="input-field"
                required
                placeholder="请输入项目ID"
            >
          </label>
        </div>

        <div class="input-group">
          <label class="input-label">
            <span class="label-text">通知内容</span>
            <div class="rich-text-editor">
              <QuillEditor
                  v-model:content="formData.notificationContent"
                  contentType="html"
                  :options="editorOptions"
              />
            </div>
          </label>
        </div>
      </div>
    </div>

    <button type="submit" class="submit-button">
      <span>发布通知</span>
      <div class="button-sparkles"></div>
    </button>
  </form>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { QuillEditor } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import myAxios from "../../api/myAxios.ts";
import router from "../../router";

interface QuillOptions {
  theme: string;
  modules: {
    toolbar: Array<any[] | object>;
  };
  placeholder?: string;
}

const editorOptions: QuillOptions = {
  theme: 'snow',
  modules: {
    toolbar: [
      ['bold', 'italic', 'underline', 'strike'],
      ['blockquote', 'code-block'],
      [{ header: 1 }, { header: 2 }],
      [{ list: 'ordered' }, { list: 'bullet' }],
      ['link', 'image', 'video'],
      ['clean']
    ]
  },
  placeholder: '请输入通知内容...'
};

interface NotificationForm {
  notificationTitle: string;
  notificationContent: string;
  projectId: string;
}

const formData = reactive<NotificationForm>({
  notificationTitle: '',
  notificationContent: '',
  projectId: ""
});

const handleSubmit = async () => {
  try {
    const storedToken = localStorage.getItem('token');
    const res: any = await myAxios.post(
        '/projectNotification/add',
        JSON.stringify(formData),
        {
          headers: {
            'Content-Type': 'application/json',
            'Authorization': storedToken
          }
        }
    );

    if (res.code === 1) {
      alert('通知发布成功！');
      router.back();
      Object.assign(formData, {
        notificationTitle: '',
        notificationContent: '',
        projectId: 33
      });
    } else {
      alert(`发布失败：${res.message}`);
    }
  } catch (error) {
    console.error('请求失败：', error);
    alert('通知发布失败，请检查网络或数据格式');
  }
};
</script>

<style scoped>
/* 保留原有样式，调整部分选择器 */
.phone-notification {
  padding: 20px;
}

.notification-title {
  font-size: 1.2rem;
  color: #2c3e50;
  margin-bottom: 15px;
  font-weight: 600;
}

.notification-content {
  font-size: 0.95rem;
  color: #4a5568;
  line-height: 1.6;
}

.notification-content >>> img {
  max-width: 100%;
  border-radius: 8px;
  margin: 10px 0;
}
.modern-form {
  max-width: 90%;
  margin: 2rem auto;
  padding: 2.5rem;
  background: white;
  border-radius: 1.5rem;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  font-family: 'Segoe UI', system-ui, sans-serif;
}

.form-title {
  text-align: center;
  font-size: 1.8rem;
  color: #2c3e50;
  margin-bottom: 2rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
}

.input-group {
  margin-bottom: 1.5rem;
}

.input-label {
  display: block;
}

.label-text {
  display: block;
  margin-bottom: 0.6rem;
  color: #4a5568;
  font-size: 0.9rem;
  font-weight: 500;
}

.input-field{
  width: 100%;
  padding: 0.8rem 1.2rem;
  border: 2px solid #e2e8f0;
  border-radius: 0.75rem;
  transition: all 0.3s ease;
  font-size: 1rem;
  background: white;
}

.input-field:focus{
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
  outline: none;
}

.submit-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 1rem;
  margin-top: 1.5rem;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
  border: none;
  border-radius: 0.75rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.submit-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(99, 102, 241, 0.3);
}

.button-sparkles {
  position: absolute;
  width: 20px;
  height: 20px;
  background: rgba(255,255,255,0.4);
  border-radius: 50%;
  animation: sparkle 1.5s infinite;
}

@keyframes sparkle {
  0% { transform: scale(0) translate(0,0); }
  50% { transform: scale(1) translate(100px, -50px); }
  100% { transform: scale(0) translate(200px, -100px); }
}

@media (max-width: 768px) {
  .form-grid {
    grid-template-columns: 1fr;
  }

  .modern-form {
    padding: 1.5rem;
    margin: 1rem;
  }
}

.rich-text-editor {
  border: 2px solid #e2e8f0;
  border-radius: 0.75rem;
  overflow: hidden;
  transition: all 0.3s ease;
}

.rich-text-editor:focus-within {
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}



/* 手机预览样式 */
.phone-preview-container {
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.phone-frame {
  position: relative;
  width: 320px;
  height: 640px;
  background: #000;
  border-radius: 40px;
  padding: 15px;
  box-shadow: 0 0 0 12px #1f1f1f, 0 0 30px rgba(0,0,0,0.3);
}

.phone-header {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 180px;
  height: 25px;
  background: #000;
  border-radius: 12px;
}

.phone-camera {
  position: absolute;
  top: 50%;
  left: 20px;
  transform: translateY(-50%);
  width: 10px;
  height: 10px;
  background: #15294c;
  border-radius: 50%;
}

.phone-speaker {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 6px;
  background: #1f1f1f;
  border-radius: 3px;
}

.phone-screen {
  width: 100%;
  height: 100%;
  background: #f5f5f5;
  border-radius: 28px;
  overflow: hidden;
  position: relative;
}

.phone-content {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.phone-home-button {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #1f1f1f;
  cursor: pointer;
}


.phone-section-content * {
  max-width: 100%;
}

.phone-section-content img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 10px 0;
}

.phone-section-content h1,
.phone-section-content h2,
.phone-section-content h3 {
  color: #2c3e50;
  margin-top: 15px;
  margin-bottom: 10px;
}

.phone-section-content p {
  margin-bottom: 10px;
}

.phone-section-content ul,
.phone-section-content ol {
  margin-left: 20px;
  margin-bottom: 10px;
}



@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
</style>
