<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="分销商名称" label-width="90px">
        <el-input
          v-model="queryParams.personsName"
          placeholder="请输入分销商名称"
          clearable
          style="width: 200px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收支类型">
        <el-select
          v-model="queryParams.serialType"
          placeholder="收支类型"
          clearable
          style="width: 160px"
        >
          <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.serialContent"-->
<!--          placeholder="业务类型"-->
<!--          clearable-->
<!--          style="width: 160px"-->
<!--        >-->
<!--          <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
          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>
        <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="['distributionDetailMr:distributionDetailMr: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="personsName" />
      <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="type">
        <template slot-scope="scope">
          <span v-if="scope.row.balance">￥{{ scope.row.balance }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收支类型" align="center" prop="serialType">
        <template slot-scope="scope">
          <span>{{ scope.row.serialType == 1 ? '收入' : '支出' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="serialContent" />
      <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"
    />

  </div>
</template>

<script>

import { pageList, downOrderListXls } from '@/api/distribution/detailsMr'
import { exportExcel } from '@/utils/exportexcel'
export default {
  name: "agreement",
  dicts: ['distribution_type'],
  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: 1},
        {id: 2, name: '支出', value: 2},
      ],
      businessTypeList: [
        {id: 1, name: '佣金收益', value: '佣金收益'},
        {id: 2, name: '余额提现', value: '余额提现'},
      ],
      visibleStatus: false,
      newObj: {},
      visibleType: '',
      handleExportLoading: false,
      tableData: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.time && this.queryParams.time.length > 0){
        this.queryParams.beginTime = this.queryParams.time[0];
        this.queryParams.endTime = this.queryParams.time[1];
      } else {
        if(this.queryParams.beginTime){
          delete this.queryParams.beginTime
          delete this.queryParams.endTime
        }
      }
      pageList(this.addDateRange(this.queryParams, this.dateRange))
      .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, 'time', []);
      this.$set(this.queryParams, 'serialContent', '');
      this.$set(this.queryParams, 'personsName', '');
      this.$set(this.queryParams, 'serialType', '');
      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 postMap = {}
          for (let key in this.queryParams) {
            if(key != 'pageNum' && key != 'pageSize'){
              postMap[key] = this.queryParams[key]
            }
          }
          downOrderListXls(postMap)
            .then((res) => {
              exportExcel(res, '流水管理', '.xlsx');
              this.handleExportLoading = false;
            })
            .catch(() => {
              this.handleExportLoading = false;
            });
        })
        .catch(() => {
          this.$message.info('您已取消导出！');
        });
    },
  }
};
</script>
