<template>
  <div class="app-container app-container-scheduling">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item v-if="queryShow.personName" label="分销商名称">
        <el-input
          v-model="queryParams.personName"
          placeholder="分销商名称"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
        <span @click="clearQuery('personName')" class="query_clear"><i class="el-icon-circle-close"></i></span>
      </el-form-item>
      <el-form-item v-if="queryShow.personType" 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>
        <span @click="clearQuery('personType')" class="query_clear"><i class="el-icon-circle-close"></i></span>
      </el-form-item>
      <el-form-item label-width="80px" v-if="queryShow.orderId" label="订单号">
        <el-input
          v-model="queryParams.orderId"
          placeholder="订单号"
          clearable
          style="width: 200px;"
          @keyup.enter.native="handleQuery"
        />
        <span @click="clearQuery('orderId')" class="query_clear"><i class="el-icon-circle-close"></i></span>
      </el-form-item>
      <el-form-item v-if="queryShow.memberMobile" label="购买人手机号" label-width="100px">
        <el-input
          v-model="queryParams.memberMobile"
          placeholder="购买人手机号"
          clearable
          style="width: 180px;"
          @keyup.enter.native="handleQuery"
        />
        <span @click="clearQuery('memberMobile')" class="query_clear"><i class="el-icon-circle-close"></i></span>
      </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 v-if="queryShow.performDate" label="场次时间" label-width="80px">
          <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>
          <span @click="clearQuery('performDate')" class="query_clear"><i class="el-icon-circle-close"></i></span>
        </el-form-item>
        <el-form-item label="场次" label-width="60px" v-if="!multiPerformDate && queryShow.performDate">
          <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>
          <span @click="clearQuery('performDate')" class="query_clear"><i class="el-icon-circle-close"></i></span>
        </el-form-item>
      <el-form-item v-if="queryShow.time" 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>
          <span @click="clearQuery('time')" class="query_clear"><i class="el-icon-circle-close"></i></span>
      </el-form-item>
      <el-form-item v-if="queryShow.unionName" label="入驻银联主体名称" label-width="150px">
          <el-input
            v-model="queryParams.unionName"
            placeholder="请输入入驻银联主体名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
          <span @click="clearQuery('unionName')" class="query_clear"><i class="el-icon-circle-close"></i></span>
      </el-form-item>
      <el-form-item>
        <el-dropdown @command="openQuery">
          <el-button size="mini" type="primary" icon="el-icon-plus"></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="personName">分销商名称</el-dropdown-item>
            <el-dropdown-item command="personType">分销商类型</el-dropdown-item>
            <el-dropdown-item command="orderId">订单号</el-dropdown-item>
            <el-dropdown-item command="memberMobile">购买人手机号</el-dropdown-item>
            <el-dropdown-item command="performDate">场次时间/场次</el-dropdown-item>
            <el-dropdown-item command="time">支付时间</el-dropdown-item>
            <el-dropdown-item command="unionName">入驻银联主体名称</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button style="margin-left: 10px;" 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" fixed="left"></el-table-column>
      <el-table-column label="分销商名称" width="120" align="center" prop="personName" fixed="left" />
      <el-table-column label="分销商类型" width="120" align="center" prop="personType" fixed="left">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.distribution_type" :value="scope.row.personType"/>
        </template>
      </el-table-column>
      <el-table-column label="入驻银联主体名称" width="120" align="center" prop="unionName" fixed="left" />
      <el-table-column label="订单号" width="180" align="center" fixed="left">
        <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="下单地址" width="180" align="center" prop="performDate11">
        <template slot-scope="scope">
          <span>{{ setAderr(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="场次时间" width="120" 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="购买人手机号" width="120" align="center" prop="memberMobile" />
      <el-table-column label="票务名称" width="120" align="center" prop="goodsName" />
      <el-table-column label="佣金单价(元)" width="120" 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="实付金额" width="120" align="center" prop="realPrice" />
      <el-table-column label="支付时间" width="120" align="center" prop="payTime" />
      <el-table-column label="购买数量(张)" width="120" align="center" prop="retailNum" />
      <el-table-column label="已核销(张)" width="120" align="center" prop="usedNum" />
      <el-table-column label="获得佣金(元)" width="120" 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: [],
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      visibleStatus: false,
      newObj: {},
      visibleType: '',
      handleExportLoading: false,
      pagePerformTimeList: [], // 场次列表
      multiPerformDate:false,

      queryShow: {
        personName: true,
        personType: true,
        orderId: false,
        memberMobile: false,
        performDate: true,
        time: false,
        unionName: 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) {

      }
    },
    setAderr(row) {
      let str = ""
      str = str + (row.province?row.province:'')
      str = str + (row.city?row.city:'')
      str = str + (row.county?row.county:'')
      str = str + (row.address?row.address:'')
      return str
    },

    clearQuery(key) {
      this.$set(this.queryShow,key,false)
    },
    openQuery(key) {
      this.$set(this.queryShow,key,true)
    }
  }
};
</script>
<style scoped lang="scss">
.app-container-scheduling ::v-deep .el-select__tags {
    flex-wrap: inherit !important;
    overflow-x: auto !important;
}
.app-container-scheduling ::v-deep .el-form-item__content {
  position: relative;
}
.app-container-scheduling ::v-deep .el-form-item__content .query_clear{
  position: absolute;
  top: -15px;
  right: -10px;
  display: none;
  cursor: pointer;
  z-index: 99;
}
.app-container-scheduling ::v-deep .el-form-item__content:hover .query_clear {
  display: block;
}
</style>