<template>
  <div class="asset" ref="asset">
    <div class="main-list" :class="{'main-list-with-sub': bottomBox.showSubList}">
      <div class="main-modal"></div>
      <div class="top-tools" v-show="bottomBox.mainResizeShow">
        <div class="top-tool-main-right" :class="{'top-tool-main-right-to-left': bottomBox.showSubList}">
          <div class="top-tool-search margin-r-20"><search-input :searchMsg="searchMsg" @search="search" :inTransform="bottomBox.inTransform"></search-input></div>
          <export-excel
            export-file-name="asset"
            export-url="/asset/export"
            import-url="/asset/import"
            :params="searchLabel"
            @afterImport="getTableData"
          >
            <template slot="optionZone">
              <button @click.stop="add" :title="$t('overall.createAsset')" class="nz-btn nz-btn-size-normal nz-btn-style-light" id="asset-create-asset">
                <i class="nz-icon nz-icon-create-square"></i></button>
            </template>
          </export-excel>
        </div>
        <div class="pagination-top pagination-top-hide display-none"></div>
      </div>
      <!-- 自定义table列 -->
      <transition name="el-zoom-in-top">
        <element-set
          v-if="tools.showCustomTableTitle"
          @close="tools.showCustomTableTitle = false"
          :custom-table-title.sync="tools.customTableTitle"
          :original-table-title="tableTitle"
          ref="customTableTitle"
        ></element-set>
      </transition>
      <el-table
        class="nz-table"
        :height="mainTableHeight"
        style="width: 100%;"
        :data="tableData"
        v-scrollBar:el-table="'large'"
        v-show="bottomBox.mainResizeShow"
        border
        v-loading="tools.loading"
        tooltip-effect="light"
        ref="assetTable"
        :cell-class-name="messageStyle"
        @sort-change="tableDataSort"
      >
        <el-table-column
          :resizable="false"
          v-for="(item, index) in tools.customTableTitle"
          v-if="item.show"
          :width="item.width"
          :key="`col_${index}`"
          :label="item.label"
          :fixed="item.fixed"
          :show-overflow-tooltip="item.prop != 'Alert' || item.prop != 'Module'"
          min-width="110px"
          :class-name="item.prop == 'option' ? 'content-right-options' : ''"
          :sortable="$tableSet.sortableShow(item.prop,'asset')"
          :prop="$tableSet.propTitle(item.prop,'asset')"
          :sort-orders="['ascending', 'descending']"
        >
          <template slot-scope="scope" :column="item">
            <div v-if="item.prop=='ID'">
              <span>{{scope.row.id}}</span>
            </div>
            <div v-if="item.prop=='assetType'">
              <span>{{scope.row.model.type.value}}</span>
            </div>
            <template v-if="item.prop=='SN'">{{scope.row.sn}}</template>
            <div v-if="item.prop=='HOST'">
              <span>{{scope.row.host}}</span>
            </div>
            <div v-if="item.prop=='state'">
              <span>{{scope.row.state==1 ? $t('asset.inStock') : $t('asset.notInStock')}}</span>
            </div>
          <div v-if="item.prop == 'pingStatus'">
            <el-popover
              placement="right"
              width="200"
              trigger="hover"
              :content="formatPingTime(scope.row.pingLastUpdate)">
              <div slot="reference" style="width: 20px">
                <div :class="{'active-icon green':scope.row.pingState == 1,'active-icon red':scope.row.pingState == 0}"></div><span>{{scope.row.pingRtt?scope.row.pingRtt+'ms':''}}</span>
              </div>
            </el-popover>
          </div>
          <template v-if="item.prop=='Module'">
            <span class="link" @click="showEndpoint(scope.row)">{{scope.row.endpointNum}}</span>
          </template>
          <template v-if="item.prop=='Alert'">
            <el-tooltip :content="scope.row.alertNum+' '+$t('overall.active')" placement="top" effect="light" :disabled=" scope.row.alertNum < 99">
              <span :id="'asset-alerts-'+scope.row.id" @click="jumpToAlertMsg(scope.row)" class="link">
                {{(scope.row.alertNum < 99 ? scope.row.alertNum : 99)}}
                  <sup class="linkSup" v-if="scope.row.alertNum > 99">+</sup>
                {{' ' + $t('overall.active')}}
              </span>
            </el-tooltip>
          </template>
          <div v-if="item.prop=='dataCenter'">
            <span  >{{scope.row.idc.name}}</span>
          </div>
          <template v-if="item.prop=='cabinet'">
            <span v-if="scope.row.cabinet && scope.row.cabinet != '--'">{{returnData(scope.row.cabinet).name}}</span>
            <span v-else >{{returnData(scope.row.cabinet)}}</span>
          </template>
          <div v-if="item.prop=='model'">
            <span>{{scope.row.model.name}}</span>
          </div>
          <div v-if="item.prop=='vendor'">
            <span>{{scope.row.model.vendor.value}}</span>
          </div>
          <div v-if="item.prop=='purchaseDate'">
            <span>{{returnData(scope.row.purchaseDate)}}</span>
          </div>
          <div v-if="item.prop=='principal'">
            <span>{{getPrincipalName(scope.row.idc.principal)}}</span>
          </div>
          <div v-if="item.prop=='tel'">
            <span>{{scope.row.idc.tel}}</span>
          </div>
          <template v-if="item.prop == 'option'">
            <span :title="$t('overall.view')" @click.stop="detail(scope.row)" class="content-right-option" :id="'asset-detail-'+scope.row.id"><i
              class="el-icon-view"></i>
            </span>
              &nbsp;
              <span :title="$t('overall.edit')" @click.stop="edit(scope.row)" class="content-right-option" :id="'asset-edit-'+scope.row.id"><i
                class="nz-icon nz-icon-edit"></i>
            </span>
              &nbsp;
              <span :title="$t('overall.delete')" @click.stop="del(scope.row)" class="content-right-option" :id="'asset-del-'+scope.row.id"><i
                class="el-icon-delete"></i>
            </span>
              &nbsp;
              <el-dropdown trigger="hover" @command="cli">
              <span title="CLI" class="content-right-option" :id="'asset-ssh-'+scope.row.id"><i
                class="nz-icon nz-icon-cli" :class="{'gray-filter': !scope.row.accounts || scope.row.accounts.length == 0}"></i>
              </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(account, index) in scope.row.accounts" v-if="account && account.protocol != 'SNMP'" :key="index" :command="[scope.row.id ,scope.row.host, account]">{{account.protocol}}</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>

            </template>
          </template>
        </el-table-column>
        <el-table-column width="28" fixed="right" :resizable="false">
          <template slot="header" slot-scope="scope">
          <span @mousedown.stop="!tools.showCustomTableTitle && (tools.showCustomTableTitle = true)" class="nz-table-gear">
            <i class="nz-icon nz-icon-gear"></i>
          </span>
          </template>
          <template v-slot="scope">
            <button v-if="scope.$index == 0" class="to-top" :style="{top: tools.toTopBtnTop}" :class="{'to-top-is-hover': tools.tableHover}" v-show="tools.showTopBtn && bottomBox.mainResizeShow" @click="$toTop('ps', 0)"><i class="nz-icon nz-icon-top"></i></button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-bottom" v-show="!bottomBox.showSubList">
        <Pagination :tableId="tableId" :pageObj="pageObj" @pageNo='pageNo' @pageSize='pageSize' ref="Pagination"></Pagination>
      </div>
    </div>
    <transition name="el-zoom-in-bottom">
      <bottom-box v-if="bottomBox.showSubList" :sub-resize-show="bottomBox.subResizeShow" :obj="bottomBox.asset" :isFullScreen="bottomBox.isFullScreen" :from="'asset'" :targetTab.sync="bottomBox.targetTab" :detail="bottomBox.assetDetail"
                  @reload="getTableData"
                  @closeSubList="bottomBox.showSubList = false"
                  @fullScreen="fullScreen"
                  @exitFullScreen="exitFullScreen"
                  @listResize="listResize" ></bottom-box>
    </transition>
    <transition name="right-box">
      <asset-box v-if="rightBox.show" :asset="asset" @refresh="flushData" ref="assetBox" @close="closeRightBox"></asset-box>
    </transition>
  </div>
