<template>
  <div class="courseCreate">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="上传源文件" prop="ssz_file">
        <multi-file-upload v-model:part="ruleForm.ssz_file" @image="initImageData" :limit="1" accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.mp4,.mp3" :size-limit="500*1024">
          <template #default>
            <div class="flex_center w-65px h-65px border border-dashed border-hex-E4E6E8 bg-hex-F9F9F9 text-hex-9D9AB9">
              <i-akar-icons:plus class="text-xl" />
            </div>
          </template>
          <template #tip>
            <div class="mt-2">支持格式：doc,docx,xls,xlsx,ppt,pptx,pdf,mp4,mp3（500M以内）</div>
          </template>
        </multi-file-upload>
      </el-form-item>
      <el-form-item label="课程封面图" prop="ssz_img">
        <div class="uploadDiv">
          <file-upload-progress v-model:part="ruleForm.ssz_img" :limit="1" accept=".png,.jpg,.jpeg" :size-limit="200">
            <template #default>
              <div class="flex_center w-65px h-65px border border-dashed border-hex-E4E6E8 bg-hex-F9F9F9 text-hex-9D9AB9">
                <i-akar-icons:plus class="text-xl" />
              </div>
            </template>
            <template #tip>
              <div class="mt-2">支持格式：jpg,png,jpeg（200kb以内） 建议尺寸8.5:5</div>
            </template>
          </file-upload-progress>
          <div class="tips" v-if="imageData.length!=0">
            <div style="color: #f35421;font-size: 12px;margin: 10px 0;">如上不上传，封面自动抓取，提供前5帧当中的任意三张图片（如下）供教师选择</div>
            <div class="flex">
              <img class="pointer" v-for="(item, index) in imageData" :key="index" :src="getFullUrl(item)" alt="" @click="initImg(item)" />
            </div>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="课程名称" prop="ssz_name">
        <el-input v-model="ruleForm.ssz_name" />
      </el-form-item>
      <el-form-item label="课程简介" prop="ssz_jj">
        <el-input v-model="ruleForm.ssz_jj" type="textarea" rows="5" />
      </el-form-item>
      <div class="flex justify-between">
        <el-form-item label="年级" prop="grade_id">
          <remote-select v-model:model-name="ruleForm.grade_name" v-model="ruleForm.grade_id" to="grade"></remote-select>
        </el-form-item>
        <el-form-item label="学科" prop="subject_id">
          <remote-select v-model:model-name="ruleForm.subject_name" v-model="ruleForm.subject_id" to="subject"></remote-select>
        </el-form-item>
      </div>
      <el-form-item label="主讲老师" prop="ssz_zjls_name">
        <el-input v-model="ruleForm.ssz_zjls_name" />
      </el-form-item>
      <div class="btnDiv" style="text-align: center;">
        <el-button @click="emit('update', 2)">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import MultiFileUpload from "./MultiFileUpload.vue";
import { Picture } from "@element-plus/icons-vue";
import { getFullUrl } from "~/utils/helper";
import { schoolZpAdd, schoolZpEdit, schoolZpDetail } from "../api";

const props = defineProps({
  current: {
    type: String,
    default: "",
  },
  parentId: {
    type: String,
    default: "",
  }
});

const ruleFormRef = ref();
const ruleForm = reactive({
  ss_id: props.parentId,
  ssz_file: "",
  ssz_img: "",
  ssz_name: "",
  ssz_jj: "",
  grade_id: "",
  grade_name: "",
  subject_id: "",
  subject_name: "",
  ssz_zjls_name: "",
})

const rules = reactive({
  ssz_file: [{ required: true, message: "请上传源文件", trigger: "submit" }],
  ssz_img: [{ required: true, message: "请上传课程封面图", trigger: "submit" }],
  ssz_name: [{ required: true, message: "请输入课程名字", trigger: "blur" }],
  ssz_jj: [{ required: true, message: "请输入课程简介", trigger: "blur" }],
  grade_id: [{ required: true, message: "请选择年级", trigger: "change" }],
  subject_id: [{ required: true, message: "请选择学科", trigger: "change" }],
  ssz_zjls_name: [{ required: true, message: "请输入主讲老师", trigger: "blur" }],
});


const imageData = ref([]);
const initImageData = (data) => {
  imageData.value = data || [];
  if (imageData.value.length != 0 && ruleForm.ssz_img == "") {
    ruleForm.ssz_img = imageData.value[0];
  }
}

const initImg = (img) => {
  ruleForm.ssz_img = img;
};

const emit = defineEmits(["update", "load"]);
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        szmsg_school_zp: ruleForm,
      };
      emit("load", true);
      if (props.current == "") {
        await schoolZpAdd(data);
      } else {
        data = Object.assign({ ssz_id: props.current }, data);
        await schoolZpEdit(data);
      }
      emit("load", false);
      ElMessage.success("保存成功！");
      emit("update", 1);
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

watch(
  () => props.current,
  (newVal, oldVal) => {
    if (newVal != "") {
      initDetailInfo(newVal);
    } else {
      resetForm(ruleFormRef.value);
    }
  },
  { immediate: true }
);

async function initDetailInfo(ssz_id) {
  emit("load", true);
  let data = await schoolZpDetail({ ssz_id });
  for (let key in ruleForm) {
    ruleForm[key] = data.one_info[key];
  }
  emit("load", false);
}
</script>

<style lang="scss" scoped>
.courseCreate {
  ::v-deep .uploadDiv {
    flex: 1;
    .el-upload--picture-card {
      width: 180px;
      height: 96px;
    }
    .mainUpload {
      .el-upload--picture-card {
        width: 180px;
        height: 96px;
      }
      .el-upload-list--picture-card .el-upload-list__item {
        display: none;
      }
    }
    .attachUpload {
      .el-upload-dragger {
        width: 65px;
        height: 65px;
        padding: 0;
        line-height: 65px;
        background: #f9f9f9;
      }
    }
    .tips {
      img {
        width: 90px;
        height: 48px;
        border-radius: 4px;
        margin-right: 10px;
      }
    }
  }
}
</style>