<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="chapterName">
        <el-input
          v-model="queryParams.chapterName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hezhi:courseChapter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="courseChapterList"
      row-key="chapterId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
<!--      <el-table-column label="课程ID" prop="courseId" />-->
<!--      <el-table-column label="父级ID" align="center" prop="pid" />-->
<!--      <el-table-column label="层级 0 1 2 " align="center" prop="level" />-->
<!--      <el-table-column label="类型" align="center" prop="type" />-->
      <el-table-column label="课程名称" align="center" prop="chapterName" />
<!--      <el-table-column label="链接类型" align="center" prop="urlType" />-->
      <el-table-column label="资源" align="center" prop="chapterUrl">
        <template slot-scope="scope" v-if="scope.row.isVideo===1">
          <el-link type="primary" @click="getVideo(scope.row)">
            {{scope.row.chapterUrl}}
          </el-link>
        </template>
      </el-table-column>
<!--      <el-table-column label="资源" align="center" prop="chapterUrl" >-->
<!--        <template slot-scope="scope">-->
<!--          <video width="120" height="90" controls v-if="scope.row.isVideo===1">-->
<!--            <source :src="scope.row.chapterUrl" type="video/mp4" >-->
<!--          </video>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="是否视频" align="center" prop="isVideo" />-->
<!--      <el-table-column label="备注" align="center" prop="remarks" />-->
      <el-table-column label="排序" align="center" prop="sort" />
<!--      <el-table-column label="状态" align="center" prop="status" />-->

      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hezhi:courseChapter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-if="scope.row.pid === 0"
            v-hasPermi="['hezhi:courseChapter:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hezhi:courseChapter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改课程章节对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="课程章节" prop="chapterName">
          <el-input v-model="form.chapterName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="链接类型" prop="urlType" v-if="form.pid !== 0">
          <el-radio v-model="form.urlType" :label="1">上传视频</el-radio>
          <el-radio v-model="form.urlType" :label="2">输入链接</el-radio>
        </el-form-item>
        <el-form-item label="上传视频" prop="chapterUrl" v-if="form.urlType===1">
          <video-upload v-model="form.chapterUrl"  />
        </el-form-item>
        <el-form-item label="输入链接" prop="chapterUrl" v-if="form.urlType === 2">
          <el-input v-model="form.chapterUrl" placeholder="请输入链接" />
        </el-form-item>
        <el-form-item label="视频时长" prop="videoDuration" v-if="form.pid !== 0">
          <el-input v-model="form.videoDuration" placeholder="请输入视频时长" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openVideo" @close="openVideoCancel" width="600px" append-to-body>
      <el-form ref="formVideo" :model="formVideo" :rules="rules" label-width="90px">
        <el-form-item prop="carVideo">
          <video style="width: 100%; height: 100%" :src="formVideo.chapterUrl" disabled controls />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openVideoCancel">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCourseChapter, getCourseChapter, delCourseChapter, addCourseChapter, updateCourseChapter } from "@/api/hezhi/courseChapter";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "CourseChapter",
  components: {
    Treeselect
  },
  props: ["screenCourseId"],
  data() {
    return {
      openVideo: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 课程章节表格数据
      courseChapterList: [],
      // 课程章节树选项
      courseChapterOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        courseId: null,
        pid: null,
        level: null,
        type: null,
        chapterName: null,
        urlType: null,
        chapterUrl: null,
        videoDuration: null,
        isVideo: null,
        remarks: null,
        sort: null,
        status: null,
        publicationTime: null,
        publisher: null,
        deleteTime: null,
        deleteBy: null,
      },
      // 表单参数
      form: {},
      formVideo: {},
      // 表单校验
      rules: {
        chapterName: [
          { required: true, trigger: "blur", message: "请输入名称" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    openVideoCancel() {
      this.formVideo.chapterUrl = null;
      this.openVideo = false;
    },
    /** 视频弹窗 */
    getVideo(row) {
      this.formVideo.chapterUrl = row.chapterUrl;
      this.openVideo = true;
    },

    /** 查询课程章节列表 */
    getList() {
      this.loading = true;
      if (this.screenCourseId != null) {
        this.queryParams.courseId = this.screenCourseId;
      }
      console.log("this.screenCourseId", this.screenCourseId);
      listCourseChapter(this.queryParams).then(response => {
        this.courseChapterList = this.handleTree(response.data, "chapterId", "pid");
        this.loading = false;
      });
    },
    /** 转换课程章节数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.chapterId,
        label: node.chapterName,
        children: node.children
      };
    },
	/** 查询课程章节下拉树结构 */
    getTreeselect() {
      listCourseChapter().then(response => {
        this.courseChapterOptions = [];
        const data = { chapterId: 0, chapterName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "chapterId", "pid");
        this.courseChapterOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chapterId: null,
        courseId: null,
        pid: null,
        level: null,
        type: null,
        chapterName: null,
        urlType: null,
        chapterUrl: null,
        videoDuration: null,
        isVideo: null,
        remarks: null,
        sort: null,
        status: null,
        publicationTime: null,
        publisher: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleteTime: null,
        deleteBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.chapterId) {
        this.form.pid = row.chapterId;
      } else {
        this.form.pid = 0;
      }
      this.open = true;
      this.title = "添加课程章节";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.pid = row.pid;
      }
      getCourseChapter(row.chapterId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程章节";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.chapterId != null) {
            updateCourseChapter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.screenCourseId != null) {
              this.form.courseId = this.screenCourseId;
            }
            addCourseChapter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除课程章节编号为"' + row.chapterId + '"的数据项？').then(function() {
        return delCourseChapter(row.chapterId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
