<script setup>
import { ElMessage } from "element-plus";

const props = defineProps({
  id: String,
});

const kz_img = ref("");
let form = $ref({
  kz_img: "",
  kz_master_teacher: "",
  kz_master_user_name: "",
  kz_title: "",
  kz_kcjj: "",
  kz_kss: "",
  grade_id: "",
  grade_name: "",
  subject_id: "",
  subject_name: "",
  kz_starttime: "",
  kz_endtime: "",
  kz_recommend: "",
  kz_best: "",
  is_shenhe: "",
  shenhe_user: "",
  // shenhe_status: '',
  kz_time: undefined,
  kz_kcap: "",
});

let loading = $ref(true);

if (props.id) {
  request({
    url: "/kzkt/zbkc/detail",
    data: {
      id: props.id,
    },
  }).then((res) => {
    if (res.code === "1") {
      console.log("res :>> ", res);
      Object.keys(form).forEach((key) => {
        form[key] = res.data.one_info[key];
      });
      form.kz_starttime = res.data.one_info.starttime;
      form.kz_endtime = res.data.one_info.endtime
      loading = false;
    }
  });
} else {
  loading = false;
}

const rules = {
  kz_img: [{ required: true, message: "请选择课程封面图", trigger: "submit" }],
  kz_master_teacher: [
    { required: true, message: "请输入主讲老师", trigger: "submit" },
  ],
  kz_master_user_name: [
    { required: true, message: "请输入主讲账号", trigger: "submit" },
  ],
  kz_title: [{ required: true, message: "请输入课程名称", trigger: "submit" }],
  kz_kss: [{ required: true, message: "请输入课时数", trigger: "submit" }],
  kz_starttime: [
    { required: true, message: '请选择开始时间', trigger: 'submit', type: 'date' },
  ],
  kz_endtime: [
    { required: true, message: '请选择结束时间', trigger: 'submit', type: 'date' },
  ],
  // kz_time: [
  //   { required: true, message: "请选择开始时间和结束时间", trigger: "submit" },
  // ],
  grade_id: [{ required: true, message: "请选择年级", trigger: "submit" }],
  subject_id: [{ required: true, message: "请选择学科", trigger: "submit" }],
  kz_recommend: [
    { required: true, message: "请选择是否手机端推荐课程", trigger: "submit" },
  ],
  kz_best: [{ required: true, message: "请选择是否精品", trigger: "submit" }],
  is_shenhe: [
    { required: true, message: "请选择是否需要审核", trigger: "submit" },
  ],
  // shenhe_user: [
  //   { required: true, message: '请选择审核对象', trigger: 'submit' },
  // ],
  // shenhe_status: [
  //   { required: true, message: '请选择审核状态', trigger: 'submit' },
  // ],
};


const handleCompile = (fileList) => {
  let data = fileList[fileList.length - 1];
  form.kz_img = data.response.data.url;
}

let isRead = $ref(false);
let formRef = $ref();
function handleSubmit() {
  console.log("form :>> ", form);
  if (isRead) {
    formRef.validate().then((res) => {
      console.log("res :>> ", res);
      if (res === true) {
        if (new Date(form.kz_endtime).getTime() <= new Date(form.kz_starttime).getTime()) {
          ElMessage.warning("结束时间必须大于开始时间！");
          return;
        }
        const submitData = {
          kz_img: form.kz_img,
          kz_master_teacher: form.kz_master_teacher,
          kz_master_user_name: form.kz_master_user_name,
          kz_title: form.kz_title,
          kz_kcjj: form.kz_kcjj,
          kz_kss: form.kz_kss,
          grade_id: form.grade_id,
          grade_name: form.grade_name,
          subject_id: form.subject_id,
          subject_name: form.subject_name,
          kz_starttime: form.kz_starttime,
          kz_endtime: form.kz_endtime,
          kz_recommend: form.kz_recommend,
          kz_best: form.kz_best,
          kz_kcap: form.kz_kcap,
          is_shenhe: form.is_shenhe,
          shenhe_user: form.shenhe_user,
          shenhe_status: form.is_shenhe * 1 + 1,
        };
        request({
          url: `/kzkt/zbkc/${props.id ? "edit" : "add"}`,
          data: {
            kz_id: props.id,
            kzkt_zbkc: submitData,
          },
        }).then((res) => {
          if (res.code === "1") {
            ElMessage.success("创建课程成功");
            handleBack();
          }
        });
      }
    });
  } else {
    ElMessage.warning("请先勾选协议");
  }
}

const router = useRouter();
function handleBack() {
  router.push({ 'name': 'personal_create_wlzb' });
}
</script>

