<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('chart.title')}}</div>
      </div>
      <div class="bar d-inline-block float-right">
        <el-button type="primary" size="small" @click="newCharts">{{$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')"
              @keyup.enter.native="getChartsList"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getChartsList" 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.data"
            @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('chart.name')">
            </el-table-column>
            <el-table-column
              prop="note"
              :label="$t('chart.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 { getChartsList, deleteCharts } from "@/service/ModulesApi.js";
import {delMsg} from '@/utils/elementui'
export default {
  name: 'chartslist',
  data(){
    return{
      searchParams: {
        appId: "",
        moduleId: "",
        searchword: "",
        pageNo: 1,
        linesPerPage: 10
      },
      params: {
        id: "",
        name: "",
        type: "",
        note: "",
        scripttext: ""
      },
      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.getChartsList();
  },

  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)
    },

    /**
     * 获取表单列表
     */
    async getChartsList(){
      let response = await getChartsList(this.searchParams)
      if (response.data.errcode == 0) {
        this.data = response.data.data;
      }
    },
    /**
     * 重置查询名称值
     */
    resetName(){
      let searchParams = {
        ...this.searchParams,
        searchword: "",
      }
      this.searchParams = searchParams;
      this.getChartsList();
    },

    /**
     * 勾选行时响应
     */
    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 deleteCharts(multipleSelectionId)
          if (response.data.errcode == 0) {
            this.getChartsList();
            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.getChartsList()
    },

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

    /**
     * 选中数据源回显
     */
    onRowClick(row){
      this.$router.push({path: '/home/softwaresdetails/charts', query:{edit: true, moduleId: this.$route.query.moduleId, id:row.id}})
    },

    /**
     * 新建图表
     */
    newCharts(){
      this.$router.push({path: '/home/softwaresdetails/charts', query:{edit: false, moduleId: this.$route.query.moduleId}})
    }
  }
}
</script>