<template>
  <div class="listPage H100">
    <div class="search-container">
      <el-form :inline="true" :model="form" class="search-form" onsubmit="return false;">
        <el-form-item label="">
          <el-input
            placeholder="请输入视频名称"
            v-model="form.nameOrCode"
            @keyup.enter.native="Search"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="">
            <el-select
              placeholder="请选择视频版权方"
              @focus="getAssetCopyrightData"
              v-model="form.videoContentCopyrightOwnerId"
              clearable
            >
              <el-option
                v-for="item in copyrightOwner"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select
              placeholder="请选择预设视频分类"
              @focus="getAssetTypeData"
              v-model="form.videoContentCatId"
              clearable
            >
              <el-option
                v-for="item in videoContentCat"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        <el-form-item>
          <div class="btn-group">
            <el-button size="mini" type="primary" class="btn_form_search" @click="Search">查询</el-button>
            <el-button size="mini" class="btn_form_search" @click="Reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="page-tip">
        <span class="page-tip-title">页面说明:</span>
        <span class="page-tips">可使用版权方、视频分类、视频名称模糊搜索对需要汇出的视频进行快速检索，选定视频后将视频汇出到指定U盘。</span>
      </div>
    </div>
    <div class="table-content">
      <div class="btn-group">
        <el-button type="primary" @click="multipleRemit()">汇出</el-button>
      </div>
      <div class="party-table">
        <el-table
          border
          style="width: 100%;height:100%"
          height="100%"
          ref="multipleTable"
          :data="tableData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
           <el-table-column type="index" width="120" label="序号">
            <template slot-scope="scope">
              <span>{{ (page.currentPage - 1) * 10 + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="视频名称" prop="fileName"></el-table-column>
          <el-table-column label="版权方" prop="videoContentCopyrightOwnerName"></el-table-column>
          <el-table-column label="视频类别" prop="videoContentCatName"></el-table-column>
        </el-table>
      </div>
      <div class="partyt-pagination">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          layout="prev, pager, next, jumper"
          :total="page.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    var checkIsNull = (rule, value, callback) => {
      if (value) {
        if (value.trim() == "") {
          callback(new Error("不允许只输入空格"));
        } else {
          callback();
        }
      }
    };
    return {
      copyrightOwner:[],
      videoContentCat:[],
      type: true,
      value1: "",
      page: { currentPage: 1, pageSize: 10, total: 0 },
      tableData: [],
      FormVisible: false,
      FormVisible1: false,
      formLabelWidth: "100px",
      form: {
        nameOrCode: ""
      },
      editform: {
        menuName: "",
        menuUrl: "",
        parentId: "",
        sort: "",
        id: ""
      },
      classForm: {
        menuName: "",
        menuUrl: "",
        parentId: "",
        sort: ""
      },
      typeList: [],
      rules: {
        sort: [
          { required: true, message: "请输入顺序值", trigger: "change" },
          { pattern: /^(\d{1,5})?$/, message: "排序值为低于5位数的纯数字" }
        ],
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "change" },
          { max: 20, message: "不能超过20个字符", trigger: "change" },
          { validator: checkIsNull, trigger: "blur" }
        ],
        menuUrl: [
          { required: true, message: "请输入菜单地址", trigger: "change" },
          { validator: checkIsNull, trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "请输入父级菜单", trigger: "change" }
        ]
      },
      value: "",
      name: ""
    };
  },
  computed: {},
  mounted() {
    this.onSearch();
    // this.getPermis();
  },
  components: {},
  methods: {
       // 获取展板分类列表
    getAssetCopyrightData(){
       let vm = this;
      vm.$https({
        url: "copyrightOwner/getList",
        method: "get",
        authType: this.backToken
      },
      {copyrightOwnerType:'VIDEO_CONTENT'})
        .then(res => {
          let data = res.data.data;
          this.copyrightOwner = data;
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    // 获取视频分类列表
    getAssetTypeData() {
      let vm = this;
      vm.$https({
        url: "videoContentCat/getList",
        method: "get",
        authType: this.backToken
      })
        .then(res => {
          let data = res.data.data;
          this.videoContentCat = data;
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    // 渲染父级权限
    getPermis() {
      let vm = this;
      vm.$https({
        method: "get",
        url: "menu/getRoleMenu?roleId=" + sessionStorage.getItem("roleId"),
        authType: this.backToken
      }).then(
        res => {
          let data = res.data;
          vm.typeList = data.map((e, i) => {
            return {
              id: e.id,
              name:
                e.menuName.indexOf("-") == -1
                  ? e.menuName
                  : e.menuName.split("-")[1]
            };
          });
        },
        error => {
          console.log(error);
        }
      );
    },
    // 获得数据接口
    getTableData(param) {
      let vm = this;
      vm.$https(
        {
          url: "asset/getPageList",
          method: "post",
          authType: this.backToken
        },
        vm.$qs.stringify(param)
        // param
      )
        .then(res => {
          let data = res.data.data;
          vm.page.pageSize = data.size;
          vm.page.total = data.total;
          vm.tableData = data.records;
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    // 分页
    handleCurrentChange(val) {
      let _this = this;
      _this.page.currentPage = val;
      _this.onSearch();
    },
    onSearch() {
      let _this = this;
      let param = _this.getSearchQuery();
      _this.getTableData(param);
    },
    // 重置
    Reset(){
      this.form = {}
      this.Search()
    },
    Search() {
      let _this = this;
      _this.page.currentPage = 1;
      let searchObj = {
        _index: 1,
        _size: _this.page.pageSize,
        nameOrCode: _this.form.nameOrCode,
        videoContentCatId:_this.form.videoContentCatId,
        videoContentCopyrightOwnerId:_this.form.videoContentCopyrightOwnerId
      };
      this.getTableData(searchObj);
    },
    // // 获取当前查询参数
    getSearchQuery() {
      let _this = this;
      let searchObj = {
        _index: _this.page.currentPage,
        _size: _this.page.pageSize
      };
      for (let key in _this.form) {
        if (_this.form[key]) {
          searchObj[key] = _this.form[key];
        }
      }
      return searchObj;
    },

    // 批量操作
    handleSelectionChange(selection) {
      let _this = this;
      _this.selection = selection;
    },
    getMultipleSelect() {
      let _this = this;
      let arr = [];
      let str = "";
      if (_this.selection) {
        _this.selection.forEach(function(e) {
          arr.push(e.id);
        });
        str = arr.join(",");
        // str = arr;
      } else {
        str = "";
        // str = [];
      }
      return str;
    },
    // 汇出
    multipleRemit() {
      let _this = this;
      let str = _this.getMultipleSelect();
      //   console.log(str)
      if (str.length > 0) {
        console.log(str);
        _this.$https({
            url: "asset/download",
            method: "post",
            authType: this.backToken
          },
          // {idList:str}
          _this.$qs.stringify({idList:str})
          )
          .then(
            res => {
                const blob = new Blob([res.data], {
            type:
              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'"
          })
          const downloadElement = document.createElement('a')
          const contentDisposition = res.headers['content-disposition']
          const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
          const result = patt.exec(contentDisposition)
          const filename = decodeURI(result[1])
          const href = window.URL.createObjectURL(blob) // 创建下载的链接
          downloadElement.href = href
          downloadElement.download = filename // 下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() // 点击下载
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放blob对象 
            },
            error => {}
          );
      } else {
        this.$message({
          type: "info",
          message: "请至少选择一个选项！"
        });
      }

      //   if (str) {
      //     this.$confirm("此操作将删除选中菜单, 是否继续?", "提示", {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       type: "warning"
      //     }).then(() => {
      //       _this
      //         .$https({
      //           url: "menu/batchDel?menuIds=" + str,
      //           method: "DELETE",
      //           authType: this.backToken
      //         })
      //         .then(
      //           res => {
      //             if (res.data.status == 201 || res.data.status == 200) {
      //               this.$message({
      //                 type: "success",
      //                 message: "删除成功!"
      //               });
      //             }
      //             //重新查询数据
      //             _this.onSearch();
      //           },
      //           error => {}
      //         );
      //     });
      //   } else {
      //     this.$message({
      //       type: "info",
      //       message: "请至少选择一个选项！"
      //     });
      //   }
    }
  }
};
</script>

<style lang="less">
// @import "../../../../style/common";
// @import "../../style/list";
@import "../../../../style/dialog.less";
@import "../../../../style/table.less";
@import "../../../../style/pagination.less";
</style>
