<template>
  <div class="container">
    <!-- 查询配置 -->
    <div style="padding: 0px 20px">
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="工具箱操作日志" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">

          </div>
          <slot>
            <div class="searchFields">
              <el-autocomplete class="inline-input" v-model="searchFields.client" :fetch-suggestions="querySearch"
                placeholder="所在工具箱" :trigger-on-focus="true" @select="handleSelectSClient" @blur="handleSelectSClient">
              </el-autocomplete>
              <Select v-model="searchFields.type" style="width:150px;margin-left:10px;" placeholder="操作类型">
                <Option v-for="item in options" :value="item" :key="item">{{ item }}</Option>
              </Select>
              <DatePicker type="datetimerange" v-model="searchFields.daterange" format="yyyy-MM-dd" placeholder="选择时间范围"
                style="width: 300px;margin-left:10px;" @on-ok="handleDaterange"></DatePicker>
              <Input style="width: 200px; margin-left: 10px" v-model.number="searchFields.Name" placeholder="关键词搜索" />
              <Button type="primary" @click="load" style="margin-left: 10px">查询</Button>
              <Button icon="md-cloud-download" style="margin-left: 10px" type="primary">导出</Button>
            </div>
          </slot>
        </LxHeader>
      </div>

      <div ref="lxTable" class="table-tree">
        <el-table :data="tableData" class="v-table" :height="tableHeight" style="width: 100%">

          <el-table-column type="index" width="50" align="center"></el-table-column>
          <el-table-column :prop="column.field" :align="column.align" v-for="(column,index) in columns"
            :label="column.title" :width="column.width">
            <template slot-scope="scope">
              <div v-if="column.type == 'img'">
                <img v-for="(file, vIndex) in getFilePath( scope.row[column.field], column)" :key="vIndex"
                  @click="viewImg(scope.row, column, file.path)" class="table-img" :src="file.path" />
              </div>
              <div v-else-if="column.type=='format'">

                <div v-if="column.field=='client'">
                  {{scope.row[column.field]?scope.row[column.field].name:""}}
                </div>
              </div>
              <div v-else>{{scope.row[column.field]}}</div>

            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" :current-page="paginations.page"
            :page-size="paginations.page_size" background layout="prev, pager, next" :total="paginations.total">
          </el-pagination>
        </div>
      </div>

    </div>

  </div>
</template>
<script>
  import {
    listToolbox
  } from "../../api/toolbox/list.js"
  import {
    listTouchTypes,
    listclientTouch
  } from "../../api/toolbox/log.js"
  import LxHeader from "@/components/LxHeader/index.vue";
  export default {
    components: {
      LxHeader
    },
    created() {
      this.initLoad();
      this.load();
      this.loadTouchTypes();
    },
    mounted() {},
    data() {
      return {
        options: [],
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          client_id: "",
          keyword: "",
          client: "",
          type: "",
          date_range: "",
          daterange: ""
        },
        paginations: {
          page: 1,
          page_size: 15,
          total: 10
        },
        tableData: [],
        columns: [{
            field: "client",
            title: "工具箱",
            type: "format"
          },
          {
            field: "ip",
            title: "IP",
            type: "string",
            width: 90,
            require: true
          },
          {
            field: "PhoneNo",
            title: "操作时间",
            type: "string",
            link: true,
            width: 100,
            require: true,
          },
          {
            field: "Quantity",
            title: "操作类型",
            type: "int",
            width: 80,
            require: true,
          }
        ],
      };
    },
    methods: {
      handleDaterange() {
        console.log(this.formatdate(this.searchFields.daterange[0]))
        this.searchFields.date_range = this.formatdate(this.searchFields.daterange[0]) + "~" + this.formatdate(this
          .searchFields.daterange[1]);
      },
      formatdate(date) {
        return this.$moment(date).format("YYYY-MM-DD")
      },
      initLoad() {
        var that = this;
        var clientHeight = document.documentElement.clientHeight
        var lxHeader_height = 96.5; //查询 头部
        var paginationHeight = 37; //分页的高度
        var topHeight = 50; //页面 头部
        let tableHeight = clientHeight - lxHeader_height - topHeight - paginationHeight;
        that.tableHeight = tableHeight;
      },
      load() {
        listclientTouch({
          page: this.paginations.page,
          page_size: this.paginations.pageSize,
          client_id: this.searchFields.client_id,
          type: this.searchFields.type,
          date_range: this.searchFields.date_range,
        }).then(res => {
          var data = res.data;
          this.tableData = data;
          this.paginations.total = res.total;
        }).catch(error => {
          //reject(error)
        })
      },
      loadTouchTypes() {
        listTouchTypes().then(res => {
          this.options = res;
        }).catch(error => {
          //reject(error)
        })
      },
      querySearch(queryString, cb) {
        listToolbox().then(response => {
          var data = response.data;
          for (var m of data) {
            m.value = m.name;
          }
          cb(data)
        }).catch(error => {
          //reject(error)
        })
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      handleSelectSClient(item) {
        this.searchFields.client_id = item.id;
      },
    },
  };
</script>
