<template>
  <div>
    <div class="obpm-title">
      <div class="d-inline-block">
        <i class="el-icon-folder title-icon"></i>
        <div class="title-name d-inline-block ml-2">{{$t('tabs.report')}}</div>
      </div>
      <div class="bar d-inline-block float-right">
        <el-button type="primary" size="small" @click="createReports">{{$t("btns.new")}}</el-button>
        <el-button type="danger" plain size="small" @click="showDeleteConfirm">{{$t("btns.delete")}}</el-button>
      </div>
    </div>
    <div class="obpm-content mt-2">
      <div class="search">
        <el-form ref="form" :inline="true" :model="searchParams" class="demo-form-inline" size="mini" @submit.native.prevent>
          <el-form-item >
            <el-input v-model="searchParams.searchword"
              :placeholder="$t('placeholder.name')"
              @change="getReportsList"
              @keyup.enter.native="getReportsList"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getReportsList" type="primary" plain>{{$t("btns.query")}}</el-button>
            <el-button @click="resetName">{{$t("btns.reset")}}</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="obpm-content-table">
        <template>
          <el-table
            ref="tableList"
            :data="data.datas"
            @selection-change="handleSelectionChange"
            @row-click="onRowClick"
            :height="tableHeight"
            style="width: 100%"
            size='small'>
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              prop="name"
              :label="$t('label.name')">
            </el-table-column>
            <el-table-column
              prop="description"
              :label="$t('label.description')">
            </el-table-column>
          </el-table>
        </template>
        <el-pagination
          class="text-right pt-1"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="data.pageNo"
          :page-sizes="[10, 25, 50, 100]"
          :page-size="10"
          background
          layout="total, prev, pager, next, sizes"
          :total="data.rowCount">
        </el-pagination>
      </div>
    </div>
    
  </div>
</template>
<script>
import { getReportsList, deleteReports } from "@/service/ModulesApi.js";
import {delMsg} from '@/utils/elementui'
export default {
  name: 'reportslist',
  data(){
    return{
      searchParams: {
        appId: "",
        moduleId: "",
        searchword: "",
        pageNo: 1,
        linesPerPage: 10
      },
      data: {},
      multipleSelectionId: [],
      multipleSelectionArray: [],
      tableHeight: 100,
    }
  },

  mounted(){
    this.getScollerHeight();
    let appId = sessionStorage.getItem("appId");
    let moduleId = this.$route.query.moduleId;
    let searchParams = {
      ...this.searchParams,
      appId: appId,
      moduleId: moduleId
    }
    this.searchParams = searchParams;
    this.getReportsList();
  },

  methods:{
    
    /**
     * 动态计算页面table高度
     */
    getScollerHeight() {
      setTimeout(() => {
        let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        //60 头部， 32 面包屑， 41 第一层页签， 50 列表标题， 71 查询， 20 父组件外边距, 40分页
        this.tableHeight = (clientHeight - 60 - 32 - 41 - 50 - 71 - 20 - 40) + 'px'
      }, 100)
    },

    /**
     * 获取表单列表
     */
    getReportsList(){
      getReportsList(this.searchParams, {
        onSucess: response => {
          if (response.data.errcode == 0) {
            this.data = response.data.data;
          }
        }
      })
    },
    /**
     * 重置查询名称值
     */
    resetName(){
      let searchParams = {
        ...this.searchParams,
        searchword: "",
      }
      this.searchParams = searchParams;
      this.getReportsList();
    },
    /**
     * 勾选行时响应
     */
    handleSelectionChange(val) {
      let multipleSelectionId = [];
      if(val.length){
        val.map((e) => {
          multipleSelectionId.push(e.id)
        })
      }
      this.multipleSelectionArray = val;
      this.multipleSelectionId = multipleSelectionId;
    },

     /**
     * 删除角色
     */
    async showDeleteConfirm(){
      let multipleSelectionId = this.multipleSelectionId;
      if(multipleSelectionId.length > 0){
        try{
          await delMsg(this.$t('msg.delConfirm'), this.$t('msg.tip'))
          const response = await deleteReports(multipleSelectionId);
          if (response.data.errcode == 0) {
            this.getReportsList();
            this.$message.success(this.$t('msg.del_ss'));
          }
        }catch(e){
          console.log(e)
        }
      }else{
        this.$message.error(this.$t('msg.check_data'))
      }

    },

    /**
     * 当前页
     */
    handleCurrentChange(val) {
      let searchParams = {
        ...this.searchParams,
        pageNo: val
      }
      this.searchParams = searchParams;
      this.getReportsList()
    },

    /**
     * 切换分页，每页显示多少条
     */
    handleSizeChange(val){
      let searchParams = {
        ...this.searchParams,
        linesPerPage: val
      }
      this.searchParams = searchParams;
      this.getReportsList()
    },

    /**
     * 选中数据源回显
     */
    onRowClick(row){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('view.print_info'), url: "application/application_module_print_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/reports', query:{edit: true, moduleId: this.$route.query.moduleId, reportId: row.id}});
    },

    /**
     * 创建报表
     */
    createReports(){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('view.print_info'), url: "application/application_module_print_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/reports', query:{edit: false, moduleId: this.$route.query.moduleId}});
    },

  },
  watch: {
    "$store.state.rightHelpPageParams": {
      handler() {
        if (this.$store.state.rightHelpFrame) {
          this.$store.state.rightHelpFrame.contentWindow.showHelpContentHtml(this.$store.state.rightHelpPageParams.title, this.$store.state.rightHelpPageParams.url);
        }
      },
      deep: true,
    },
  },
}
</script>