<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="直播名称" prop="liveName">
        <el-input
          v-model="queryParams.liveName"
          placeholder="请输入直播名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="直播状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择直播状态" clearable size="small">
          <el-option label="待直播" value="1" />
          <el-option label="直播中" value="2" />
          <el-option label="已结束" value="3" />
          <el-option label="直播回放中" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="直播类型" prop="liveType">
        <el-select v-model="queryParams.liveType" placeholder="请选择直播类型" clearable size="small">
          <el-option label="直播" value="1" />
          <el-option label="录播" value="2" />
          <el-option label="直播回放" value="3" />
        </el-select>
      </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="['live:live:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="直播名称" align="center" prop="liveName" :show-overflow-tooltip="true" />
      <el-table-column label="直播类型" align="center" prop="liveType" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.liveType == 1">直播</span>
          <span v-else-if="scope.row.liveType == 2">录播</span>
          <span v-else-if="scope.row.liveType == 3">直播回放</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="直播状态" align="center" prop="liveStatus" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.liveStatus == 2 ? 'success' : scope.row.liveStatus == 3 ? 'info' : 'warning'">
            {{ scope.row.liveStatus == 1 ? '待直播' : scope.row.liveStatus == 2 ? '直播中' : scope.row.liveStatus == 3 ? '已结束' : scope.row.liveStatus == 4 ? '回放中' : '-' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['live:live:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['live:live:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="直播名称" prop="liveName">
          <el-input v-model="form.liveName" placeholder="请输入直播名称" />
        </el-form-item>
        <el-form-item label="直播类型" prop="liveType">
          <el-select v-model="form.liveType" placeholder="请选择直播类型">
            <el-option label="直播" :value="1" />
            <el-option label="录播" :value="2" />
            <el-option label="直播回放" :value="3" />
          </el-select>
        </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>
  </div>
</template>

<script>
import { getRequest, postRequest, putRequest, delRequest } from "@/api/common";

export default {
  name: "LiveLiveList",
  data() {
    return {
      loading: false,
      ids: [],
      multiple: true,
      showSearch: true,
      total: 0,
      list: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        liveName: null,
        status: null,
        liveType: null
      },
      form: {},
      rules: {
        liveName: [{ required: true, message: "直播名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRequest("/live/live/list", this.queryParams).then(response => {
        this.list = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      }).catch(() => {
        this.list = [];
        this.total = 0;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = { id: null, liveName: null, liveType: 1 };
      this.resetForm("form");
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增直播";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRequest("/live/live/" + id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            putRequest("/live/live", this.form).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            postRequest("/live/live", this.form).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的直播?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delRequest("/live/live/" + ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