</template>

<script>
  import bus from "../../../libs/bus";
  import exportXLSX from "../../common/exportXLSX";
  import endpointStatusPop from "./endpointStatusPop";
  export default {
    name: "asset",
    components:{
      'export-excel':exportXLSX,
      'endpoint-status-pop':endpointStatusPop,
    },
    data() {
      return {
        //侧滑
        rightBox: {
          show: false,
        },
        /*二级页面相关*/
        bottomBox: {
          assetDetail: {}, //asset详情
          asset: {}, //告警信息对应的asset对象
          mainResizeShow: true, //dom高度改变时是否展示|隐藏
          subResizeShow: true,
          isFullScreen: false, //全屏状态
          showSubList: false, //是否显示二级列表
          targetTab: '', //显示二级列表中的哪个页签
          inTransform: false, //搜索框相关，搜索条件下拉框是否在transform里
        },

        mainTableHeight: this.$tableHeight.normal, //主列表table高度

        /*工具参数*/
        tools: {
          loading: false, //是否显示table加载动画
          toTopBtnTop: this.$tableHeight.toTopBtnTop, //to-top按钮的top属性
          tableHover: false, //控制滚动条和top按钮同时出现
          showTopBtn: false, //显示To top按钮
          showCustomTableTitle: false, //自定义列弹框是否显示
          customTableTitle: [], //自定义列工具的数据
        },
        asset: {},
        blankAsset: {
          id: '',
          sn: '',
          host: '',
          state: '',
          purchaseDate: '',
          idcId: '',
          cabinetId: '',
          modelId: '',
          model: {type: {code: ""}},
          assetType: '',
          impi:{
            host:'',
            port:'',
          },
          tags: [],
          accounts: [],
        },
        tableId: 'assetTable', //需要分页的table的id，用于记录每页数量
        searchMsg: { //给搜索框子组件传递的信息
          zheze_none: true,
          searchLabelList: [{
            id: 1,
            name: 'ID',
            type: 'input',
            label: 'id',
            disabled: false
          },{
            id: 20,
            name: 'SN',
            type: 'input',
            label: 'sn',
            disabled: false
          },{
            id: 21,
            name: 'Host',
            type: 'input',
            label: 'host',
            disabled: false
          },{
            id: 22,
            name: this.$t("asset.state"),
            type: 'select',
            label: 'assetState',
            disabled: false
          },{
            id: 23,
            name: "pingStatus",
            type: 'select',
            label: 'pingStatus',
            disabled: false
          }],
        },

        searchLabel: {}, //搜索参数
        activeType: 'dataCenter',

        tableTitle: [
          {
            label: this.$t("asset.tableTitle.host"),
            prop: 'HOST',
            show: true,
            width: 130,
            fixed:'left'
          },
          {
            label: this.$t("asset.tableTitle.id"),
            prop: 'ID',
            show: false,
            allowed:true,
            // width: 110
          }, {
            label: this.$t("asset.tableTitle.assetType"),
            prop: 'assetType',
            show: true,
          }, {
            label: this.$t("asset.tableTitle.device"),
            prop: 'SN',
            show: true,
            width: 130
          },  {
            label: this.$t("asset.tableTitle.assetState"),
            prop: 'state',
            show: true,
          }, {
            label:this.$t('asset.tableTitle.assetPing'),
            prop:'pingStatus',
            show:true,
          },{
            label: this.$t("asset.tableTitle.modules"),
            prop: 'Module',
            show: true,
            width: 105
          }, {
            label: this.$t("asset.tableTitle.alerts"),
            prop: 'Alert',
            show: true,
            width: 100
          }, {
            label: this.$t("asset.tableTitle.dataCenter"),
            prop: 'dataCenter',
            show: true,
          }, {
            label: this.$t("asset.tableTitle.cabinet"),
            prop: 'cabinet',
            show: true,
          }, {
            label: this.$t("asset.tableTitle.model"),
            prop: 'model',
            show: true,
          }, {
            label: this.$t("asset.tableTitle.vendor"),
            prop: 'vendor',
            show: true,
          }, {
            label: this.$t("asset.tableTitle.procurementDate"),
            prop: 'purchaseDate',
            show: false,
          }, {
            label: this.$t("asset.tableTitle.principal"),
            prop: 'principal',
            show: false,
          }, {
            label: this.$t("asset.tableTitle.principalTel"),
            prop: 'tel',
            show: false,
            width: 115
          }, {
            label: this.$t('config.account.option'),
            prop: 'option',
            show: true,
            width: 150,
            fixed:'right'
          }],
        tableData: [],

        pageObj: {
          id: '',
          pageNo: 1,
          pageSize: 50,
          total: 0,
          idcId: '',
          idcIds: ''
        },

        assetPingSwitch: localStorage.getItem('nz-sys-asset-ping-switch'),
      }
    },
    watch: {
      'bottomBox.showSubList': function(n) {
        let vm = this;
        this.$bottomBoxWindow.showSubListWatch(vm, n);
      },
      'bottomBox.asset': {
        deep: true,
        handler(n) {
          if (n.id) {
            this.bottomBox.assetDetail = this.convertToDetail(n);
            if (n.model.id) {
              n.modelId = n.model.id;
            }
          }
        }
      },
    },
    methods: {
      fullScreen() {
        let vm = this;
        this.$bottomBoxWindow.fullScreen(vm);
      },
      exitFullScreen() {
        let vm = this;
        this.$bottomBoxWindow.exitFullScreen(vm);
      },
      listResize(e) {
        let vm = this;
        this.$bottomBoxWindow.listResize(vm, e);
      },
      cli([id,host,account]){
        const consoleParam = {
          id: id,
          host: host,
          accountId: account.id,
          port: account.port,
        };
        this.$store.commit('addConsole', consoleParam);
      },
      jumpToAlertMsg(asset) {
        this.bottomBox.targetTab = 'alertMessage';
        this.bottomBox.asset = JSON.parse(JSON.stringify(asset));
        this.bottomBox.showSubList = true;
      },
      getTableData() {
        this.searchLabel = Object.assign(this.searchLabel, this.pageObj);
        this.tools.loading = true;
        this.$get('asset', this.searchLabel).then(response => {
          this.tools.loading = false;
          if (response.code === 200) {
            this.tableData = response.data.list;
            this.pageObj.total = response.data.total
          }
        });
      },
      getUserData() {
        return new Promise(resolve => {
          this.$get('sys/user/list').then(response => {
            if (response.code === 200) {
              this.userData = response.data.list;
            }
            resolve(this.userData);
          });
        });
      },
      messageStyle(e) {
        if (e.column.label == 'Alerts' || e.column.label == this.$t("asset.tableTitle.alerts")) {
          if (e.row.alertNum > 0) {
            return 'danger';
          } else {
            return 'success';
          }
        }
        return '';
      },
      edit(asset) {
        this.asset = JSON.parse(JSON.stringify(asset));
        this.rightBox.show = true;
      },
      detail(asset) {
        this.bottomBox.asset = JSON.parse(JSON.stringify(asset));
        this.bottomBox.targetTab = "panel";
        this.bottomBox.showSubList = true;
      },
      del(asset) {
        this.$confirm(this.$t("tip.confirmDelete"), {
          confirmButtonText: this.$t("tip.yes"),
          cancelButtonText: this.$t("tip.no"),
          type: 'warning'
        }).then(() => {
          this.$delete("asset?ids=" + asset.id).then(response => {
            if (response.code === 200) {
              this.$message({duration: 2000, type: 'success', message: this.$t("tip.deleteSuccess")});
              this.getTableData();
              bus.$emit('asset-list-change')
            } else {
              this.$message.error(response.msg);
            }
          })
        });
      },
      markOptionData(data) {
        data.forEach(item => {
          this.$set(item, 'isEdit', false);
          this.$set(item, 'oldName', item.value);
          this.$set(item, item.name, false)
          this.$set(item, item.id, false)
          this.$set(item, item.id + item.name, false)
        })
      },
      showEndpoint(asset) {
        this.bottomBox.asset = Object.assign({}, asset);
        this.bottomBox.targetTab = "endpoint";
        this.bottomBox.showSubList = true;
      },
      add() {
        this.asset = this.newAsset();
        this.rightBox.show = true;
      },
      newAsset() {
        return JSON.parse(JSON.stringify(this.blankAsset));
      },
      closeRightBox(refresh) {
        this.rightBox.show = false;
        if (refresh) {
          this.getTableData();
          bus.$emit('asset-list-change')
        }
      },
      pageNo(val) {
        this.pageObj.pageNo = val;
        this.getTableData()
      },
      pageSize(val) {
        this.pageObj.pageSize = val;
        localStorage.setItem('nz-pageSize-' + localStorage.getItem('nz-username') + '-' + this.tableId, val);
        this.getTableData();
      },
      getPrincipalName(data) {
        for (let item in this.userData) {
          if (this.userData[item].userId === data) {
            return this.userData[item].username
          }
        }
      },
      returnData(data) {
        if (data && data !== '--') {
          return data;
        } else if (data === '--') {
          return '--';
        } else {
          return "--";
        }
      },
      flushData() {
        Promise.all([this.getLeftMenuList()]).then(response => {
          this.getTableData();
        });
      },
      closeAllPop() {
        this.$refs.idcConfigBox.forEach((item) => {
          item.show(false);
        });
        this.$refs.cabinetConfigBox.forEach((item) => {
          item.show(false);
        });
      },
      search(searchObj) {
        let orderBy = '';
        if(this.searchLabel.orderBy){
          orderBy = this.searchLabel.orderBy
        }
        this.searchLabel = {};
        this.pageObj.pageNo = 1;
        for (let item in searchObj) {
          if (searchObj[item]) {
            this.$set(this.searchLabel, item, searchObj[item]);
          }
        }
        if(orderBy){
          this.$set(this.searchLabel, 'orderBy', orderBy);
        }
        this.getTableData();
      },
      convertToDetail(obj) {
        let detail = JSON.parse(JSON.stringify(obj));
        detail.state = obj.state == 1 ? this.$t('asset.inStock'): this.$t('asset.notInStock');
        detail.assetType = obj.model.type.value;
        detail.vendorModel = obj.model.vendor.value + " " + obj.model.name;
        detail.dataCenter = obj.idc ? obj.idc.name : "";
        detail.location = obj.idc ? obj.idc.location : "";
        detail.principal = obj.idc ? this.getPrincipalName(obj.idc.principal) : "";
        detail.tel = obj.idc ? obj.idc.tel : "";
        detail.cabinet = obj.cabinet ? obj.cabinet.name : "";
        detail.uSize = obj.cabinet ? obj.cabinet.uSize : "";
        detail.remark = obj.idc ? obj.idc.remark : "";
        if (obj.accounts && obj.accounts.length > 0) {
          let account = obj.accounts[0];
          detail.protocol = account.protocol;
          detail.account = account.user;
          let loginType = "";
          if (account.authType == 1) {
            loginType = this.$t('asset.password');
          } else if (account.authType == 2) {
            loginType = this.$t('asset.ssh');
          }
          detail.loginType = loginType;
          detail.port = account.port;
        }

        return detail;
      },
      formatPingTime:function(str){
        if(!str || str == ''){
          return this.$t('asset.assetStatPre')+this.$t('asset.assetStatDown');
        }
        let ds='-';
        let ts=':';
        let time=new Date(str);
        let year=time.getFullYear();
        let month=time.getMonth()+1 > 9 ? time.getMonth()+1 : ('0'+(time.getMonth()+1));
        let day=time.getDate() > 9 ? time.getDate():('0'+time.getDate());
        let hours=time.getHours() > 9? time.getHours():('0'+time.getHours());
        let minutes=time.getMinutes() > 9 ?time.getMinutes():('0'+time.getMinutes());
        let seconds=time.getSeconds() > 9 ? time.getSeconds():('0'+time.getSeconds());
        return this.$t('asset.assetStatPre')+year+ds+month+ds+day+' '+hours+ts+minutes+ts+seconds;

      },
      // 数据排序
      tableDataSort(item){
        let orderBy='';
        if(item.order==='ascending'){
          orderBy=item.prop;
        }
        if(item.order==='descending'){
          orderBy='-'+item.prop;
        }
        this.$set(this.searchLabel, "orderBy", orderBy);
        this.getTableData();
      },
      // 获取左侧菜单数据
      getLeftMenuList(){
        return new Promise(resolve => {
          this.$get('asset/filter').then(response => {
            if (response.code === 200) {
              //dc
              this.dcData = response.data.dc;
              // AssetType
              this.assetTypeData = response.data.assetType;
              // vendor
              this.vendorData = response.data.vendor;
              // ping
              this.pingData = response.data.ping.map(item => {
                item.label = item.name;
                item.value = item.status;
                return item;
              });
            }
            resolve();
          });
        });
      },
      initEvent() {
        bus.$on("asset-filter-change", (column, content) => {
          this.pageObj[column] = content;
          this.getTableData();
        });
      }
    },

    mounted() {
      //初始化表头
      let tableTitle = localStorage.getItem("nz-tableTitle-" + localStorage.getItem("nz-username") + "-" + this.$route.path)
        ? JSON.parse(localStorage.getItem("nz-tableTitle-" + localStorage.getItem("nz-username") + "-" + this.$route.path))
        : this.tableTitle;
      if (this.assetPingSwitch == 'off') {
        for (let i in tableTitle) {
          let label = tableTitle[i];
          if (label.prop == 'pingStatus') {
            tableTitle.splice(i, 1);
            break;
          }
        }
      }
      this.tools.customTableTitle = tableTitle;
      //初始化数据
      this.getUserData();
      this.getTableData();
      //是否存在分页缓存
      let pageSize = localStorage.getItem('nz-pageSize-' + localStorage.getItem('nz-username') + '-' + this.tableId);
      if (pageSize != 'undefined' && pageSize != null) {
        this.pageObj.pageSize = pageSize
      }
      this.initEvent();
      this.$nextTick(() => {
        //绑定滚动条事件，控制top按钮
        let el = this.$refs.assetTable.$el.querySelector(".el-table__body-wrapper");
        if (el._ps_) {
          el.addEventListener("ps-scroll-y", () => {
            if (el._ps_.scrollbarYTop > 50) {
              this.tools.showTopBtn = true;
            } else {
              this.tools.showTopBtn = false;
            }
          });
          el.addEventListener("mouseenter", () => {
            this.tools.tableHover = true;
          });
          el.addEventListener("mouseleave", () => {
            this.tools.tableHover = false;
          });
        }
        //resize时刷新左侧列表滚动条
        let vm = this;
        window.onresize = function () {
          vm.$refs.leftScrollbar.update();
          el._ps_.update();
        };
      });
    },
    destroyed() {
      window.onresize = null;
    }
  }
</script>

<style scoped lang="scss">
  .asset {
    height: 100%;
  }
  /deep/ .el-table .cell.el-tooltip{
    min-width: 88px;
  }
  .linkSup{
    position: absolute;
    top: -15px;
    left: 17px;
  }
  .link{
    position: relative;
  }

  .mark{
    vertical-align: middle;
    display: inline-flex;
  }
  /*/deep/ .sidebar-info-item .el-checkbox__label{*/
    /*padding-right: 15px;*/
    /*width: calc(100% - 32px);*/
  /*}*/
</style>
