<template>
  <div id="main" class="h-all">
    <el-container class="h-all">
      <el-header height="auto">
        <!-- 通用工具栏 -->
        <div class="header-button-box">
            <el-button size = 'mini' icon="el-icon-circle-plus-outline" type="primary" @click="createBasicInit"
                       v-if="createAndCopyCommand.visible">{{$t('common.createBtn')}}</el-button>
            <el-button size = 'mini' icon="el-icon-document" type="primary" @click="copyBasicInit"
                       v-if="createAndCopyCommand.visible">{{$t('common.copyBtn')}}</el-button>
            <el-button size = 'mini' icon="el-icon-edit" type="primary" @click="updateBasicInit"
                       v-if="updateServerCommand.visible">{{$t('common.updateBtn')}}</el-button>
            <el-button size = 'mini' icon="el-icon-delete" type="primary" @click="deleteBasicInit"
                       v-if="deleteServerCommand.visible">{{$t('common.deleteBtn')}}</el-button>
        </div>
        <!-- 条件工具栏 -->
        <div class="header-search">
          <div class="search-input-box">
            <label>{{$t('BasicConfigurationManagement.serverType')}}：</label>
            <el-select v-model="SelectServer" size="mini" :placeholder="$t('common.selectOption')" :clearable="true">
              <el-option
                v-for="item in ServerList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="search-input-box">
            <el-button icon="el-icon-search" type="primary" size = 'mini' @click="queryBasicByCondition">{{$t('common.queryBtn')}}</el-button>
          </div>

        </div>
      </el-header>
      <!-- table数据表格 -->
      <el-main class="adapt-height-box">
        <el-table
          :empty-text="$t('common.noData')"
          tooltip-effect="dark"
          stripe
          header-row-class-name="table-header"
          cell-class-name="table-cell"
          class="adapt-height"
          height="auto"
          border
          ref="singleTable"
          :data="basicList"
          highlight-current-row
          @current-change="handleCurrentChange"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="28">
          </el-table-column>

          <el-table-column
            prop="serverKey"
            label="主键"
            v-if="false">
          </el-table-column>
          <el-table-column
            text-align="left"
            prop="serverName"
            :label="$t('BasicConfigurationManagement.serverName')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="serverType"
            :label="$t('BasicConfigurationManagement.serverType')"
            :formatter="formatServerType"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="ipAddress"
            :label="$t('DeviceConfigurationManagement.ipAddress')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="snmpVersion"
            :label="$t('DeviceConfigurationManagement.snmpVersion')"
            :formatter="formatsnmpVersion"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="snmpPort"
            :label="$t('DeviceConfigurationManagement.snmpPort')"
           :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="trapPort"
            :label="$t('DeviceConfigurationManagement.trapPort')"
            :show-overflow-tooltip="true">
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
    <CommandExecuteContext v-bind:commands="commandContext"></CommandExecuteContext>
  </div>
</template>

