<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types.sync="filterTypes" filter-btn-name="Select"
                     @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%;" @row-click="handleExpand" :height="tool.getTableHeight()">
        <el-table-column type="expand" v-if="tableHeaders">
          <template slot-scope="scope">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>请求内容</span>
              </div>
              <template v-if="scope.row.request_content.value">
                <ul class="log-content">
                  <li v-for="(item,key) in JSON.parse(scope.row.request_content.value)" :key="key"><span>{{key}}:</span><strong>{{item}}</strong></li>
                </ul>
              </template>
            </el-card>
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>返回内容</span>
              </div>
              <template v-if="scope.row.response_content.value">
                <ul class="log-content">
                  <li v-for="(item,key) in JSON.parse(scope.row.response_content.value)" :key="key"><span>{{key}}:</span><strong>{{item}}</strong></li>
                </ul>
              </template>
            </el-card>
          </template>
        </el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''" :prop="tableHeader.other.prop ? tableHeader.other.prop : null" :show-overflow-tooltip="tableHeader.other['show-overflow-tooltip'] ? tableHeader.other['show-overflow-tooltip'] : false" :min-width="tableHeader.other['min-width'] ? tableHeader.other['min-width'] : ''">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" >{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import FilterUser from '@/components/FilterUser'
  import Pagination from '@/components/Pagination'
  import permission from '@/utils/permission'
  import filterHelper from '@/utils/filterHelper'

  const UAParser = require('ua-parser-js')

  export default {
    name: 'log_system',
    components: {FilterUser, Pagination},
    mounted() {
      this.getData()
    },
    data() {
      return {
        tableHeaders: null,
        allData: null,
        tableData: null,
        loading: true,
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: null,
        filterModel: {},
      }
    },
    methods: {
      handleExpand(row, event, column) {
        this.$refs.multipleTable.toggleRowExpansion(row)
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        let vueObj = this
        network.getPageData(this, 'log/system', page, pageSize, filterData, function (response) {
          vueObj.filterTypes = response.filterTypes
          vueObj.allData = response
        })
      },
    }
  }

</script>

<style scoped>
  .box-card {
    display: inline-block;
    min-width: 480px;
    margin-right: 20px;
    max-width: 45%;
    vertical-align: top;
  }

  .log-content {
    padding: 0 15px;
    margin: 0;
  }

  .log-content li {
    line-height: 2;
    display: flex
  }

  .log-content span {
    display: inline-block;
    color: #999;
    width: 200px;
    word-break: break-all;
  }

  .log-content strong {
    flex: 1;
    word-break: break-all;
  }
</style>
