<!--  -->
<template>
  <div class="m-container">
    <el-page-header
      @back="onCancel"
      :content="year === '2021'? '2021双十一活动' : '2022双十一活动'"
    ></el-page-header>
    <!-- <h2>
      <div class="squrt"></div>
      2021十一会员活动
    </h2> -->
    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="organId">
          <el-select
            class="multiple"
            style="width: 180px !important"
            v-model.trim="searchForm.organId"
            filterable
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item, index) in selects.branchs"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onCreateQRCode"
            >H5统计链接</el-button
          >
        </el-form-item>
        <el-form-item v-if="year === '2022'">
          <el-button type="primary" @click="onCreateQRCode2"
            >杭州H5统计链接</el-button
          >
        </el-form-item>
        <el-form-item>
          <auth auths="export/organDoubleEleven2021Statis">
            <el-button type="primary" @click="onExport">导出</el-button>
          </auth>
        </el-form-item>
        <el-form-item>
          <auth auths="export/doubleEleven2021OrderDetail">
            <el-button type="primary" @click="onExport2">学员明细导出</el-button>
          </auth>
        </el-form-item>
      </save-form>
      <!-- <div style="font-size: 14px; color: #f85043; padding-bottom: 10px"> -->
      <!-- 应收总金额：{{ totalExpectAmount }}元 &nbsp;&nbsp;&nbsp;&nbsp;
        现金实收总额：{{ totalActualAmount }}元 &nbsp;&nbsp;&nbsp;&nbsp;
        余额实收总额：{{ Number((totalExpectAmount - totalActualAmount).toFixed(2)) }}元 -->
      <!-- 营收金额=2-3
        实收金额=应收总额（包含余额支付）
        预收金额=充值总额（发生消费，记负数）
        预收余额=充值总余额 -->
      <!-- 总金额：{{ head.buyAmount | moneyFormat }}元<i
          style="width: 10px; display: inline-block"
        ></i>
        目标总金额：{{ head.targetAmount | moneyFormat }}元<i
          style="width: 10px; display: inline-block"
        ></i>
        目标金额达成率：{{ head.targetAmountFinishScale }}%<i
          style="width: 10px; display: inline-block"
        ></i>

        购买人数：{{ head.buyNum }}人<i
          style="width: 10px; display: inline-block"
        ></i>
        人均购买金额：{{ head.avgBuyAmount | moneyFormat }}元<i
          style="width: 10px; display: inline-block"
        ></i>
        目标人数：{{ head.targetNum }}人<i
          style="width: 10px; display: inline-block"
        ></i>
        目标达成率：{{ head.targetFinishScale }}%<i
          style="width: 10px; display: inline-block"
        ></i> -->

      <!-- 购买总金额：{{ head.buyAmount | moneyFormat }}元<i
          style="width: 10px; display: inline-block"
        ></i> -->

      <!-- 购买率：{{ head.buyScale }}%
        <i style="width: 10px; display: inline-block"></i> -->

      <!-- 总人数：{{ head.totalNum }}元<i
          style="width: 10px; display: inline-block"
        ></i> -->
    </div>
    <div class="tableWrap">
      <el-table
        style="width: 100%"
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
        :data="tableList"
        @sort-change="sortChang"
        :default-sort="{
            prop: 'totalBuyAmount',
            order: 'descending',
          }"
      >

        <!-- <el-table-column
            align="center"
            prop="targetNum"
            label="排名"
          ></el-table-column> -->
        <el-table-column align="center" label="排名" type="index" width="50">
        </el-table-column>
        <el-table-column
          align="center"
          prop="organName"
          label="分部"
          width="170px"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="totalBuyAmount"
          label="总成交金额"
          sortable="custom"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.totalBuyAmount | moneyFormat }}元</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="avgBuyAmount"
          label="人均购买金额"
          sortable="custom"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.avgBuyAmount | moneyFormat }}元</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="targetAmount"
          label="20节1v1成交金额/人数"
          width="150px"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.vip1V120Amount| moneyFormat  }}元/{{ scope.row.vip1V120Num }}人
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="targetAmount"
          label="40节1v1成交金额/人数"
          width="150px"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.vip1V140Amount| moneyFormat  }}元/{{ scope.row.vip1V140Num }}人
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="targetAmount"
          label="20节1v2成交金额/人数"
          width="150px"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.vip1V220Amount| moneyFormat  }}元/{{ scope.row.vip1V220Num }}人
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="targetAmount"
          label="40节1v2成交金额/人数"
          width="150px"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.vip1V240Amount| moneyFormat  }}元/{{ scope.row.vip1V240Num }}人
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="targetAmount"
          label="乐理课成交金额/人数"
          width="150px"
          v-if="year === '2021'"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.musicTheoryAmount | moneyFormat }}元/{{ scope.row.musicTheoryNum }}人
            </div>
          </template>
        </el-table-column>

        <el-table-column label="详情" align="left">
          <template slot-scope="scope">
            <div>
              <el-button type="text" @click="gotoDetail(scope.row)"
                >详情</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        /> -->
      <!-- </div> -->
    </div>
    <qr-code v-model="qrcodeStatus" title="统计二维码" :codeUrl="codeUrl" />
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import QrCode from "@/components/QrCode/index";
import { vaildTeachingUrl } from "@/utils/validate";
import { getDoubleElevenList, getDoubleElevenList2022 } from "./api";
import { Export } from "@/utils/downLoadFile";
import cleanDeep from "clean-deep";
import qs from "qs";
export default {
  components: { pagination, QrCode },
  data() {
    return {
      searchForm: {
        organId: null,
      },
      searchList: { sort: "totalBuyAmount", order: "DESC" },
      head: {
        avgBuyAmount: null,
        buyAmount: null,
        buyNum: null,
        buyScale: null,
        targetAmount: null,
        targetFinishScale: null,
        targetNum: null,
        totalNum: null,
      },
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      qrcodeStatus: false,
      codeUrl: "",
      qrcodeStatus2: false,
      codeUrl2: "",
      year: "2021",
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  async mounted() {
    // 获取分部
    await this.$store.dispatch("setBranchs");
    this.year = this.$route.query.year || "2021"
    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      try {
        let res = null
        if (this.year === "2021") {
          res = await getDoubleElevenList({
            organId: this.searchForm.organId,
            ...this.searchList
          });
        } else {
          res = await getDoubleElevenList2022({
            organId: this.searchForm.organId,
            ...this.searchList
          });
        }
        //   ...this.searchList,
        this.tableList = res.data;
        // this.head = res.data.head;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.searchForm.organId = null;
      this.searchList = { sort: "targetFinishScale", order: "DESC" };
      this.search();
    },
    sortChang(val) {
      const dates = {
        ascending: "ASC",
        descending: "DESC",
      };
      this.searchList = {};
      if (val.prop && val.order) {
        this.searchList = { sort: val.prop, order: dates[val.order] };
        // this.searchList[val.prop] = dates[val.order];
      }
      this.rules.page = 1;
      this.getList();
    },
    onCreateQRCode(row) {
      // 生成报名二维码
      this.qrcodeStatus = true;
      if (this.year === "2022") {
        this.codeUrl = vaildTeachingUrl() + "/#/statistic-20221111";
      } else {
        this.codeUrl = vaildTeachingUrl() + "/#/statistic-20211111";
      }
    },
    onCreateQRCode2(row) {
      // 生成报名二维码
      this.qrcodeStatus = true;
      this.codeUrl = vaildTeachingUrl() + "/#/statistic-20221111?type=hz";
    },
    gotoDetail(row) {
      // console.log('跳转详情')
      if (this.year === "2022") {
        this.$router.push({
          path: "/operateManager/2021doubleDetail",
          query: { organId: row.organId, organName: row.organName, year: "2022" },
        });
      } else {
        this.$router.push({
          path: "/operateManager/2021doubleDetail",
          query: { organId: row.organId, organName: row.organName },
        });
      }
    },
    async onExport() {
      let obj = {
        organId: this.searchForm.organId,
        ...this.searchList,
      };
      await Export(
        this,
        {
          url: this.year === "2021" ? "/api-web/export/organDoubleEleven2021Statis" : "/api-web/export/organDoubleEleven2022Statis",
          fileName: this.year === "2021" ? "双十一活动.xls" : "双十一活动.xls",
          method: "post",
          params: qs.stringify(cleanDeep(obj)),
        },
        "您确定导出双十一活动列表?"
      );
    },
    async onExport2() {
      //   let { getTime, ...rest } = this.searchForm;
      // let params = {
      //   ...rest,
      //   page: this.rules.page,
      //   rows: this.rules.limit,
      //   ...getTimes(getTime, ["startTime", "endTime"]),
      // };
      let obj = {
        organId: this.searchForm.organId,
        ...this.searchList,
        year: this.year,
      };
      await Export(
        this,
        {
          url: "/api-web/export/doubleEleven2021OrderDetail",
          fileName: this.$route.query.year === "2021" ? "学员购买详情.xls" : "学员购买详情.xls",
          method: "post",
          params: qs.stringify(cleanDeep(obj)),
        },
        "您确定导出双十一活动详情列表?"
      );
    },
    onCancel() {
      this.$store.dispatch("delVisitedViews", this.$route);
      this.$router.push("/activeMarketing");
    },
  },
};
</script>
<style lang='scss' scoped>
.titleCell {
  display: inline-block;
}
.tableWrap {
  margin-top: 20px;
}
.qrcode {
  display: flex;
  flex-direction: column;
  align-items: center;
  img {
    width: 200px;
    height: 200px;
    margin: 0 auto;
  }
}
.code-url {
  font-size: 18px;
  text-align: center;
  padding: 15px 15px 0 15px;
}
</style>
