<template>
  <el-dialog v-model="props.visible" :title="dialogTitle" :width="'1350px'" :close-on-click-modal="false"
    :before-close="dialogClose" style="margin-top:15px;">
    <el-form style="display: flex" ref="formRef" :model="formData" label-width="80px">
      <div style="width: 880px;flex: none" :style="{ height: fwbHeight + 'px' }">
        <el-form-item label-width="0px" prop="content">
          <Editor ref="editorRef" :height="fwbHeight" name="content" />
        </el-form-item>
      </div>
      <div style="flex:1;margin-left: 10px">
        <el-form-item label="广告标题" prop="title" :rules="{ required: true, message: '请输入广告标题', trigger: 'blur' }">
          <el-input placeholder="请输入广告标题" :maxlength="50" v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item label="广告位" prop="adSortId" class="flex-1"
          :rules="{ required: true, message: '请选择广告位', trigger: 'change' }">
          <el-select v-model="formData.adSortId" class="flex-1" clearable placeholder="请选择广告位">
            <el-option v-for="(item, index) in adSortList" :key="index" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="广告格式" prop="type" :rules="{ required: true, message: '请选择广告格式', trigger: 'change' }">
          <el-select v-model="formData.type" placeholder="请选择广告格式">
            <el-option label="图片" :value="1"></el-option>
            <el-option label="文字" :value="2"></el-option>
            <el-option label="视频" :value="3"></el-option>
            <el-option label="推荐" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告封面" prop="cover" :rules="{ required: true, message: '请上传广告封面', trigger: 'change' }">
          <UploadImg
            :fileType="['image/jpeg', 'image/png', 'image/jpg', 'image/gif']" name="ad" :fileSize="200"
            v-model:image-url="formData.cover">
            <template v-slot:tip>支持扩展名: jpg png jpeg;文件大小不超过200M</template>
          </UploadImg>
        </el-form-item>
        <el-form-item label="广告链接" prop="url" :rules="{ required: true, message: '请输入广告链接', trigger: 'blur' }">
          <el-input v-model="formData.url" type="textarea" placeholder="请输入广告链接" />
        </el-form-item>
          <el-form-item label="广告宽度" prop="width"
            :rules="{ required: true, message: '请输入广告宽度', trigger: 'change' }">
            <number-input v-model="formData.width" placeholder="请输入广告宽度" clearable />
          </el-form-item>
          <el-form-item label="广告高度" prop="height"
            :rules="{ required: true, message: '请输入广告高度', trigger: 'blur' }">
            <number-input v-model="formData.height" placeholder="请输入广告高度" clearable />
          </el-form-item>
        <el-form-item label="广告时间" prop="adTime" class="flex-1" :rules="{ required: true, message: '请选择广告时间', trigger: 'change' }">
          <el-date-picker v-model="formData.adTime" value-format="YYYY-MM-DD HH:mm:ss" @change="timeChange" style="width:100%" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="点击率" prop="click" :rules="{ required: true, message: '请输入点击率', trigger: 'blur' }">
          <number-input v-model="formData.click" placeholder="请输入点击率" clearable />
        </el-form-item>
        <el-form-item label="广告描述" prop="note" :rules="{ required: true, message: '请输入备注', trigger: 'blur' }">
          <el-input v-model="formData.note" type="textarea" placeholder="请输入广告描述" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="formData.sort" :max="9999" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="formData.status" :label="1">在用</el-radio>
          <el-radio v-model="formData.status" :label="2">停用</el-radio>
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button :loading="subLoading" type="primary" @click="submit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { adAdd, adUpdate, getAdDetail } from '@/api/content/ad';
import { getAdSortAllList } from '@/api/content/adSort';
import { computed, onMounted, ref, shallowRef, reactive } from "vue";
import { FormInstance } from "element-plus";
const formRef = shallowRef<FormInstance>();
import UploadImg from "@/components/Upload/Image.vue";
import { useLockFn } from "@/utils/useLockFn";
import Editor from '@/components/Editor/tinymce.vue'
import { message } from "@/utils/auth";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  adId: {
    type: Number,
    required: true,
    default: 0
  },
  searchParamData: {
    type: Object,
    default: {}
  }
});
const adSortList = ref([])
const adTime = ref([])
const emit = defineEmits(["success", "update:visible"]);

const dialogTitle = computed(() => {
  return props.adId ? "编辑内容" : "新增内容";
});

const fwbHeight = document.body.clientHeight - 180

const formData = reactive({
  id: "",
  title: "",
  adSortId: '',
  cover: '',
  type: '',
  note: '',
  content: '',
  url: '',
  width: "",
  height: "",
  adTime:[],
  startTime: '',
  endTime: '',
  click: '',
  status: 1,
  sort: 0,
});

const dialogClose = () => {
  emit("update:visible", false);
};

const setFormData = async () => {
  const data = await getAdDetail(props.adId);
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key];
    }
  }
  formData.adTime = [formData.startTime,formData.endTime]
  editorRef.value.myValue = formData.content
};

const editorRef = ref()
const handleSubmit = async () => {
    formData.startTime = formData.adTime.length>0?formData.adTime[0]:''
  formData.endTime = formData.adTime.length>0?formData.adTime[1]:''
  await formRef.value?.validate();
  let ruleForm = JSON.parse(JSON.stringify(formData));
  ruleForm.content = editorRef.value.myValue
  if (!ruleForm.content) {
    message("请添加文章内容", "error");
    return;
  }
  props.adId ? await adUpdate(ruleForm) : await adAdd(ruleForm);
  message("操作成功");
  emit("update:visible", false);
  emit("success");
};

const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);
const getAllDict = async () => {
  let list = await getAdSortAllList();
  adSortList.value = list ? list : [];
};
onMounted(() => {
  getAllDict()
  if (props.adId) {
    setFormData();
  }
});

</script>
