<template>
  <div class="meedu-main-body">
    <back-bar class="mb-30" title="新建电子书"></back-bar>

    <div class="float-left">
      <el-form
        ref="form"
        :model="course"
        :rules="rules"
        class="float-left"
        label-width="200px"
      >
        <div class="float-left">
          <el-form-item prop="cid" label="所属分类">
            <div class="d-flex">
              <div>
                <el-select class="w-300px" v-model="course.cid">
                  <el-option
                    v-for="(item, index) in categories"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </div>
              <div class="ml-10">
                <p-link
                  text="分类管理"
                  p="addons.meedu_books.book_category.list"
                  type="primary"
                  @click="$router.push({ name: 'MeedubookCategory' })"
                ></p-link>
              </div>
            </div>
          </el-form-item>

          <el-form-item label="电子书名称" prop="name">
            <el-input
              v-model="course.name"
              class="w-300px"
              placeholder="请输入电子书名称"
            ></el-input>
          </el-form-item>

          <el-form-item prop="thumb" label="电子书封面">
            <upload-image
              v-model="course.thumb"
              helper="长宽比3:4，建议尺寸：300x400像素"
              width="90"
              height="120"
              name="上传封面"
            ></upload-image>
          </el-form-item>

          <el-form-item label="免费" prop="is_free">
            <div class="d-flex">
              <div>
                <el-switch
                  v-model="is_free"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </div>
            </div>
          </el-form-item>

          <el-form-item label="价格" prop="charge" v-if="is_free === 0">
            <div class="d-flex">
              <div>
                <el-input
                  type="number"
                  placeholder="单位：元"
                  v-model="course.charge"
                  class="w-300px"
                ></el-input>
              </div>
              <div class="ml-10">
                <helper-text text="最小单位“元”，不支持小数"></helper-text>
              </div>
            </div>
          </el-form-item>

          <el-form-item
            label="会员免费"
            prop="is_vip_free"
            v-if="course.charge > 0"
          >
            <div class="d-flex">
              <div>
                <el-switch
                  v-model="course.is_vip_free"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </div>
              <div class="ml-10">
                <helper-text
                  text="如果开启该选项，则购买VIP会员的学员可以无需购买即可观看该电子书。"
                ></helper-text>
              </div>
            </div>
          </el-form-item>

          <el-form-item label="上架时间" prop="published_at">
            <div class="d-flex">
              <div>
                <el-date-picker
                  style="width: 300px"
                  v-model="course.published_at"
                  type="datetime"
                  format="yyyy-MM-dd HH:mm"
                  value-format="yyyy-MM-dd HH:mm"
                  placeholder="请选择日期"
                >
                </el-date-picker>
              </div>
              <div class="ml-10">
                <helper-text text="上架时间越晚，排序越靠前"></helper-text>
              </div>
            </div>
          </el-form-item>

          <el-form-item label="隐藏">
            <div class="d-flex">
              <div>
                <el-switch
                  v-model="course.is_show"
                  :active-value="0"
                  :inactive-value="1"
                >
                </el-switch>
              </div>
              <div class="ml-10">
                <helper-text text="打开后此电子书在前台隐藏显示"></helper-text>
              </div>
            </div>
          </el-form-item>

          <el-form-item prop="short_desc" label="简短介绍">
            <el-input
              class="w-800px"
              rows="4"
              type="textarea"
              maxlength="150"
              show-word-limit
              v-model="course.short_desc"
              placeholder="简短介绍"
            ></el-input>
          </el-form-item>

          <el-form-item prop="original_desc" label="详情介绍">
            <div class="w-800px">
              <quill-editor
                :height="400"
                v-model="course.original_desc"
              ></quill-editor>
            </div>
          </el-form-item>
        </div>
      </el-form>

      <div class="bottom-menus">
        <div class="bottom-menus-box">
          <div>
            <el-button @click="formValidate" :loading="loading" type="primary"
              >保存</el-button
            >
          </div>
          <div class="ml-24">
            <el-button @click="$router.back()">取消</el-button>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :visible.sync="visible"
      width="500px"
      @close="$router.replace({ name: 'Meedubook' })"
    >
      <div class="text-center">
        <span>新建电子书成功，请在电子书中添加文章吧！</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$router.replace({ name: 'Meedubook' })"
          >暂不添加</el-button
        >
        <el-button @click="goArticle" type="primary">立即添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import QuillEditor from "@/components/quill-editor";
import UploadImage from "@/components/upload-image";

export default {
  components: {
    QuillEditor,
    UploadImage,
  },
  data() {
    return {
      is_free: 0,
      course: {
        published_at: null,
        is_show: 1,
        charge: null,
        name: null,
        cid: null,
        is_vip_free: 0,
        thumb: null,
        short_desc: null,
        original_desc: null,
        render_desc: null,
      },
      rules: {
        cid: [
          {
            required: true,
            message: "分类不能为空",
            trigger: "blur",
          },
        ],
        charge: [
          {
            required: true,
            message: "价格不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "电子书名称不能为空",
            trigger: "blur",
          },
        ],
        thumb: [
          {
            required: true,
            message: "请上传封面",
            trigger: "blur",
          },
        ],
        short_desc: [
          {
            required: true,
            message: "简短介绍不能为空",
            trigger: "blur",
          },
        ],
        original_desc: [
          {
            required: true,
            message: "详细介绍不能为空",
            trigger: "blur",
          },
        ],
        published_at: [
          {
            required: true,
            message: "上架时间不能为空",
            trigger: "blur",
          },
        ],
      },
      categories: [],
      loading: false,
      visible: false,
    };
  },
  watch: {
    is_free(val) {
      if (val === 1) {
        this.course.charge = 0;
      }
    },
  },
  mounted() {
    this.params();
  },
  methods: {
    params() {
      this.$api.Meedubook.Book.Create().then((res) => {
        this.categories = res.data.categories;
      });
    },
    formValidate() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.confirm();
        }
      });
    },
    confirm() {
      if (this.loading) {
        return;
      }
      if (this.course.charge % 1 !== 0) {
        this.$message.error("电子书价格必须为整数");
        return;
      }
      this.loading = true;
      this.course.render_desc = this.course.original_desc;
      this.$api.Meedubook.Book.Store(this.course)
        .then(() => {
          this.loading = false;
          this.visible = true;
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    goArticle() {
      this.$api.Meedubook.Book.List({
        page: 1,
        size: 1,
        sort: "id",
        order: "desc",
      }).then((res) => {
        this.$router.replace({
          name: "MeedubookArticle",
          query: { bid: res.data.data.data[0].id },
        });
      });
    },
  },
};
</script>
