<template>
  <div class="layout-no-cols">
    <div class="tar">
      <el-row type="flex" class="row-bg" justify="end" v-if="!isComplexSearch">
        <el-col :span="6" >
          <el-input v-model="sqlLog.search.message" placeholder="原始消息" @keyup.enter.native="searchLogsClickHandler"></el-input>
        </el-col>
        <el-col class="search-btn-box" :span="5">
          <el-button-group>
            <el-button type="primary" @click="searchLogsClickHandler">
              <i class="fa fa-search"></i>搜索
            </el-button>
            <el-button @click="isComplexSearch=!isComplexSearch">高级搜索 ...</el-button>
          </el-button-group>
        </el-col>
      </el-row>
      <template v-else>
        <el-row type="flex" class="row-bg" justify="space-around" align="middle">
          <el-col :span="7">
            <el-select class="layout-select" v-model="sqlLog.search.subType" placeholder="请选择类型" @change="searchLogsClickHandler">
              <el-option label="所有类型" value=""></el-option>
              <el-option label="执行速度较慢的SQL" value="Slow"></el-option>
              <el-option label="SQL错误信息" value="Error"></el-option>
              <el-option label="SQL执行超时" value="Timeout"></el-option>
            </el-select>
          </el-col>
          <el-col :span="7">
            <el-input v-model="sqlLog.search.userName" @keyup.enter.native="searchLogsClickHandler" placeholder="用户名"></el-input>
          </el-col>
          <el-col :span="7">
            <el-input v-model="sqlLog.search.IP" @keyup.enter.native="searchLogsClickHandler" placeholder="IP地址"></el-input>
          </el-col>
        </el-row>
        <div style="padding:3px"></div>
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="7">
            <el-input v-model="sqlLog.search.message" @keyup.enter.native="searchLogsClickHandler" placeholder="原始消息"></el-input>
          </el-col>
          <el-col :span="7">
            <el-button-group style="float:left">
              <el-button  type="primary" @click="searchLogsClickHandler">
                <i class="fa fa-search"></i>高级搜索
              </el-button>
              <el-button @click="isComplexSearch=!isComplexSearch">搜索 ...</el-button>
            </el-button-group>
          </el-col>
          <el-col :span="7"></el-col>
        </el-row>
      </template>
    </div>

      <el-list-view ref="dataTable1" :data-read-url="getSqlLogsUrl" :pageSize="10">
        <template slot-scope="item">
          <div class="panel-card">
            <div class="card-header">
              <el-row>
                <el-col :span="24">
                  <span class="f12"><i class="fa fa-clock-o"></i>{{item.addTime | fixDateTime}}</span>
                </el-col>
              </el-row>
            </div>
          <div class="card-content">
            <el-row>
              <el-col :span="8">
                <span>用户名：<em>{{item.userName}}</em></span>
              </el-col>
              <el-col :span="8">
                <span>IP地址：<em>{{item.IP}}</em></span>
              </el-col>
              <el-col :span="8">
                <span>耗时：<em>{{item.CostTime}}</em>ms</span>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <span>原始消息：<em>{{item.logMessage}}</em></span>
              </el-col>
            </el-row>
          </div>
          </div>
        </template>
      </el-list-view>
  </div>
</template>

<script>
import util from '@src/common/util.js'
export default {
  filters: {
    fixDateTime: function(val) {
      if (!val) {
        return ''
      }
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      isComplexSearch: false,
      sqlLog: {
        search: {
          subType: '',
          userName: '',
          IP: '',
          message: ''
        }
      }
    }
  },
  methods: {
    getSqlLogsUrl() {
      const subType = this.sqlLog.search.subType
      const userName = this.sqlLog.search.userName
      const IP = this.sqlLog.search.IP
      const message = this.sqlLog.search.message
      return [
        '/ui/logs/sqllog',
        {
          params: {
            subType,
            userName,
            IP,
            message
          }
        }
      ]
    },
    searchLogsClickHandler() {
      this.$refs.dataTable1.getData(1)
    }
  }
}
</script>

<style scoped>
  .panel-card {
    padding: 0px;
    margin: 10px auto 20px;
  }

  .panel-card .card-header{
    background-color: #fafafa;
    padding: 10px 15px;
  }

  .card-content{
    padding: 10px 15px;
  }
</style>
