<template>
  <div class="app-container-push-log">
    <div class="filter-container">
      <!--
      <el-select
        v-model="listQuery.pushBusinessScene"
        placeholder="业务类型"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in messageBusinessScene"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        />
      </el-select> -->
      <el-select
        v-model="listQuery.pushStatus"
        :placeholder="$t('pages.messagePushLog.filter.pushStatus')"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in pushStatus"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        />
      </el-select>

      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.messagePushLog.filter.system')"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in sysOriginPlatforms"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-select
        v-model="listQuery.platform"
        :placeholder="$t('pages.messagePushLog.filter.mobileSystem')"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in appPlatforms"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        />
      </el-select>
      <el-select
        v-model="listQuery.deviceType"
        :placeholder="$t('pages.messagePushLog.filter.pushPlatform')"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in deviceTypes"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        />
      </el-select>

      <div class="filter-item">
        <el-date-picker
          v-model="rangeDate"
          value-format="timestamp"
          type="datetimerange"
          :picker-options="pickerOptions"
          :range-separator="$t('pages.messagePushLog.dateRange.separator')"
          :start-placeholder="$t('pages.messagePushLog.dateRange.start')"
          :end-placeholder="$t('pages.messagePushLog.dateRange.end')"
        />
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('pages.messagePushLog.filter.search') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
    >
      <el-table-column prop="sendUserName" :label="$t('pages.messagePushLog.table.sender')" align="center">
        <template slot-scope="scope">
          {{ scope.row.sendUserName || $t('pages.messagePushLog.word.system') }}
        </template>
      </el-table-column>
      <el-table-column prop="sysOrigin" :label="$t('pages.messagePushLog.table.system')" align="center">
        <template slot-scope="scope">
          <sys-origin-icon
            :icon="scope.row.sysOrigin"
            :desc="scope.row.sysOrigin"
          />
        </template>
      </el-table-column>
      <el-table-column prop="platform" :label="$t('pages.messagePushLog.table.platform')" align="center" />
      <el-table-column prop="deviceType" :label="$t('pages.messagePushLog.table.pushPlatform')" align="center" />
      <el-table-column prop="businessScene" :label="$t('pages.messagePushLog.table.businessScene')" align="center" />
      <!-- <el-table-column prop="pushUserType" label="用户类型" align="center" /> -->
      <el-table-column prop="title" :label="$t('pages.messagePushLog.table.title')" align="center" />
      <el-table-column prop="content" :label="$t('pages.messagePushLog.table.content')" align="center" />
      <el-table-column prop="pushStatus" :label="$t('pages.messagePushLog.table.status')" align="center" />
      <el-table-column prop="successQuantity" :label="$t('pages.messagePushLog.table.successQuantity')" align="center" />
      <el-table-column prop="failQuantity" :label="$t('pages.messagePushLog.table.failQuantity')" align="center" />
      <el-table-column prop="createTime" :label="$t('pages.messagePushLog.table.createdAt')" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />
  </div>
</template>

<script>
import { messageBusinessScene, userTypes } from '@/constant/type'
import {
  appPlatforms,
  deviceTypes,
  sysOriginPlatforms
} from '@/constant/origin'
import { pushLogTable } from '@/api/message'
import Pagination from '@/components/Pagination'
import { pickerOptions } from '@/constant/el-const'

export default {
  components: { Pagination },
  data() {
    return {
      pickerOptions,
      appPlatforms,
      deviceTypes,
      sysOriginPlatforms,
      messageBusinessScene,
      userTypes,
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        pushType: '',
        pushBusinessScene: '',
        pushUserType: '',
        pushStatus: '',
        startTime: '',
        endTime: '',
        sysOrigin: '',
        platform: '',
        deviceType: ''
      },
      rangeDate: '',
      listLoading: true
    }
  },
  computed: {
    pushStatus() {
      return [
        { value: 0, name: this.$t('pages.messagePushLog.status.success') },
        { value: 1, name: this.$t('pages.messagePushLog.status.fail') }
      ]
    }
  },
  watch: {
    rangeDate: {
      immediate: true,
      deep: true,
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.listQuery.startTime = newVal[0]
          this.listQuery.endTime = newVal[1]
          return
        }
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    this.renderData()
  },
  methods: {
    renderData() {
      const that = this
      that.listLoading = true
      pushLogTable(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData()
    },
    queryUserDetails(row) {
      this.userDeatilsDrawer = true
      this.thatSelectedUserId = row.id
    }
  }
}
</script>
