<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="订单号" label-width="60px">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入订单号"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式">
        <el-select
          v-model="queryParams.payWay"
          placeholder="支付方式"
          clearable
          style="width: 100%"
        >
            <el-option
              v-for="dict in dict.type.pay_way_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
        </el-select>
      </el-form-item>
      <el-form-item label="收支类型">
        <el-select
          v-model="queryParams.incomeExpenses"
          placeholder="收支类型"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="dict in incomeExpensesList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型">
        <el-select
          v-model="queryParams.businessType"
          placeholder="业务类型"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="dict in businessTypeList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.value"
          />
        </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="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>
        <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"  style="margin-left: 0; margin-top: 10px">
      <el-button
        type="primary"
        size="mini"
        icon="el-icon-download"
        :disabled="tableData.length === 0"
        v-hasPermi="['flowingWaterMr:flowingWaterMr: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="id">
        <template slot-scope="scope">
          <span style="color: #1890ff;cursor: pointer;" @click="openDetails(scope.row)">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购票数量" align="center" prop="quantity" />
      <el-table-column label="实付单价" align="center" prop="price" />
      <el-table-column label="支付方式" align="center" prop="payWay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_way_type" :value="scope.row.payWay"/>
        </template>
      </el-table-column>
      <el-table-column label="变动类型" align="center" prop="incomeExpenses" />
      <el-table-column label="变动金额(元)" align="center" prop="type">
        <template slot-scope="scope">
          <span>￥{{ scope.row.orderPrice && scope.row.orderPrice != 0 ? ( (scope.row.incomeExpenses=='支出'?'-':'') + scope.row.orderPrice) : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="businessType" />
      <el-table-column label="产生渠道" align="center" prop="refundTime" width="160" >
        <template slot-scope="scope">
          <!-- <span>{{ sourceMap[scope.row.source] }}</span> -->
          <dict-tag :options="dict.type.order_form_type" :value="scope.row.source"/>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="160" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payTime) }}</span>
            </template>
      </el-table-column>
      <el-table-column label="场次时间" align="center" prop="performDate" />
      <el-table-column label="场次" width="120" align="center" prop="performTime" />
      <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, downOrderListXls } from '@/api/financeMr/flowingWaterMr'
import { exportExcel } from '@/utils/exportexcel'
import detailsDia from "./dialog/details";
import { pagePerformTimeList } from "@/api/schedulingMr/schedulingMr"
import moment from "moment"
export default {
  name: "agreement",
  dicts: ['agreement_type','order_form_type','pay_way_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,
      },
      statusList: [
        {id: 1, name: '申请中', value: 0},
        {id: 2, name: '退款成功', value: 1},
        {id: 3, name: '退款失败', value: 2},
        {id: 4, name: '退款中', value: 3},
      ],
      sourceMap: {
        1: '小程序',
        2: '美团',
        3: '携程',
        4: '公众号',
        5: '支付宝',
      },
      incomeExpensesList: [
        {id: 1, name: '收入', value: '收入'},
        {id: 2, name: '支出', value: '支出'},
      ],
      businessTypeList: [
        {id: 1, name: '购买演出票', value: '购买演出票'},
        {id: 2, name: '票务退款', value: '票务退款'},
      ],
      visibleStatus: false,
      newObj: {},
      visibleType: '',
      handleExportLoading: false,
      tableData: {},
      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: {
    moment,
    /** 查询列表 */
    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;
        }
      ).catch(()=>{
        this.dataList = [];
        this.total = 0;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this.queryParams, 'id', '');
      this.$set(this.queryParams, 'businessType', '');
      this.$set(this.queryParams, 'incomeExpenses', '');
      this.$set(this.queryParams, 'payWay', '');
      this.$set(this.queryParams, 'performDate', '');
      this.$set(this.queryParams, 'time', '');
      this.$set(this.queryParams, 'performTimeId', '');
      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;
          // const { pageNum, pageSize} = this.params;
          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
          downOrderListXls(params)
            .then((res) => {
              exportExcel(res, '流水管理', '.xlsx');
              this.handleExportLoading = false;
            })
            .catch(() => {
              this.handleExportLoading = false;
            });
        })
        .catch(() => {
          this.$message.info('您已取消导出！');
        });
    },
    /** 详情按钮操作 */
    openDetails(row, type) {
      this.$refs["detailsDia"].openDialog("详情", row, type);
    },
    async pagePerformTimeListFun(value) {
      // console.log('value',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>
