<template>
  <div class="app-container withdrawalApp-table" :style="{'--q-height':qHeight}">
    <div class="app-container-query" ref="queryFormBox">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="分销商名称" prop="personName">
          <el-input
            v-model="queryParams.personName"
            placeholder="请输入分销商名称"
            clearable
            style="width: 220px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="开户人姓名" prop="realName">
          <el-input
            v-model="queryParams.realName"
            placeholder="请输入开户人姓名"
            clearable
            style="width: 220px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="打款状态" prop="voucherStatus">
          <el-select
            v-model="queryParams.voucherStatus"
            placeholder="打款状态"
            clearable
            style="width: 140px"
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.name"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
            style="width: 240px"
            v-model="dateRange"
            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>
    </div>
    
    <div class="app-container-table-box">
      <el-row :gutter="10" class="mb8">
        <el-col :span="3">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-download"
            @click="handleExport"
            v-hasPermi="['withdrawalApp:export']"
            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>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <div class="app-container-table-info">
        <el-table ref="tables" v-loading="loading" height="100%" :data="dataList" border>
          <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
          <el-table-column label="分销商名称" align="center" prop="personName"  width="100" />
          <el-table-column label="分销商类型" align="center" prop="type" width="100" >
            <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="realName" width="100" />
          <el-table-column label="开户行" align="center" prop="openBankName" width="120" />
          <el-table-column label="开户人身份证号" align="center" prop="openIdCard" width="120" />
          <el-table-column label="银行卡号" align="center" prop="bankNo" width="150" />
          <el-table-column label="申请金额" align="center" prop="realPrice" />
          <el-table-column label="申请时间" align="center" prop="createTime" width="160" />
          <el-table-column label="打款状态" align="center" prop="voucherStatus">
            <template slot-scope="scope">
              {{ scope.row.voucherStatus === 1 ? '已打款':'未打款' }}
            </template>
          </el-table-column>
          <el-table-column label="打款凭证" align="center" prop="voucherImg" >
            <template slot-scope="scope">
            <el-button type="text" @click="seeCenter(scope.row, 'img')">查看</el-button>
          </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="提现到账时间" align="center" prop="queryTime"  width="160" />
          <el-table-column label="操作人" align="center" prop="auditUserName" />
          <el-table-column label="操作" align="center" width="180" fixed="right" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <!-- <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['withdrawalApp:withdrawalApp:edit']"
              >修改</el-button> -->
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.voucherStatus === 0"
                @click="withdrawImgAudit(scope.row)"
                v-hasPermi="['withdrawalApp:add']"
              >上传打款凭证</el-button>
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.voucherStatus === 1"
                @click="withdrawImgAudit(scope.row)"
                v-hasPermi="['withdrawalApp:edit']"
              >修改打款凭证</el-button>
              <el-button
                size="mini"
                type="text"
                @click="viewOrderInfo(scope.row)"
                v-hasPermi="['withdrawalApp:orderInfo']"
              >分销订单信息</el-button>
            </template>
          </el-table-column>
        </el-table>

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

    <!--  修改凭证  -->
    <imgAduit ref="imgAduit" @getList="getList" />

    <!--  分销订单信息  -->
    <orderInfo ref="orderInfo" />
    
    <el-dialog
      title="查看凭证"
      :visible.sync="visibleStatus"
      width="600px"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <div v-if="visibleType == 'img'">
        <el-image
          style="width: 260px; height: 100%;margin: 0 5px;"
          v-for="(item, index) in newObj.contractImgList"
          :key="index"
          :src="item"
          :preview-src-list="newObj.contractImgList"
          fit="cover"
        />
      </div>
      <!-- <div v-if="visibleType == 'html'">
        <div v-html="newObj.centent"></div>
      </div> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibleStatus = false">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { 
  pageList, 
  downOrderListXls
} from '@/api/distribution/Withdrawl'
import imgAduit from "./dialog/imgAduit.vue";
import orderInfo from "./dialog/orderInfo.vue";
import { exportExcel } from '@/utils/exportexcel'
import moment from "moment"
// import html2canvas from 'html2canvas'

