<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      直播课管理
    </h2>
    <div class="m-core">
      <auth auths="imLiveBroadcastRoom/add">
        <el-button
          @click="newLiveClass"
          type="primary"
          style="margin-bottom: 20px"
          >新建直播课</el-button
        >
      </auth>
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
        ref="searchForm"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="直播间编号/标题"
          ></el-input>
        </el-form-item>
        <el-form-item prop="popularize">
          <el-select
            placeholder="是否推广"
            v-model="searchForm.popularize"
            clearable
          >
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="liveState">
          <el-select
            placeholder="直播状态"
            v-model="searchForm.liveState"
            clearable
          >
            <el-option label="未开始" value="0"></el-option>
            <el-option label="直播中" value="1"></el-option>
            <el-option label="已结束" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="timer">
          <el-date-picker
            v-model.trim="searchForm.timer"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="直播开始日期"
            end-placeholder="直播结束日期"
            :picker-options="{
              firstDayOfWeek: 1,
            }"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="id"
            label="直播间编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="roomTitle"
            label="直播课标题"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="speakerName"
            label="主讲人"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="liveStartTime"
            label="直播开始时间"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.liveStartTime | dateForMinFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="popularize" label="是否推广">
            <template slot-scope="scope">
              <div>
                {{ scope.row.popularize == 1 ? "是" : "否" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="liveState" label="直播状态">
            <template slot-scope="scope">
              <div>
                {{ scope.row.liveState | liveState }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="createdByName"
            label="创建人"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="操作">
            <template slot-scope="scope">
              <div>
                <auth auths="imLiveBroadcastRoom/opsPopularize"  v-if="scope.row.liveState != 2">
                  <el-button type="text" @click="popularizeRoom(scope.row)">{{
                    scope.row.popularize == 1 ? "取消推广" : "首页推广"
                  }}</el-button>
                </auth>
                <auth auths="imLiveBroadcastRoom/roomDestroy">
                  <el-button
                    type="text"
                    v-if="scope.row.liveState == 1"
                    @click="closeRoom(scope.row)"
                    >关闭直播</el-button
                  >
                </auth>

                <el-button
                  type="text"
                  v-if="scope.row.liveState == 2"
                  @click="gotoDetail(scope.row)"
                  >直播详情</el-button
                >
                <auth auths="imLiveBroadcastRoom/shareGroup">
                  <el-button type="text" @click="shareLive(scope.row)"
                    >分享</el-button
                  >
                </auth>
                <auth auths="imLiveBroadcastRoom/update">
                  <el-button
                    type="text"
                    v-if="scope.row.liveState == 0"
                    @click="resetLive(scope.row)"
                    >修改</el-button
                  >
                </auth>
                <auth auths="imLiveBroadcastRoom/delete">
                  <el-button
                    type="text"
                    v-if="scope.row.liveState == 0"
                    @click="deteleLive(scope.row)"
                    >删除</el-button
                  >
                </auth>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
    <el-dialog
      title="分享"
      width="510px"
      append-to-body
      :visible.sync="shareVisible"
      v-if="shareVisible"
    >
      <shareDetail @close="shareVisible = false" :row="activeRow" />
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import { getTimes } from "@/utils";
import pagination from "@/components/Pagination/index";
import shareDetail from "./modals/shareDetail.vue";
import load from "@/utils/loading";
import {
  getLiveBroadcastList,
  delLiveBroadcast,
  closeBroadcastRoomList,
  opsPopularize,
} from "./api";
export default {
  components: { pagination, shareDetail },
  data() {
    return {
      searchForm: {
        search: null,
        timer: [],
        liveState: null,
        popularize: null,
      },

      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      shareVisible: false,
      activeRow: null,
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      const { timer, ...rest } = this.searchForm;
      try {
        const res = await getLiveBroadcastList({
          ...rest,
          ...getTimes(timer, ["startTime", "endTime"]),
          rows: this.rules.limit,
          page: this.rules.page,
        });
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs.searchForm.resetFields();
      this.search();
    },
    newLiveClass() {
      let params = {
        path: "/business/createLiveClass",
      };
      this.$router.push(params, (route) => {
        route.meta.title = "新建直播课";
      });
      this.$router.push("/business/createLiveClass");
      //liveClassDetail
    },
    gotoDetail(row) {
      this.$router.push({
        path: "/business/liveClassDetail",
        query: { roomUid: row.roomUid },
      });
    },
    shareLive(row) {
      this.activeRow = row;
      this.shareVisible = true;
    },
    resetLive(row) {
      let params = {
        path: "/business/createLiveClass",
        query: { ...row },
      };
      this.$router.push(params, (route) => {
        route.meta.title = "修改直播课";
      });
    },
    deteleLive(row) {
      this.$confirm("您确定删除该直播间", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            const res = await delLiveBroadcast({ id: row.id });
            this.$message.success("删除成功");
            this.getList();
          } catch (e) {
            console.log(e);
          }
        })
        .catch(() => {});
    },
    closeRoom(row) {
      this.$confirm("您确定关闭直播间", "提示", {
        confirmButtonText: "确定",
        type: "warning",
      }).then(async (res) => {
        try {
          const res = await closeBroadcastRoomList(row.id);
          this.$message.success("关闭成功");
          this.getList();
        } catch (e) {}
      });
    },
    async popularizeRoom(row) {
      let popularize,str;
      if (row.popularize) {
        popularize = 0;
        str='取消推广'
      } else {
        popularize = 1;
        str='推广'
      }
      this.$confirm(`您是否${str}直播间"${row.roomTitle}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            const res = await opsPopularize({
              popularize,
              id: row.id,
            });
            this.$message.success(`${str}成功`);
            this.getList();
          } catch (e) {
            console.log(e);
          }
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang='scss' scoped>
</style>

