<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('multilanguage.title')}}</div>
      </div>
      <div class="bar d-inline-block float-right">
        <el-button size="small" @click="reloadLanguage">{{$t('btns.reload_properties')}}</el-button>
        <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 label="">
            <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.data"
          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="label"
            :label="$t('multilanguage.label')"
            min-width="30%">
          </el-table-column>
          <el-table-column
            prop="text"
            :label="$t('multilanguage.text')"
            min-width="40%">
          </el-table-column>
          <el-table-column
            prop="language"
            :label="$t('multilanguage.type')"
            min-width="40%">
          </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 { getMultiLanguageList,deleteMultiLanguageList,reloadMultiLanguage } from "@/service/MultiLanguageAPI.js";
import {delMsg} from '@/utils/elementui'
export default {
  name: "MacroLibsList",
  data () {
    return {
      formInline: {
        inputName: "",        //名称
      },
      tableHeight: "100",
      tableData: {},
      parmas: {
        id: "",
        searchword: "",
        currpage: "",
        pagelines: ""
      },
      multipleSelection: [],   //将获取选中的数组放进去
    }
  },
  mounted(){
    this.getScollerHeight();
    this.getMultiLanguagetions();
  },
  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 - 50 - 71 - 20 - 38) + 'px'
      }, 100)
    },
    /**
     *  获取多语言列表的数据
     */
    async getMultiLanguagetions(searchword,pagelines, currpage = 1){
      let params = {
        ...this.params,
        label: searchword ? searchword : "",
        text:'',
        type:'',
        currpage,
        pagelines: pagelines ? pagelines : "",
      }
      this.params = params;
      let appId = sessionStorage.getItem("appId");
      const response = await getMultiLanguageList(params, appId)
      if (response.data.errcode == 0) {
        let data = response.data.data;
        if(data.data == null){
          data.data = []
        }
        for(let i=0;i<data.data.length;i++){
          if(data.data[i].type == 1){
            data.data[i].language = "English";
          }else if(data.data[i].type == 2){
            data.data[i].language = "简体中文";
          }else{
            data.data[i].language = "繁體中文";
          }
        }
        this.tableData = response.data.data;
      }
    },
    /**
     * 当前页
     */
    handleCurrentChange(val) {
      let params = {
        ...this.params,
        currpage: val
      }
      this.params = params;
      this.getMultiLanguagetions(this.formInline.inputName, this.params.pagelines, val);
    },

    /**
     * 切换分页，每页显示多少条
     */
    handleSizeChange(val){
      let params = {
        ...this.params,
        pagelines: val
      }
      this.params = params;
      this.getMultiLanguagetions("",val);
    },
    /**
     * 点击列表某一列进去编辑
     */
    rowClick(row){
      this.$store.dispatch("rightHelpPageParams", { title: "多语言信息", url: "domain/domain_multiLanguages_info_help.html" });
      let params = {
        appId: sessionStorage.getItem("appId"),
        languageId: row.id
      }
      this.$router.push({path: '/home/softwaresdetails/MultiLanguage',query:{edit: true, params: params}});
    },
    /**
     * selectInputName
     * 名称查询
     */
    selectInputName(){
      this.getMultiLanguagetions(this.formInline.inputName);
      
    },
    /**
     * 重置查询
     */
    resetApplictions(){
      this.formInline.inputName='';
      this.selectInputName();
    },
    /**
     * 获取选中的表格数据
     */
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    /**
     * 重新加载多语言
     */
    async reloadLanguage(){
      const response = await reloadMultiLanguage()
      if (response.data.errcode == 0) {
        this.$message.success(response.data.data);
        this.getMultiLanguagetions();
      }
    },
    /**
     * 新建多语言详情
     */
    addDataSource(){
      this.$store.dispatch("rightHelpPageParams", { title: "多语言信息", url: "domain/domain_multiLanguages_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/MultiLanguage',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){
        try{
          await delMsg(this.$t('msg.delConfirm'), this.$t('msg.tip'))
          const response = await deleteMultiLanguageList(data)
          if (response.data.errcode == 0) {
            this.$message.success(this.$t('msg.remove_ss'));
            this.getMultiLanguagetions();
          }
        }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>