<template>
  <div class="obpm-common-tool">
    <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('library.stylelibs')}}</div>
      </div>
      <div class="bar d-inline-block float-right">
        <el-button type="primary" size="small" @click="addDataSource">{{$t('btns.new')}}</el-button>
        <el-button type="danger" plain size="small" @click="remove">{{$t('btns.delete')}}</el-button>
      </div>
    </div>
    <div class="obpm-content mt-2">
      <div class="search">
        <el-form ref="form" :inline="true" :model="formInline" class="demo-form-inline" size="mini" @submit.native.prevent>
          <el-form-item>
            <el-input v-model="formInline.inputName"
              :placeholder="$t('placeholder.name')"
              @keyup.enter.native="selectInputName"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="selectInputName" type="primary" plain>{{$t('btns.query')}}</el-button>
            <el-button @click="resetApplictions">{{$t('btns.reset')}}</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="obpm-content-table">
        <template>
          <el-table
          ref="multipleTable"
          :data="tableData.datas"
          tooltip-effect="dark"
          style="width: 100%"
          :height="tableHeight"
          @selection-change="handleSelectionChange"
          @row-click="rowClick"
          size="small"
          >
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="name"
            :label="$t('library.name')"
            min-width="50%">
          </el-table-column>
          <el-table-column
            prop="content"
            show-overflow-tooltip
            :label="$t('library.content')"
            min-width="50%">
          </el-table-column>
        </el-table>
        </template>
        <el-pagination
          class="text-right pt-2"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="tableData.pageNo"
          :page-sizes="[10, 25, 50, 100]"
          :page-size="10"
          background
          layout="total, prev, pager, next, sizes"
          :total="tableData.rowCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { getStyleLibsList,deleteStyleLibsList } from "@/service/LibraryAPI.js";
import {delMsg} from '@/utils/elementui'
export default {
  name: "MacroLibsList",
  data () {
    return {
      formInline: {
        inputName: "",        //名称
      },
      tableHeight: "100",
      tableData: {},
      params: {
        id: "",
        searchword: "",
        currpage: 1,
        pagelines: 10
      },
      multipleSelection: [],   //将获取选中的数组放进去
    }
  },
  mounted(){
    this.getScollerHeight();
    this.getStyleLibstions();
  },
  methods:{
    /**
     * 动态计算页面table高度
     */
    getScollerHeight() {
      setTimeout(() => {
        let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        //60 头部， 32 面包屑， 41 第一层页签， 40 第二层页签， 50 列表标题， 71 查询， 20 父组件外边距, 38分页
        this.tableHeight = (clientHeight - 60 - 32 - 41 - 40 - 50 - 71 - 20 - 38) + 'px'
      }, 100)
    },
    /**
     *  获取样式库列表的数据
     */
    async getStyleLibstions(searchword = ""){
      let params = {
        ...this.params,
        searchword,
      }
      this.params = params;
      let appId = sessionStorage.getItem("appId");
      const response = await getStyleLibsList(params, appId)
      if (response.data.errcode == 0) {
        this.tableData = response.data.data;
      }
    },
    /**
     * 当前页
     */
    handleCurrentChange(val) {
      let params = {
        ...this.params,
        currpage: val,
        searchword: this.formInline.inputName
      }
      this.params = params;
      this.getStyleLibstions(this.formInline.inputName);
    },

    /**
     * 切换分页，每页显示多少条
     */
    handleSizeChange(val){
      let params = {
        ...this.params,
        pagelines: val
      }
      this.params = params;
      this.getStyleLibstions();
    },
    /**
     * 点击列表某一列进去编辑
     */
    rowClick(row){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('msg.style_info'), url: "application/application_info_libraries_styleLibs_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/Library/StyleLibs',query:{edit: true, styleId: row.id}});
    },
    /**
     * selectInputName
     * 名称查询
     */
    selectInputName(){
      this.getStyleLibstions(this.formInline.inputName);
      
    },
    /**
     * 重置查询
     */
    resetApplictions(){
      this.formInline.inputName='';
      this.selectInputName();
    },
    /**
     * 获取选中的表格数据
     */
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    /**
     * 新建样式库详情
     */
    addDataSource(){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('msg.style_info'), url: "application/application_info_libraries_styleLibs_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/Library/StyleLibs',query:{edit: false}});
    },
    /**
     * 移除
     */
    async remove(){
      let selectData = this.multipleSelection;
      let data = [];
      for(let i=0;i<selectData.length;i++){
        data.push(selectData[i].id);
      }
      if(data.length > 0){
        let appId = sessionStorage.getItem("appId");
        try{
          await delMsg(this.$t('msg.delConfirm'), this.$t('msg.tip'))
          const response = await deleteStyleLibsList(appId, data)
          if (response.data.errcode == 0) {
            this.$message.success(this.$t('msg.remove_ss'));
            this.getStyleLibstions();
          }
        }catch(e){
          console.log(e)
        }
      }else{
        this.$message.error(this.$t('msg.check_data'))
      }
    },
  },
  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>