<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="分销商名称">
        <el-input
          v-model="queryParams.personName"
          placeholder="分销商名称"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分销商类型">
        <el-select
          v-model="queryParams.personType"
          placeholder="分销商类型"
          clearable
          style="width: 140px"
        >
          <el-option
            v-for="dict in dict.type.distribution_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号">
        <el-input
          v-model="queryParams.orderId"
          placeholder="订单号"
          clearable
          style="width: 200px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购买人手机号" label-width="100px">
        <el-input
          v-model="queryParams.memberMobile"
          placeholder="购买人手机号"
          clearable
          style="width: 180px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="场次时间">
        <el-date-picker style="width: 230px;" v-model="queryParams.performDate" @change="pagePerformTimeListFun"
          type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="场次">
        <el-select v-model="queryParams.performTimeId" placeholder="场次" clearable style="width: 100%">
          <el-option v-for="dict in pagePerformTimeList" :key="dict.id" :label="dict.timeSnapshot+'('+dict.performTimeStart+'-'+dict.performTimeEnd+')'" :value="dict.id" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="场次时间" label-width="70px">
          <el-date-picker
            style="width: 250px;"
            v-model="queryParams.performDate"
            @change="pagePerformTimeListFun"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="场次" v-if="!multiPerformDate">
          <el-select
            v-model="queryParams.performTimeId"
            placeholder="场次"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in pagePerformTimeList"
              :key="dict.id"
              :label="dict.timeSnapshot+'('+dict.performTimeStart+'-'+dict.performTimeEnd+')'"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
      <el-form-item label="支付时间">
          <el-date-picker
            style="width: 230px;"
            v-model="queryParams.time"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
      </el-form-item>
      <el-form-item label="入驻银联主体名称" label-width="150px">
          <el-input
            v-model="queryParams.unionName"
            placeholder="请输入入驻银联主体名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </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-button
        type="primary"
        size="mini"
        icon="el-icon-download"
        :disabled="dataList.length === 0"
        v-hasPermi="['recordMr:recordMr:downloadExcel']"
        @click="handleExport"
        v-loading.fullscreen.lock="handleExportLoading"
        element-loading-text="正在拼命生成数据中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.5)"
      >导出excel</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" border>
      <el-table-column label="序号" align="center" type="index" width="60"></el-table-column>
      <el-table-column label="分销商名称" align="center" prop="personName" />
      <el-table-column label="分销商类型" align="center" prop="personType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.distribution_type" :value="scope.row.personType"/>
        </template>
      </el-table-column>
      <el-table-column label="入驻银联主体名称" align="center" prop="unionName" />
      <el-table-column label="订单号" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="openDetails(scope.row)">{{ scope.row.orderId }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="场次时间" align="center" prop="performDate" />
      <el-table-column label="场次" width="120" align="center" prop="timeSnapshot">
        <template slot-scope="scope">
          <span>{{ scope.row.timeSnapshot }} <br /> ({{ scope.row.performTimeStart + '-' + scope.row.performTimeEnd }})</span>
        </template>
      </el-table-column>
      <el-table-column label="购买人手机号" align="center" prop="memberMobile" />
      <el-table-column label="票务名称" align="center" prop="goodsName" />
      <el-table-column label="佣金单价(元)" align="center" prop="brokeragePrice">
        <template slot-scope="scope">
          <span v-if="scope.row.brokeragePrice">￥{{ scope.row.brokeragePrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="realPrice" />
      <el-table-column label="支付时间" align="center" prop="payTime" />
      <el-table-column label="购买数量(张)" align="center" prop="retailNum" />
      <el-table-column label="已核销(张)" align="center" prop="usedNum" />
      <el-table-column label="获得佣金(元)" align="center" prop="brokeragePriceTotal">
        <template slot-scope="scope">
          <span v-if="scope.row.brokeragePriceTotal">￥{{ scope.row.brokeragePriceTotal }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="获得时间" align="center" prop="createTime" width="160" >-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

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

    <!-- 详情 -->
    <details-dia ref="detailsDia" @getList="getList"></details-dia>

  </div>
</template>

<script>

import { pageList , downTimeCountListXls } from '@/api/distribution/recordMr'
import detailsDia from "./dialog/details.vue";
import { exportExcel } from '@/utils/exportexcel'
import { pagePerformTimeList } from "@/api/schedulingMr/schedulingMr"
import moment from "moment"
export default {
  name: "agreement",
  dicts: ['agreement_type', 'distribution_type'],
  components: { detailsDia },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: null,
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      visibleStatus: false,
      newObj: {},
      visibleType: '',
      handleExportLoading: false,
      pagePerformTimeList: [], // 场次列表
      multiPerformDate:false,
    };
  },
  created() {
    this.$set(this.queryParams,'performDate',[moment().format("yyyy-MM-DD"),moment().format("yyyy-MM-DD")])
    this.pagePerformTimeListFun(this.queryParams.performDate)
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      let params = {...this.addDateRange(this.queryParams, this.dateRange)};
      if(params.time){
        params.payBeginTime = params.time[0];
        params.payEndTime = params.time[1];
      }else {
        params.payBeginTime = null
        params.payEndTime = null
      }
      if(params.performDate){
        params.performBeginDate = params.performDate[0];
        params.performEndDate = params.performDate[1];
      }else {
        params.performBeginDate   = null
        params.performEndDate = null
      }
      delete params.performDate
      delete params.time
      pageList(params)
      .then(response => {
          this.dataList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this.queryParams, 'orderId', '');
      this.$set(this.queryParams, 'personName', '');
      this.$set(this.queryParams, 'memberMobile', '');
      this.$set(this.queryParams, 'personType', '');
      this.$set(this.queryParams, 'performDate', '');
      this.$set(this.queryParams, 'performTimeId', '');
      this.$set(this.queryParams, 'time', '');
      this.$set(this.queryParams, 'unionName', '');
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    /** 详情按钮操作 */
    openDetails(row, type) {
      this.$refs["detailsDia"].openDialog("详情", row, type);
    },
    /**
     * 导出报表
     * @date 2022-10-24
     * @returns {any}
     */
     handleExport() {
      this.$confirm('您确定要导出当前查询的数据吗？', '提示', {
        confirmButtonText: '确定 ',
        cancelButtonText: '取消 ',
        type: 'warning'
      })
        .then(() => {
          this.handleExportLoading = true;
          let params = {...this.addDateRange(this.queryParams, this.dateRange)};
          if(params.time){
            params.payBeginTime = params.time[0];
            params.payEndTime = params.time[1];
          }else {
            params.payBeginTime = null
            params.payEndTime = null
          }
          if(params.performDate){
            params.performBeginDate = params.performDate[0];
            params.performEndDate = params.performDate[1];
          }else {
            params.performBeginDate   = null
            params.performEndDate = null
          }
          delete params.performDate
          delete params.time
          delete params.pageNum
          delete params.pageSize
          downTimeCountListXls(params)
            .then((res) => {
              exportExcel(res, '分销记录', '.xlsx');
              this.handleExportLoading = false;
            })
            .catch(() => {
              this.handleExportLoading = false;
            });
        })
        .catch(() => {
          this.$message.info('您已取消导出！');
        });
    },
    async pagePerformTimeListFun(value) {
      try {
        this.pagePerformTimeList = []
        this.$set(this.queryParams, 'performTimeId', null)
        if (!value) {
          return
        }
        if(value.length==2&&value[0]==value[1]){
          this.multiPerformDate = false
        }else{
          this.multiPerformDate = true
          return
        }
        let { data, code } = await pagePerformTimeList({
          performDate: value[0],
          pageNum: 1,
          pageSize: 999
        })
        this.pagePerformTimeList = [].concat(data.rows)
      } catch (error) {

      }
    },
  }
};
</script>
