<template>
  <div class="app-container">
    <!--操作区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      @click="create('add')"
      class="margin-bottom"
      >添加章节</el-button
    >
    <!--搜索区-->
    <div class="float">
      <!-- <el-input
        v-model="page.data.title"
        placeholder="请选择课件名"
        clearable
        class="margin-left input"
      ></el-input> -->
      <el-input
        v-model="page.data.title"
        placeholder="请输入标题"
        clearable
        class="margin-left input"
      ></el-input>
      <el-input
        v-model="page.data.description"
        placeholder="请输入概述"
        clearable
        class="margin-left input"
      ></el-input>
      <el-date-picker
        v-model="page.data.startTime"
        type="datetime"
        placeholder="选择起始时间"
        value-format="yyyy-MM-dd HH:mm:ss"
        class="margin-left"
      >
      </el-date-picker>
      <el-date-picker
        v-model="page.data.endTime"
        type="datetime"
        placeholder="选择结束时间"
        value-format="yyyy-MM-dd HH:mm:ss"
        class="margin-left"
      >
      </el-date-picker>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData"
        class="margin-left"
        >搜索</el-button
      >
    </div>
    <!--主表格-->
    <el-table
      class="table"
      ref="table_form"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="概述" align="center">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column label="学习时长" align="center">
        <template slot-scope="scope">
          {{ scope.row.duration }}
        </template>
      </el-table-column>
      <el-table-column label="时长单位" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.timeUnit == 2" type="success">小时</el-tag>
          <el-tag v-if="scope.row.timeUnit == 1" type="paimary">分钟</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="人脸次数" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="弹窗次数" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="200">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteFun(scope.row.stStudyContentId)"
            >删除</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="create('update', scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!--对话框-->
    <el-dialog
      :title="dialogTitle[dialogType]"
      :visible.sync="dialogVisible"
      width="900px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
        label-position="left"
      >
        <div class="flex">
          <el-form-item label="标题" class="form-item-style" prop="title">
            <el-input
              placeholder="请输入标题"
              v-model.trim="form.title"
              maxlength="50"
              show-word-limit
            />
          </el-form-item>
          <el-form-item
            label="概述"
            class="form-item-style special"
            prop="description"
          >
            <el-input
              placeholder="请输入概述"
              v-model.trim="form.description"
              maxlength="150"
              show-word-limit
            />
          </el-form-item>
        </div>
        <QuillEditor
          @getFormItem="getFormItem"
          :quillEditorVal="form.content"
          :clearVal="clearVal"
        />
        <div class="title">
          <div class="line"></div>
          <span>属性设置</span>
        </div>
        <div class="flex">
          <el-form-item label="学习时长" prop="duration" style="flex: 1">
            <el-input-number
              v-model="form.duration"
              :min="1"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="时长单位" prop="content" style="flex: 1">
            <el-radio v-model="form.timeUnit" :label="1">分钟</el-radio>
            <el-radio v-model="form.timeUnit" :label="2">小时</el-radio>
          </el-form-item>
        </div>
        <!-- <div class="flex">
          <el-form-item label="人脸次数" prop="ccccc" style="flex: 1">
            <el-input-number
              :disabled="true"
              v-model="form.ccccc"
              :min="1"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="弹窗次数" prop="description" style="flex: 1">
            <el-input-number
              :disabled="true"
              v-model="form.description"
              :min="1"
              size="mini"
            ></el-input-number>
          </el-form-item>
        </div>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="addOrUpdate"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import QuillEditor from "@/components/QuillEditor/index.vue";
import { studyDelete, studyEdit, studyDetPageList } from "@/api/examination";
export default {
  components: { Pagination, QuillEditor },
  filters: {},
  computed: {},
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          startTime: null,
          endTime: null,
          title: null,
          description: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: "添加章节",
        update: "编辑章节",
      },
      dialogType: "add",
      form_copy: null,
      // 添加角色参数
      form: {
        title: null,
        description: null,
        content: null,
        duration: null,
        timeUnit: 1,
      },
      // 验证规则
      rules: {
        title: [{ required: true, message: "请输入", trigger: "change" }],
      },
      clearVal: false,
    };
  },
  created() {
    this.fetchData();
    this.form_copy = JSON.parse(JSON.stringify(this.form));
  },
  watch: {},
  methods: {
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      studyDetPageList(this.page).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    // 打开添加角色对话框
    create(type, row) {
      if (type === "add") {
        this.clearVal = !this.clearVal;
        this.form = JSON.parse(JSON.stringify(this.form_copy));
      } else {
        this.form = Object.assign(this.form, row);
      }
      this.dialogType = type;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          studyEdit(this.form).then((res) => {
            this.dialogVisible = false;
            this.fetchData();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
        }
      });
    },
    // 删除
    deleteFun(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        studyDelete(id).then((res) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    // 图片赋值
    getImg(val, index, type) {
      this.form[type] = val;
    },
    // 赋值
    getFormItem(val, type) {
      this.form[type] = val;
      // console.log(11111,val, type);
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>
<style scoped lang="scss">
</style>