<script>
  import CommandExecuteContext from '@/commands/CommandExecuteContext'
  import ServerCommands from "@/commands/ServerCommands"
  import ServerService from '@/domain/services/ServerService'
  import HelperUtil from '@/utils/HelperUtil'

  export default {
    name: 'BasicConfigurationManagement',
    components: {
      CommandExecuteContext,
    },

    data: function () {   //存放数据
      return {
        //命令
        commandContext: {},
        //为了创建复制初始化传参
        createAndCopyInitList: [],
        createAndCopyTitle: '',
        //删除相关
        deleteList: [],
        //为修改传参数
        updateInitList: [],
        fullscreenLoading: false,
        formLabelWidth: '120px',

        //服务器类型下拉列表
        ServerList:[
          {
            value: '0',
            label: 'SDNC服务器'
          }, {
            value: '1',
            label: '中心数据库服务器'
          }, {
            value: '2',
            label: '认证服务器'
          }, {
            value: '3',
            label: 'NMS服务器'
          }],
        SelectServer:null,
        value:'',

        SNMPList: [
          {
            key: '0',
            label: 'v1'
          },
          {
            key: '1',
            label: 'v2c'
          },
          {
            key: '3',
            label: 'v3'
          }
        ],

        //双向绑定弹出窗口input域
        form:{
          serverName: "",
          serverType: "",
          ipAddress: "",
          snmpVersion: "",
          snmpPort: "",
          trapPort: ""
        },

        //双向绑定展示数据
        basicList: [],
        //保存当前行数据
        currentRow: null,
        //保存多选行数据
        multipleSelection: [],
      }
    },
    //vue加载初始化函数，加载数据
    created:function(){
      this.getAllBasicInit()
    },
    computed: {
      openTab () {
        return this.$store.state.openTab;
      },
      activeIndex:{
        get () {
          return this.$store.state.activeIndex;
        },
        set (val) {
          this.$store.commit('set_active_index', val);
        }
      },
      //命令
      createAndCopyCommand(){
        return ServerCommands.createAndCopyServerCommand(this.createAndCopyTitle, this.createAndCopyInitList, this.commandContext)
      },
      deleteServerCommand(){
        return ServerCommands.deleteServerCommand(this.deleteList, this.commandContext)
      },
      updateServerCommand(){
        return ServerCommands.updateServerCommand(this.updateInitList, this.commandContext)
      },
    },
    methods: {

      /**
       * @Description  ：单选一行，将所选行进行保存
       * @date         : 2019/02/19 14:32
       */
      handleCurrentChange(val) {
        this.currentRow = val;
      },

      /**
       * @Description  ：表格多选框改变时，将所选行进行保存
       * @date         : 2019/02/19 14:32
       */
      handleSelectionChange (val) {
        this.multipleSelection = val
      },

      /**
       * @Description  :snmpVersionKey，转为显示snmpVersionName
       * @date         : 2019/02/21 19:32
       */
      formatsnmpVersion: function (row, column) {
        for (var j = 0; j < this.SNMPList.length; j++) {
          if (row.snmpVersion == this.SNMPList[j]['key']) {
            return this.SNMPList[j]['label']
          }
        }
      },

      /**
       * @Description  ：过滤locationKey，转为显示lacationName
       * @date         : 2019/02/21 15:33
       */
      formatServerType: function (row, column){
        for(var i= 0;i<this.ServerList.length;i++){
          if(this.ServerList[i]['value']==row.serverType){
            return this.ServerList[i]['label']
            break;
          }
        }
      },

      /**
       * @Description  ：获取全部服务器信息
       * @date         : 2019/02/19 14:32
       */
      getAllBasicInit: function () {
        let _this = this;
        ServerService.getAllServerCollection().then(result => {
          //成功
          _this.basicList = Object.values(result)
        }).catch(err =>{
          //失败
        })
      },

      /**
       * @Description  ：增删改后调用，重新加载table
       * @date         : 2019/02/19 14:32
       */
      getAllBasic: function (vue) {
        let _this = vue;
        ServerService.getAllServerCollection().then(result => {
          //成功
          _this.basicList = Object.values(result)
        }).catch(err =>{
          //失败
        })
      },

      /**
       * @Description  ：创建服务器初始化
       * @date         : 2019/02/19 14:32
       */
      createBasicInit:function () {
        this.createAndCopyTitle =  this.$t('BasicConfigurationManagement.dialogServerCreate');
        this.createAndCopyInitList = {
          "serverKey":"",
          "serverName":"",
          "serverType":"",
          "ipAddress":"",
          "snmpVersion":'1',
          "snmpPort":'161',
          "trapPort":'162',
          "delFlag": '',
          "modifyTime": ""
        };
        this.createAndCopyCommand.execute();
      },

      /**
       * @Description  ：修改服务器信息初始化
       * @date         : 2019/02/19 14:43
       */
      updateBasicInit: function () {
        if(this.multipleSelection.length == 0){
          //请选择一条信息进行修改
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPDATE_SELECT_CODE))
          return;
        }else if(this.multipleSelection.length > 1){
          //请只选择一条信息进行修改
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPDATE_ONLY_ONE_CODE))
          return;
        }else{
          this.currentRow = this.multipleSelection[0];
          this.form.serverName = this.currentRow.serverName;
          this.value = this.currentRow.serverType;
          this.form.ipAddress = this.currentRow.ipAddress;
          this.form.snmpVersion = this.currentRow.snmpVersion;
          this.form.snmpPort = this.currentRow.snmpPort;
          this.form.trapPort = this.currentRow.trapPort;
          this.dialog_basicUpdate = true;

          this.updateInitList = {
            "serverKey":this.multipleSelection[0].serverKey,
            "serverName":this.multipleSelection[0].serverName,
            "serverType":this.multipleSelection[0].serverType,
            "ipAddress":this.multipleSelection[0].ipAddress,
            "snmpVersion":this.multipleSelection[0].snmpVersion,
            "snmpPort":this.multipleSelection[0].snmpPort,
            "trapPort":this.multipleSelection[0].trapPort,
            "delFlag": '',
            "modifyTime": ""
          };
          this.updateServerCommand.execute();
        }
      },


      /**
       * @Description  ：复制服务器信息初始化
       * @date         : 2019/02/19 14:43
       */
      copyBasicInit: function () {
        if(this.multipleSelection.length == 0){
          //请选择一条信息进行复制
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.COPY_SELECT_CODE))
          return;
        }else if(this.multipleSelection.length > 1){
          //请只选择一条信息进行复制
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.COPY_ONLY_ONE_CODE))
          return;
        }else{
          this.createAndCopyTitle = this.$t('BasicConfigurationManagement.dialogServerCopy');
          this.createAndCopyInitList = {
            "serverKey":this.multipleSelection[0].serverKey,
            "serverName":this.multipleSelection[0].serverName,
            "serverType":this.multipleSelection[0].serverType,
            "ipAddress":this.multipleSelection[0].ipAddress,
            "snmpVersion":this.multipleSelection[0].snmpVersion,
            "snmpPort":this.multipleSelection[0].snmpPort,
            "trapPort":this.multipleSelection[0].trapPort,
            "delFlag": '',
            "modifyTime": ""
          };
          this.createAndCopyCommand.execute();
        }
      },

      /**
       * @Description  ：删除服务器信息初始化（支持批量）
       * @date         : 2019/02/19 14:43
       */
      deleteBasicInit:function () {
        if(this.multipleSelection.length == 0){
          //请至少选择一条信息进行删除
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.DELETE_SELECT_CODE))
          return;
        }else{
          this.deleteList = []
          for (var i = 0; i < this.multipleSelection.length; i++) {
            this.deleteList.push(this.multipleSelection[i]['serverKey']);
          }
          this.deleteServerCommand.execute()
        }
      },

      /**
       * @Description  ：条件查询
       * @param        : 列表形式[{查询特征、比较方法（等于）、查询值}]
       * @date         : 2019/02/21 19:28
       */
      queryBasicByCondition: function () {
        if(this.SelectServer == null){
          //所有查询条件为空点查询--》重新刷新表格
          this.$options.methods.getAllBasic(this);
          return;
        }
        var selectList = new Array();
        if(this.SelectServer != null){
          selectList.push({ quaryAttribute:'serverType',
            compareSymbol:"==",
            compareValue:this.SelectServer,})
        }
        let _this = this;
        let loadingInstance = _this.Loading.openLoading();
        //按条件查询
        ServerService.conditionQueryServer(selectList).then(result => {
          //成功
          _this.basicList= Object.values(result);
          _this.Loading.closeLoading(loadingInstance);
          if(_this.basicList.length == 0){
            //查询为空
            _this.InfoTip.warningTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.QUERY_NULL_CODE))
          }else{
            _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.QUERY_CODE));
          }
        }).catch(err => {
          //失败
          _this.Loading.closeLoading(loadingInstance);
          _this.InfoTip.errorTip(_this, err);
        })
      },
    },

    beforeCreate: function () {},

    watch: {
      commandContext(newVal, oldVal){
        if(JSON.stringify(newVal) === '{}'){
          this.SelectServer = null
          this.getAllBasic(this)
          this.$refs.singleTable.clearSelection();
        }
        deep: true
      },

    }
  }
</script>

<style scoped>
</style>
