<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="业务ID" prop="traceId">
        <el-input
          v-model="queryParams.traceId"
          placeholder="请输入业务ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号ID" prop="callbackAccountId">
        <el-input
          v-model="queryParams.callbackAccountId"
          placeholder="请输入账号ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回传事件" prop="conversionType">
        <el-input
          v-model="queryParams.conversionType"
          placeholder="请输入回传事件"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回传状态" prop="callbackStatus">
        <el-select v-model="queryParams.callbackStatus" placeholder="请选择回传状态" clearable size="small">
          <el-option label="待回传" :value="0" />
          <el-option label="成功" :value="1" />
          <el-option label="失败" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="广告商" prop="advertiserId">
        <el-select v-model="queryParams.advertiserId" placeholder="请选择广告商" clearable size="small" filterable>
          <el-option
            v-for="item in advertiserOptions"
            :key="item.id"
            :label="item.advertiserName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="logList">
      <el-table-column label="账号ID" align="center" prop="callbackAccountId" width="120" show-overflow-tooltip />
      <el-table-column label="业务ID" align="center" prop="traceId" min-width="150" show-overflow-tooltip />
      <el-table-column label="系统回传事件" align="center" prop="sysConversionEvent" min-width="150" show-overflow-tooltip />
      <el-table-column label="广告商回传类型" align="center" prop="advConversionEvent" min-width="150" show-overflow-tooltip />
      <el-table-column label="站点ID" align="center" prop="siteId" width="100" />
      <el-table-column label="着陆页" align="center" prop="landingUrl" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link v-if="scope.row.landingUrl" :href="scope.row.landingUrl" target="_blank" type="primary">
            {{ scope.row.landingUrl }}
          </el-link>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="回传时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回传状态" align="center" prop="callbackStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.callbackStatus === 0" type="info">待回传</el-tag>
          <el-tag v-else-if="scope.row.callbackStatus === 1" type="success">成功</el-tag>
          <el-tag v-else-if="scope.row.callbackStatus === 2" type="danger">失败</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { pageConversionLog } from "@/api/adv/conversionLog";
import { pageAdvertiser } from "@/api/adv/advertiser";

export default {
  name: "ConversionLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回传事件表格数据
      logList: [],
      // 广告商选项
      advertiserOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        traceId: undefined,
        callbackAccountId: undefined,
        conversionType: undefined,
        callbackStatus: undefined,
        advertiserId: undefined
      }
    };
  },
  created() {
    // 从 URL 参数中获取 callbackAccountId
    if (this.$route.query.callbackAccountId) {
      this.queryParams.callbackAccountId = this.$route.query.callbackAccountId;
    }
    this.getList();
    this.getAdvertiserOptions();
  },
  methods: {
    /** 查询广告商选项 */
    getAdvertiserOptions() {
      pageAdvertiser({ current: 1, size: 1000 }).then(response => {
        this.advertiserOptions = response.data.records;
      });
    },
    /** 查询回传事件列表 */
    getList() {
      this.loading = true;
      pageConversionLog(this.queryParams).then(response => {
        this.logList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