<template>
  <div class="w-full shadow bg-white rounded-md flex items-center px-36 py-4">
    <div class="text-lg text-hex-050026">名师优课</div>
    <el-divider direction="vertical" />
    <div class="text-sm">
      <span class="text-hex-949494">我的课程</span>
      <span class="text-hex-949494"> - 我创建的</span>
      <span class="text-hex-949494"> - 名师优课</span>
      <span> - 创建</span>
    </div>
  </div>

  <div class="
      w-full
      flex flex-col
      items-center
      shadow
      bg-white
      rounded-md
      mt-4
      p-4
    ">
    <div class="text-lg text-hex-050026 w-full font-semibold">创建</div>

    <div class="w-575px text-hex-949494" v-loading="loading">
      <el-form :model="form" label-position="top" ref="formRef" :rules="rules" v-if="!loading">
        <el-form-item class="kcfmt" label="课程封面图" prop="kz_img">
          <!-- <file-upload-progress
            v-model:part="form.kz_img"
            :limit="1"
            list-type="picture-card"
            accept=".jpg,.png,.jpeg"
            :size-limit="5120"
          >
            <template #default>
              <i-akar-icons:plus class="text-4xl" />
            </template>
            <template #tip>
              <div class="mt-2">
                支持格式：jpg,png,jpeg（5MB以内）
              </div>
            </template>
          </file-upload-progress> -->
          <file-upload-progress v-model:part="kz_img" list-type="picture-card" accept=".jpg,.png,.jpeg" :size-limit="5*1024" @compile="handleCompile">
            <template #default>
              <el-icon size="38" color="#9d9ab9" v-if="form.kz_img==''">
                <Picture />
              </el-icon>
              <img v-else :src="getFullUrl(form.kz_img)" alt="" style="width: 100%;height: 100%;" />
            </template>
            <template #tip>
              <div style="color: #949494;font-size: 12px;margin-left: 20px;display: inline-block;vertical-align: top;line-height: 20px;">支持格式：jpg,png,jpeg（5M以内） 建议尺寸600x320</div>
            </template>
          </file-upload-progress>
        </el-form-item>

        <el-form-item label="主讲账号" prop="kz_master_user_name">
          <el-input v-model="form.kz_master_user_name" />
        </el-form-item>
        <el-form-item label="主讲老师" prop="kz_master_teacher">
          <el-input v-model="form.kz_master_teacher" />
        </el-form-item>
        <el-form-item label="课程名称" prop="kz_title">
          <el-input v-model="form.kz_title" />
        </el-form-item>
        <el-form-item label="课程简介" prop="kz_kcjj">
          <el-input v-model="form.kz_kcjj" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item label="课时数" prop="kz_kss">
          <el-input v-model.number="form.kz_kss" />
        </el-form-item>
        <div class="flex justify-between">
          <el-form-item label="年级" prop="grade_id">
            <remote-multi-select v-model="form.grade_id" v-model:model-name="form.grade_name" to="grade"></remote-multi-select>
          </el-form-item>
          <el-form-item label="学科" prop="subject_id">
            <remote-multi-select v-model="form.subject_id" v-model:model-name="form.subject_name" to="subject"></remote-multi-select>
          </el-form-item>
        </div>
        <div class="flex justify-between">
          <el-form-item label="开始时间" prop="kz_starttime">
            <el-date-picker v-model="form.kz_starttime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" />
          </el-form-item>
          <el-form-item label="结束时间" prop="kz_endtime">
            <el-date-picker v-model="form.kz_endtime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" />
          </el-form-item>
        </div>

        <!--        <el-form-item label="开始时间 - 结束时间" prop="kz_time">-->
        <!--          <el-date-picker-->
        <!--            v-model="form.kz_time"-->
        <!--            type="datetimerange"-->
        <!--            value-format="YYYY-MM-DD HH:mm:ss"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <div class="flex justify-between">
          <el-form-item label="是否手机端推荐课程" prop="kz_recommend">
            <el-radio-group v-model="form.kz_recommend">
              <el-radio label="1">推荐</el-radio>
              <el-radio label="0">不推荐</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否精品" prop="kz_best">
            <el-radio-group v-model="form.kz_best">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>

        <el-form-item label="是否需要审核" prop="is_shenhe">
          <el-radio-group v-model="form.is_shenhe">
            <el-radio label="1">需要审核</el-radio>
            <el-radio label="0">不需要审核</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="审核状态" prop="shenhe_status">
          <el-select v-model="form.shenhe_status" class="w-full">
            <el-option label="不需要审核" value="0"></el-option>
            <el-option label="待审核" value="1"></el-option>
            <el-option label="审核通过" value="2"></el-option>
            <el-option label="审核不通过" value="3"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="审核对象" prop="shenhe_user">
          <el-input v-model="form.shenhe_user" />
        </el-form-item> -->

        <el-form-item label="课程安排" prop="kz_kcap">
          <file-upload-progress v-model:part="form.kz_kcap" :limit="1" accept=".doc,.docx,.xls,.xlsx">
            <template #default>
              <div class="
                  flex_center
                  w-65px
                  h-65px
                  border border-dashed border-hex-E4E6E8
                  bg-hex-F9F9F9
                  text-hex-9D9AB9
                ">
                <i-ant-design:folder-open-outlined class="text-xl" />
              </div>
            </template>
          </file-upload-progress>
        </el-form-item>

        <el-checkbox v-model="isRead" label="已阅读并同意《课程上传协议》" size="large" />

        <div class="flex justify-around items-center text-sm mt-8">
          <div class="
              w-166px
              h-50px
              flex_center
              rounded
              bg-hex-00A3FF
              text-white
              cursor-pointer
            " @click="handleSubmit">
            提交
          </div>
          <div class="
              w-166px
              h-50px
              flex_center
              rounded
              bg-hex-F4F4F4
              text-hex-41387F
              cursor-pointer
            " @click="handleBack">
            关闭
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>


<style lang="scss" scoped>
:deep() {
  .kcfmt {
    .el-upload--picture-card {
      width: 180px;
      height: 96px;
    }

    .el-upload-list.el-upload--picture-card,
    .el-upload-list__item {
      display: none;
    }
  }
}
</style>
