<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="templateLog.search.templateName" 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" class="row-bg" justify="space-around" align="middle">
          <el-col :span="6">
            <el-input v-model="templateLog.search.userName" @keyup.enter.native="searchLogsClickHandler" placeholder="用户名"></el-input>
          </el-col>
          <el-col :span="6">
            <tree-select class="layout-select" clearable :value.sync="templateLog.search.catalogID" :items="catalogTrees" placeholder="栏目名称"></tree-select>
          </el-col>
          <el-col :span="10">
            <el-date-picker id="templateLog-date-picker" class="layout-date-picker" v-model="templateLog.search.dateInterval" type="datetimerange" placeholder="选择日志时间范围" style="width:100%"></el-date-picker>
          </el-col>
        </el-row>
        <div style="padding:3px"></div>
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="6">
            <el-input v-model="templateLog.search.templateType" @keyup.enter.native="searchLogsClickHandler" placeholder="模板类型"></el-input>
          </el-col>
          <el-col :span="6">
            <el-input v-model="templateLog.search.templateName" @keyup.enter.native="searchLogsClickHandler" placeholder="模板名称"></el-input>
          </el-col>
          <el-col :span="10">
            <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="getTemplateLogsUrl" :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="10">
                  <span>用户名：<em>{{item.addUser}}</em></span>
                </el-col>
                <el-col :span="7">
                  <span>栏目名称：<em>{{item.name}}</em></span>
                </el-col>
                <el-col :span="7">
                  <span>模板类型：<em>{{item.templateType}}</em></span>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="10">
                  <span>模板名称：<em>{{item.templateName}}</em></span>
                </el-col>
                <el-col :span="7">
                  <span>操作类型：<em>{{item.actionName}}</em></span>
                </el-col>
                <el-col :span="7">
                  <span>操作结果：<em>{{item.actionResultName}}</em></span>
                </el-col>
              </el-row>
              <el-row>
                <span>操作明细：<em>{{item.actionDetail}}</em></span>
              </el-row>
            </div>
          </div>
        </template>
      </el-list-view>
  </div>
</template>

<script>
import util from '@src/common/util.js'
import TreeSelect from '@src/components/TreeSelect.vue'
export default {
  filters: {
    fixDateTime: function(val) {
      if (!val) {
        return ''
      }
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      isComplexSearch: false,
      catalogTrees: [],
      templateLog: {
        search: {
          userName: '',
          catalogID: '',
          dateInterval: [],
          templateType: '',
          templateName: ''
        },
        tableLog: []
      }
    }
  },
  methods: {
    getTemplateLogsUrl() {
      const userName = this.templateLog.search.userName
      const catalogID = this.templateLog.search.catalogID
      const dateInterval = this.templateLog.search.dateInterval
      const templateType = this.templateLog.search.templateType
      const templateName = this.templateLog.search.templateName
      return [
        '/ui/contentlogs/templateLogs',
        {
          params: {
            userName,
            catalogID,
            startDate: dateInterval[0] ? Date.parse(dateInterval[0]) : null,
            endDate: dateInterval[1] ? Date.parse(dateInterval[1]) : null,
            templateType,
            templateName
          }
        }
      ]
    },
    searchLogsClickHandler() {
      this.$refs.dataTable1.getData(1)
    },
    async initTreeSelect() {
      let editTree = await axios.get('/ui/catalog/bindeditortree')
      this.catalogTrees = editTree.data.data
    }
  },
  created() {
    this.initTreeSelect()
  },
  components: {
    'tree-select': TreeSelect
  }
}
</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>
