<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="userLog.search.message" placeholder="原始消息" @keyup.enter.native="searchLogsClickHandler"></el-input>
        </el-col>
        <el-col :span="4">
          <el-button-group>
            <el-button @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" :gutter="10" class="row-bg" justify="center" align="middle">
          <el-col :span="8">
             <el-input v-model="userLog.search.userName" @keyup.enter.native="searchLogsClickHandler" placeholder="用户名"></el-input>
          </el-col>
          <el-col :span="16">
             <el-date-picker id="userLog-date-picker" class="layout-date-picker" v-model="userLog.search.dateInterval" type="datetimerange" placeholder="选择日志时间范围" style="width:100%"></el-date-picker>
          </el-col>
        </el-row>
        <div style="padding:3px"></div>
        <el-row type="flex" :gutter="10" class="row-bg" justify="center">
          <el-col :span="8">
            <el-input v-model="userLog.search.IP" @keyup.enter.native="searchLogsClickHandler" placeholder="IP"></el-input>
          </el-col>
          <el-col :span="8">
            <el-input v-model="userLog.search.message" @keyup.enter.native="searchLogsClickHandler" placeholder="原始消息"></el-input>
          </el-col>
          <el-col :span="8">
            <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-row>
      </template>
    </div>

      <el-list-view ref="dataTable1" :data-read-url="getUserLogsUrl" :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.LogMessageName}}</em></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,
      userLog: {
        search: {
          userName: '',
          dateInterval: [],
          IP: '',
          message: ''
        }
      }
    }
  },
  methods: {
    getUserLogsUrl() {
      const userName = this.userLog.search.userName
      const dateInterval = this.userLog.search.dateInterval
      const IP = this.userLog.search.IP
      const message = this.userLog.search.message
      return [
        '/ui/logs/userlog',
        {
          params: {
            userName,
            startTime: dateInterval[0] ? Date.parse(dateInterval[0]) : null,
            endTime: dateInterval[1] ? Date.parse(dateInterval[1]) : null,
            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>
