<template>
  <div class="app-container">
    <p class="tip-line">
      与后台 <code>[goofish-order-event]</code> INFO、表 <code>erp_goofish_order_event_log</code> 一致；可按订单号/来源/说明关键词跨单排查。
      来源 <code>JD_LOGISTICS_PUSH</code>：京东物流扫描服务（写 Redis、企微货主推送）触发闲鱼同步前的轨迹；可与 <code>REDIS_WAYBILL</code>（写入本地运单）、<code>AUTO_SHIP</code> 连起来看。
    </p>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="96px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="模糊" clearable style="width: 168px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="orderId" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="erp_goofish_order.id" clearable style="width: 130px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="AppKey" prop="appKey">
        <el-input v-model="queryParams.appKey" placeholder="精确" clearable style="width: 140px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="事件类型" prop="eventType">
        <el-select v-model="queryParams.eventType" placeholder="全部" clearable style="width: 140px">
          <el-option label="ORDER_SYNC" value="ORDER_SYNC" />
          <el-option label="LOGISTICS_SYNC" value="LOGISTICS_SYNC" />
          <el-option label="SHIP" value="SHIP" />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input v-model="queryParams.source" placeholder="JD_LOGISTICS_PUSH、REDIS…" clearable style="width: 148px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="说明关键字" prop="messageKeyword">
        <el-input v-model="queryParams.messageKeyword" placeholder="模糊匹配 message" clearable style="width: 180px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始"
          end-placeholder="结束"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" border size="small">
      <el-table-column label="ID" prop="id" width="72" />
      <el-table-column label="时间" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="eventType" width="120" align="center">
        <template slot-scope="scope">
          <el-tag :type="eventLogTagType(scope.row.eventType)" size="mini">{{ scope.row.eventType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="orderId" prop="orderId" width="88" />
      <el-table-column label="订单号" prop="orderNo" min-width="140" show-overflow-tooltip />
      <el-table-column label="AppKey" prop="appKey" min-width="120" show-overflow-tooltip />
      <el-table-column label="来源" prop="source" width="120" show-overflow-tooltip />
      <el-table-column label="说明" prop="message" min-width="260" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listGoofishOrderEventLogPage } from '@/api/jarvis/goofish'

export default {
  name: 'ErpGoofishEventLog',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      list: [],
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderId: undefined,
        orderNo: undefined,
        appKey: undefined,
        eventType: undefined,
        source: undefined,
        messageKeyword: undefined
      }
    }
  },
  created() {
    const q = this.$route.query || {}
    if (q.orderNo) {
      this.queryParams.orderNo = q.orderNo
    }
    if (q.orderId) {
      this.queryParams.orderId = q.orderId
    }
    this.getList()
  },
  methods: {
    eventLogTagType(t) {
      if (t === 'ORDER_SYNC') return ''
      if (t === 'LOGISTICS_SYNC') return 'warning'
      if (t === 'SHIP') return 'success'
      return 'info'
    },
    getList() {
      this.loading = true
      const q = { ...this.queryParams, params: {} }
      if (this.queryParams.orderId !== undefined && this.queryParams.orderId !== null && this.queryParams.orderId !== '') {
        const n = parseInt(String(this.queryParams.orderId).trim(), 10)
        q.orderId = Number.isFinite(n) ? n : undefined
      } else {
        q.orderId = undefined
      }
      if (this.dateRange && this.dateRange.length === 2) {
        q.params = { beginTime: this.dateRange[0], endTime: this.dateRange[1] }
      }
      listGoofishOrderEventLogPage(q).then(res => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        orderId: undefined,
        orderNo: undefined,
        appKey: undefined,
        eventType: undefined,
        source: undefined,
        messageKeyword: undefined
      }
      this.resetForm('queryForm')
      this.getList()
    }
  }
}
</script>

<style scoped>
.tip-line {
  margin: 0 0 12px 0;
  font-size: 13px;
  color: #606266;
  line-height: 1.5;
}
.tip-line code {
  background: #f4f4f5;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 12px;
}
</style>
