<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频名称" prop="videoName">
        <el-input
          v-model="queryParams.videoName"
          placeholder="请输入视频名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面地址" prop="coverUri">
        <el-input
          v-model="queryParams.coverUri"
          placeholder="请输入封面地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="COS地址" prop="videoUrl">
        <el-input
          v-model="queryParams.videoUrl"
          placeholder="请输入COS地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否上传" prop="uploadFlag">
        <el-select v-model="queryParams.uploadFlag" placeholder="请选择是否上传" clearable size="small">
          <el-option
            v-for="dict in uploadFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者Id" prop="createId">
        <el-input
          v-model="queryParams.createId"
          placeholder="请输入创建者Id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源大小 单位M" prop="fileSize">
        <el-input
          v-model="queryParams.fileSize"
          placeholder="请输入资源大小 单位M"
          clearable
          size="small"
          @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="['mingxue:mxappVideo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mingxue:mxappVideo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mingxue:mxappVideo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['mingxue:mxappVideo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mxappVideoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="视频ID" align="center" prop="videoId" />
      <el-table-column label="视频名称" align="center" prop="videoName" />
      <el-table-column label="封面地址" align="center" prop="coverUri" />
      <el-table-column label="COS地址" align="center" prop="videoUrl" />
      <el-table-column label="是否上传" align="center" prop="uploadFlag" :formatter="uploadFlagFormat" />
      <el-table-column label="上传后资源Id" align="center" prop="resourseId" />
      <el-table-column label="是否删除" align="center" prop="delFlag" :formatter="delFlagFormat" />
      <el-table-column label="创建者Id" align="center" prop="createId" />
      <el-table-column label="资源大小 单位M" align="center" prop="fileSize" />
      <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="['mingxue:mxappVideo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mingxue:mxappVideo: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"
    />

    <!-- 添加或修改app视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频名称" prop="videoName">
          <el-input v-model="form.videoName" placeholder="请输入视频名称" />
        </el-form-item>
        <el-form-item label="封面地址" prop="coverUri">
          <el-input v-model="form.coverUri" placeholder="请输入封面地址" />
        </el-form-item>
        <el-form-item label="COS地址" prop="videoUrl">
          <el-input v-model="form.videoUrl" placeholder="请输入COS地址" />
        </el-form-item>
        <el-form-item label="创建者Id" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建者Id" />
        </el-form-item>
        <el-form-item label="资源大小 单位M" prop="fileSize">
          <el-input v-model="form.fileSize" placeholder="请输入资源大小 单位M" />
        </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 { listMxappVideo, getMxappVideo, delMxappVideo, addMxappVideo, updateMxappVideo, exportMxappVideo } from "@/api/mingxue/mxappVideo";

export default {
  name: "MxappVideo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app视频表格数据
      mxappVideoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否上传字典
      uploadFlagOptions: [],
      // 是否删除字典
      delFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        videoName: null,
        coverUri: null,
        videoUrl: null,
        uploadFlag: null,
        createId: null,
        fileSize: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        videoName: [
          { required: true, message: "视频名称不能为空", trigger: "blur" }
        ],
        coverUri: [
          { required: true, message: "封面地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("bool_flag").then(response => {
      this.uploadFlagOptions = response.data;
    });
    this.getDicts("bool_flag").then(response => {
      this.delFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询app视频列表 */
    getList() {
      this.loading = true;
      listMxappVideo(this.queryParams).then(response => {
        this.mxappVideoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否上传字典翻译
    uploadFlagFormat(row, column) {
      return this.selectDictLabel(this.uploadFlagOptions, row.uploadFlag);
    },
    // 是否删除字典翻译
    delFlagFormat(row, column) {
      return this.selectDictLabel(this.delFlagOptions, row.delFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: null,
        videoName: null,
        coverUri: null,
        videoUrl: null,
        uploadFlag: null,
        resourseId: null,
        delFlag: null,
        createId: null,
        fileSize: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.videoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加app视频";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const videoId = row.videoId || this.ids
      getMxappVideo(videoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改app视频";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.videoId != null) {
            updateMxappVideo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMxappVideo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoIds = row.videoId || this.ids;
      this.$confirm('是否确认删除app视频编号为"' + videoIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMxappVideo(videoIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有app视频数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMxappVideo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