export default {
  name: "WithdrawalApp",
  dicts: ['distribution_type'],
  components: { imgAduit, orderInfo },
  data() {
    return {
      qHeight: '0px',
      resizeObserver: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: null,
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // type: undefined
      },
      statusList: [
        {id: 1, name: '未打款', value: 0},
        {id: 2, name: '已打款', value: 1},
      ],
      handleExportLoading: false,
      visibleStatus: false,
      newObj: {},
      visibleType: ''
    };
  },
  created() {
    this.setDateRange();
    this.getList();
  },
  mounted() {
    this.resizeObserver = new ResizeObserver(entries => {
      for (let entry of entries) {
        const { width, height } = entry.contentRect;
        this.qHeight = height + 'px'
      }
    });
    this.resizeObserver.observe(this.$refs.queryFormBox);
  },
  methods: {
    // 申请时间处理
    setDateRange() {
      const today = moment(); // 当前日期
      const firstDayOfMonth = moment().startOf('month'); // 当前月的第一天
      // 使用 moment 格式化日期为 yyyy-MM-dd
      this.dateRange = [
        firstDayOfMonth.format("YYYY-MM-DD"),
        today.format("YYYY-MM-DD")
      ];
    },
    /** 查询列表 */
    getList() {
      this.dataList = []
      this.loading = true;
      if(this.dateRange && this.dateRange.length > 0){
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      } else {
        if(this.queryParams.beginDate) {
          delete this.queryParams.beginDate
          delete this.queryParams.endDate
        }
      }
      pageList(this.queryParams)
      .then(response => {
          let list = response.data.rows
          list.forEach(item =>{
            item.switchValue = item.status;
          })
          this.dataList = list
          this.total = response.data.total;
          this.loading = false;
        }
      ).catch(()=>{
        this.dataList = []
        this.loading = false;
      })
    },
    // 分销订单信息
    viewOrderInfo(row) {
      this.$refs["orderInfo"].openDialog("分销订单信息", row);
    },
    // 打款凭证
    withdrawImgAudit(row) {
      this.$refs["imgAduit"].openDialog("打款凭证", row);
    },
    /** 查看 */
    seeCenter(obj, type) {
      this.visibleStatus = true
      this.visibleType = type;
      this.newObj = obj;
      if(obj.voucherImg){
        this.newObj.contractImgList = obj.voucherImg.split(',')
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this.queryParams, 'personName', '');
      // this.$set(this.queryParams, 'time', []);
      this.$set(this.queryParams, 'voucherStatus', '');
      this.$set(this.queryParams, 'realName', '');
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
   
    // 导出报表
     handleExport() {
      this.$confirm('您确定要导出当前查询的数据吗？', '提示', {
        confirmButtonText: '确定 ',
        cancelButtonText: '取消 ',
        type: 'warning'
      })
        .then(() => {
          this.handleExportLoading = true;
          // const { pageNum, pageSize} = this.params;
          let postMap = {}
          if(this.dateRange && this.dateRange.length > 0){
            this.queryParams.beginDate = this.dateRange[0];
            this.queryParams.endDate = this.dateRange[1];
          } else {
            if(this.queryParams.beginDate){
              delete this.queryParams.beginDate
              delete this.queryParams.endDate
            }
          }
          let params = JSON.parse(JSON.stringify(this.queryParams))
          // for (let key in params) {
          //   if(key != 'pageNum' && key != 'pageSize') {
          //     postMap[key] = params[key]
          //   }
          // }
          downOrderListXls(params)
            .then((res) => {
              exportExcel(res, '提现申请', '.xlsx');
              this.handleExportLoading = false;
            })
            .catch((error) => {
              // console.log("error===",error)
              this.handleExportLoading = false;
            });
        })
        .catch(() => {
          this.$message.info('您已取消导出！');
        });
    },
  },
  beforeDestroy() {
    this.resizeObserver.unobserve(this.$refs.queryFormBox);
    this.resizeObserver.disconnect();
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  height: calc( 100vh - 110px );
  box-sizing: border-box;
}
.app-container-table-box {
  height: calc( 100% - var(--q-height) );
  .app-container-table-info {
    height: calc( 100% - 100px );
  }
}
</style>