<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="结算日期">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="执行时间">
        <el-date-picker v-model="daterangeCreateTime1" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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="primary" plain size="mini" @click="handleAdd">开启活动</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="活动日期" align="center" prop="activityDate" />
      <el-table-column label="参与人数" align="center" prop="activityUserSum" />
      <el-table-column label="累计浏览广告数" prop="activityAdSum" align="center" />
      <el-table-column label="邀请好友数" prop="activityReferralSum" align="center" />
      <el-table-column label="开始时间" align="center" prop="activityStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityStartTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算时间" align="center" prop="activityComputeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityComputeTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="activityStatus">
        <template slot-scope="{row}">
          <el-tag v-if="row.activityStatus==0">待开始</el-tag>
          <el-tag type="success" v-if="row.activityStatus==10">进行中</el-tag>
          <el-tag type="warning" v-if="row.activityStatus==20">已结束</el-tag>
          <el-tag type="danger" v-if="row.activityStatus==30">已结算</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="goDetails(scope.row)">查看详情</el-button>
        </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 { shakeActivityPage, shakeActivityCreate } from "@/api/shake.js";
import { getToken } from "@/utils/auth";
export default {
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeCreateTime: [],
      daterangeCreateTime1: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "添加广告",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/demo/demo/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adName: undefined,
        status: undefined,
        locationCode: undefined
      },
      // 表单参数
      form: {
      },
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询测试单表列表 */
    getList () {
      this.loading = true;
      this.queryParams["createTimeStart"] = undefined;
      this.queryParams["createTimeEnd"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["createTimeStart"] = this.daterangeCreateTime[0];
        this.queryParams["createTimeEnd"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeCreateTime1 && '' != this.daterangeCreateTime1) {
        this.queryParams["activityStartTime"] = this.daterangeCreateTime[0];
        this.queryParams["activityEndTime"] = this.daterangeCreateTime[1];
      }
      shakeActivityPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleResourceTip (type) {
      for (let item of this.resourceModelList) {
        if (type == item.value) {
          return item.label || "";
        }
      }
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.adId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 开启活动 */
    handleAdd () {
      this.loading = true;
      shakeActivityCreate().then(res => {
        this.$message.success("开启成功")
      }).finally(() => {
        this.loading = false
      })
    },
    goDetails (row) {
      if (row.activityStatus == 10 || row.activityStatus == 20) {
        this.$router.push({ path: '/shake/activationRecordDetails', query: { id: row.activityId } })
      } else {
        // this.$router.push({ path: '/shake/ranking' })
        this.$router.push({ path: '/shake/activationRecordDetails', query: { id: row.activityId } })
      }
    },
    /** 导入按钮操作 */
    handleImport () {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('demo/demo/export', {
        ...this.queryParams
      }, `demo_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
<style lang="scss">
.el-drawer__body {
  padding: 0 20px !important;
}
</style>
<style lang="scss" scoped>
.flex {
  display: flex;
}
.demo-drawer__footer {
  margin-left: 120px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: #fff;
  color: rgb(96, 98, 102);
}
</style>
