<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">

      <el-form-item label="媒体类型" prop="type">
        <el-select v-model="queryParams.type" @change="typeChange" placeholder="请选择媒体类型" clearable size="small">
          <el-option
            v-for="item in typeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="accountId" v-if="queryParams.type != null">
        <el-select v-model="queryParams.accountId" filterable v-loading="accountLoading" clearable size="small">
          <el-option v-for="item in accountList" :value="item.id" :key="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="站点" prop="siteId">
        <el-select v-model="queryParams.siteId" filterable clearable size="small">
          <el-option v-for="item in siteList" :value="item.id" :key="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="广告ID" prop="vid">
        <el-input
          v-model="queryParams.vid"
          placeholder="请输入广告ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传时间">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          value-format="yyyy-MM-dd"
          end-placeholder="结束日期" />
      </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">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['ad:AdUploadLog:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table border v-loading="loading" :data="AdUploadLogList">
      <el-table-column label="媒体类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag prop="type" v-for="(item, index) in typeOptions" v-if="scope.row.type==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="站点" align="center" prop="siteName" />
<!--      <el-table-column label="模板ID" align="center" prop="templateId" />-->
      <el-table-column label="账户" align="center" prop="accountName" />
<!--      <el-table-column label="点击URL" align="center" prop="url">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tooltip class="item" effect="dark" :content="scope.row.url" placement="top">-->
<!--            <a>{{scope.row.url}}</a>-->
<!--          </el-tooltip>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="上传类型" align="center" prop="uploadType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.uploadType.indexOf('ADD_WX') != -1" type="success">加微</el-tag>
          <el-tag v-if="scope.row.uploadType.indexOf('REGISTERED') != -1" type="warning">注册</el-tag>
          <el-tag v-if="scope.row.uploadType.indexOf('FINISH_THE_CLASS') != -1" type="danger">完课</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="广告ID" align="center" prop="vid" />
      <el-table-column label="上传时间" align="center" prop="createTime" />
    </el-table>

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

    <!-- 添加或修改百度回传日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="站点ID" prop="siteId">
          <el-input v-model="form.siteId" placeholder="请输入站点ID" />
        </el-form-item>
        <el-form-item label="媒体类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择媒体类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板ID" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入模板ID" />
        </el-form-item>
        <el-form-item label="账户ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账户ID" />
        </el-form-item>
        <el-form-item label="域名ID" prop="domainId">
          <el-input v-model="form.domainId" placeholder="请输入域名ID" />
        </el-form-item>
        <el-form-item label="点击URL" prop="url">
          <el-input v-model="form.url" placeholder="请输入点击URL" />
        </el-form-item>
        <el-form-item label="上传类型" prop="uploadType">
          <el-select v-model="form.uploadType" placeholder="请选择上传类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="广告唯一ID" prop="vid">
          <el-input v-model="form.vid" placeholder="请输入广告唯一ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdUploadLog, getAdUploadLog, delAdUploadLog, addAdUploadLog, updateAdUploadLog, exportAdUploadLog } from "@/api/ad/AdUploadLog";
import {listAll as accountListAll} from "@/api/ad/adAccount";
import {listAll as siteListAll} from "@/api/ad/adSite";

export default {
  name: "AdUploadLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 百度回传日志表格数据
      AdUploadLogList: [],
      accountList: [],
      siteList: [],
      accountAllList: [],
      accountLoading: false,
      typeOptions: [],
      date: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        type: null,
        templateId: null,
        accountId: null,
        domainId: null,
        url: null,
        uploadType: null,
        vid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    accountListAll().then(e => {
      this.accountAllList = e.data;
    })
    this.loadingSite();
    this.getDicts("ad_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询百度回传日志列表 */
    getList() {
      this.loading = true;
      if(this.date != null && this.date.length > 0){
        this.queryParams.startDate = this.date[0];
        this.queryParams.endDate = this.date[1];
      }
      listAdUploadLog(this.queryParams).then(response => {
        this.AdUploadLogList = response.rows;
        this.AdUploadLogList.forEach(item => {
          item.uploadType = item.uploadType.split(",")
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    copyUrl(url){
      navigator.clipboard.writeText(url)
        .then(() => {
          this.msgSuccess("复制成功");
        })
        .catch(err => {
          this.msgError("复制失败");
        });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        type: null,
        templateId: null,
        accountId: null,
        domainId: null,
        url: null,
        uploadType: null,
        vid: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加百度回传日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdUploadLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改百度回传日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAdUploadLog(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdUploadLog(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除百度回传日志编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAdUploadLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有百度回传日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAdUploadLog(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    typeChange(){
      let type = this.queryParams.type;
      this.queryParams.accountId = undefined;
      this.accountList = this.accountAllList.filter(item => item.type == type);
      this.queryParams.siteId = undefined;
      this.loadingSite();
    },
    loadingSite(){
      siteListAll({type:this.queryParams.type}).then(e => {
        this.siteList = e.data;
      })
    },
  }
};
</script>
