<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索框 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="operateJournal-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="操作时间">
                <rangeDate id="operateJournal-rangeDate" ref="rangeDate" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="操作信息">
                <a-input id="operateJournal-actDesc" allowClear v-model.trim="queryParam.actDesc" placeholder="请输入操作信息" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="操作人">
                <a-input id="operateJournal-actorName" allowClear v-model.trim="queryParam.actorName" placeholder="请输入操作人" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="operateJournal-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="operateJournal-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <!-- 列表 -->
    <a-card size="small" :bordered="false">
      <div class="operateJournal">
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px' }"
          size="small"
          rowKey="id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <span slot="actDesc" slot-scope="text, record">{{ record.actDate }}-{{ record.actDesc }}</span>
          <span slot="actorName" slot-scope="text, record">{{ record.actorName }}-{{ record.actorPhone }}</span>
        </s-table>
      </div>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
import getDate from '@/libs/getDate'
import rangeDate from '@/views/common/rangeDate.vue'
// 接口
import { journalList } from '@/api/OperateJournal.js'
export default {
  name: 'PowerOperateJournal',
  mixins: [commonMixin],
  components: { STable, rangeDate },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      // 操作时间
      time: [
        getDate.getThreeday().starttime,
        getDate.getThreeday().endtime
      ],
      queryParam: {
        beginDate: getDate.getThreeday().starttime, // 操作开始时间
        endDate: getDate.getThreeday().endtime, // 操作结束时间
        actDesc: '', // 操作信息
        actorName: ''// 操作人
      },
      dateFormat: 'YYYY-MM-DD', // 操作时间显示格式
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '操作时间', dataIndex: 'actDate', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: 'IP', dataIndex: 'ip', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作人', dataIndex: 'actorName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作信息', dataIndex: 'actDesc', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // 获取列表数据 有分页
        return journalList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 计算表格序号
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          return data
        })
      }
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = getDate.getThreeday().starttime
      this.queryParam.endDate = getDate.getThreeday().endtime
      this.queryParam.actDesc = ''
      this.queryParam.actorName = ''
      this.$refs.table.refresh(true)
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 210
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  }
}
</script>
<style lang="less">
  .operateJournal{
    .sTable{
      margin-top: 0;
    }
  }
</style>
