<template>
  <div class="status-label">
    <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("stateLabel.title")}}</div>
      </div>
      <div class="bar d-inline-block float-right">
        <el-button type="primary" size="small" @click="createStatuslabels">{{$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">
      <div class="search mt-2">
        <el-form ref="form" :inline="true" :model="params" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="params.name"
              :placeholder="$t('placeholder.name')"
              @keyup.enter="getStateLabels"
              size="mini"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="getStateLabels">{{$t('btns.query')}}</el-button>
            <el-button size="mini" @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%">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              prop="name"
              :label="$t('stateLabel.name')">
            </el-table-column>
            <el-table-column
              prop="value"
              :label="$t('stateLabel.value')">
            </el-table-column>
            <el-table-column
              prop="description"
              :label="$t('stateLabel.description')">
            </el-table-column>
            <el-table-column
              prop="orderNo"
              :label="$t('stateLabel.ordernumber')">
            </el-table-column>
          </el-table>
        </template>
        <el-pagination
          class="text-right pt-2"
          @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 { getStateLabels, deleteStatuslabel } from "@/service/CommontoolsAPI.js";
import {delMsg} from '@/utils/elementui'
export default {
  name: 'statusLabelList',
  data(){
    return{
      params:{
        appId: "",
        id: "",
        name: "",
        description: "",
        value: "",
        orderNo: "",
        pageNo: 1,
        linesPerPage: 10,
      },
      data: {},
      multipleSelectionId: [],
      multipleSelectionArray: [],
      tableHeight: 100,
    }
  },

  mounted(){
    this.getScollerHeight();
    if(this.$route.query.edit){
      let appId = sessionStorage.getItem("appId");
      let params = {
        ...this.params,
        appId: appId,
      }
      this.params = params;
      this.getStateLabels();
    }
  },

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

    /**
     * 获取状态标签列表
     */
    async getStateLabels(){
      let response = await getStateLabels(this.params);
      if (response.data.errcode == 0) {
        this.data = response.data.data;
      }
    },

    /**
     * 重置查询名称值
     */
    resetName(){
      let params = {
        ...this.params,
        name: "",
      }
      this.params = params;
      this.getStateLabels()
    },

    /**
     * 勾选行时响应
     */
    handleSelectionChange(val) {
      let multipleSelectionId = [];
      if(val.length){
        val.map((e) => {
          multipleSelectionId.push(e.id)
        })
      }
      this.multipleSelectionArray = val;
      this.multipleSelectionId = multipleSelectionId;
    },

    /**
     * 新建角色
     */
    createStatuslabels(){
      this.$router.push({path: '/home/softwaresdetails/commontool/statuslabel', query:{edit: false}});
    },

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

    /**
     * 选中数据源回显
     */
    onRowClick(row){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('label.status_info'), url: "application/application_info_generalTools_stateLabel_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/commontool/statuslabel', query:{edit: true, id: row.id}});
    },

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

    /**
     * 切换分页，每页显示多少条
     */
    handleSizeChange(val){
      let params = {
        ...this.params,
        linesPerPage: val
      }
      this.params = params;
      this.getStateLabels();
    },
  },
  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>