<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="链接名称" prop="trackingName">
        <el-input
          v-model="queryParams.trackingName"
          placeholder="请输入链接名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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 label="推广类型" prop="promotionType">
        <el-input
          v-model="queryParams.promotionType"
          placeholder="请输入推广类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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">
      <el-col :span="1.5">
        <el-button
          plain
          type="success"
          icon="el-icon-refresh"
          size="mini"
          @click="getList"
        >刷新</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="linkList">
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="链接名称" align="center" prop="trackingName" show-overflow-tooltip />
      <el-table-column label="广告商" align="center" prop="advertiserName" width="200" show-overflow-tooltip />
      <el-table-column label="推广类型" align="center" prop="promotionType" width="120" />
      <el-table-column label="监测链接" align="center" prop="trackingUrl" show-overflow-tooltip min-width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleCopy(scope.row)"
          >复制链接</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="监测链接详情" :visible.sync="open" width="700px" append-to-body>
      <el-descriptions :column="1" border v-if="detail">
        <el-descriptions-item label="链接ID">{{ detail.id }}</el-descriptions-item>
        <el-descriptions-item label="链接名称">{{ detail.trackingName }}</el-descriptions-item>
        <el-descriptions-item label="广告商">{{ detail.advertiserName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="推广类型">{{ detail.promotionType }}</el-descriptions-item>
        <el-descriptions-item label="监测链接">
          <el-link :href="detail.trackingUrl" target="_blank" type="primary">{{ detail.trackingUrl }}</el-link>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ detail.updateTime }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleCopy(detail)">复制链接</el-button>
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageTrackingLink, getTrackingLink } from "@/api/adv/trackingLink";
import { pageAdvertiser } from "@/api/adv/advertiser";

export default {
  name: "TrackingLink",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测链接表格数据
      linkList: [],
      // 广告商选项
      advertiserOptions: [],
      // 是否显示弹出层
      open: false,
      // 详情数据
      detail: null,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        trackingName: undefined,
        advertiserId: undefined,
        promotionType: undefined
      }
    };
  },
  created() {
    this.getList();
    this.getAdvertiserOptions();
  },
  methods: {
    /** 查询广告商选项 */
    getAdvertiserOptions() {
      pageAdvertiser({ current: 1, size: 1000, enabled: 1 }).then(response => {
        this.advertiserOptions = response.data.records;
      });
    },
    /** 获取广告商名称 */
    getAdvertiserName(advertiserId) {
      const advertiser = this.advertiserOptions.find(item => item.id === advertiserId);
      return advertiser ? advertiser.advertiserName : advertiserId;
    },
    /** 查询监测链接列表 */
    getList() {
      this.loading = true;
      pageTrackingLink(this.queryParams).then(response => {
        this.linkList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看详情按钮操作 */
    handleView(row) {
      this.detail = null;
      this.open = true;
      getTrackingLink(row.id).then(response => {
        this.detail = response.data;
      });
    },
    /** 复制链接按钮操作 */
    handleCopy(row) {
      const url = row.trackingUrl;
      if (!url) {
        this.msgError("链接地址为空");
        return;
      }
      
      // 创建临时input元素
      const input = document.createElement('input');
      input.value = url;
      document.body.appendChild(input);
      input.select();
      
      try {
        // 执行复制
        const successful = document.execCommand('copy');
        if (successful) {
          this.msgSuccess("链接已复制到剪贴板");
        } else {
          this.msgError("复制失败，请手动复制");
        }
      } catch (err) {
        this.msgError("复制失败，请手动复制");
      }
      
      // 移除临时元素
      document.body.removeChild(input);
    }
  }
};
</script>

