<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="渠道/门店名称">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入渠道/门店名称"
          clearable
          size="small"
          style="width: 240px"
          @change="pageParams.pageNum = 1;handleQuery()"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="tradeTimeArr"
          type="datetimerange"
          size="small"
          value-format="timestamp"
          range-separator="至"
          start-placeholder="时间选择"
          end-placeholder="时间选择"
          @change="tardeTime"
        >
        </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">
      <el-col :span="1.5">
        <el-button
          type="infor"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExportDraw"
          v-hasPermi="['finance:withdraw:export']"
        >导出记录</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <br>

    <!-- 列表状态 -->
    <el-tabs type="card" v-model="state" @tab-click="handleClick">
      <el-tab-pane label="全部" name="null"></el-tab-pane>
      <el-tab-pane label="待提现" name="1"></el-tab-pane>
      <el-tab-pane label="已提现" name="2"></el-tab-pane>
      <el-tab-pane label="提现失败" name="3"></el-tab-pane>
    </el-tabs>
    <!-- 列表 -->
    <el-table ref="table" v-loading="loading" :data="list">
      <!-- 不展开列表 -->
      <el-table-column label="申请时间" min-width="100">
        <template slot-scope="{ row }">
          <div>
            {{ parseTime(row.createTime) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" min-width="100">
        <template slot-scope="{ row }">
          <div v-if="row.status.value === 3">{{ parseTime(row.updateTime) || "--" }}</div>
          <div v-else>{{ parseTime(row.transferTime) || "--" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="渠道名称" prop="name" min-width="85" />
      <el-table-column label="手机号码" prop="mobile" min-width="85" />
      <el-table-column label="渠道类型" prop="addr" min-width="80">
        <template slot-scope="{ row }">
          <div>{{ row.level == 0 ? "门店" : "渠道" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="提现金额(元)" align="center">
        <template slot-scope="{ row }">
          <div>{{ $numberFormat(row.money) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" prop="addr" min-width="80">
        <template slot-scope="{ row }">
          <div>
            <el-tag type="success" v-if="row.status.value === 1">待提现</el-tag>
            <el-tag type="info" v-else>{{ row.status.desc }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="{ row }">
          <div>
            <el-button
              v-hasPermi="['finance:withdraw:query']"
              type="text"
              @click="getDetail(row)"
              >查看</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 订单详情 -->
    <withdraw-detail v-if="detailShow" :dialog-show="detailShow" :info="detailInfo" @close="close" />
  </div>
</template>
<script>
import { withdrawList, withdrawDetail, exchangeWithdrawExport } from "@/api/finance/withdraw";
import WithdrawDetail from './components/Detail'
export default {
  name: "Withdraw",
  components: {
    WithdrawDetail
  },
  data() {
    return {
      loading: false,
      showSearch: true,
      // 筛选
      queryParams: {
        name: "",
        startDay: "",
        endDay: "",
        channelId: "",
        status: null,
      },
      // 分页
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      list: [],
      // 列表状态
      state: "null",
      // 申请时间
      tradeTimeArr: [],
      // 弹框显示
      detailShow: false,
      // 提现id
      id: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 提现列表
    getList() {
      this.loading = true;
      withdrawList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        this.queryParams
      )
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            res.rows.forEach((item) => {
              item.status = JSON.parse(item.status);
            });
            this.total = res.total;
            this.list = res.rows;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    // 搜索
    handleQuery() {
      this.getList()
    },

    // 重置
    resetQuery() {
      this.queryParams = {
        name: "",
        startDay: "",
        endDay: "",
        channelId: "",
        status: null,
      };
      this.state = "null";
      this.tradeTimeArr = [];
      this.getList();
    },

    // 列表状态切换
    handleClick(e) {
      this.queryParams.status = Number(e.name);
      this.getList();
    },

    // 选择申请时间
    tardeTime(e) {
      if (e) {
        this.queryParams.startDay = e[0];
        this.queryParams.endDay = e[1];
        this.pageParams.pageNum = 1
        this.handleQuery();
      } else {
        this.queryParams.startDay = "";
        this.queryParams.endDay = "";
        this.pageParams.pageNum = 1
        this.handleQuery();
      }
    },

    // 查看详情
    getDetail(row) {
      this.id = row.id
      this.getDetailInfo()
    },

    // 订单详情
    getDetailInfo() {
      withdrawDetail({ id: this.id }).then(res=>{
        console.log('res', res)
        if(res.code == 0){
          this.detailInfo = res.data
          this.detailShow = true;
        }
      })
    },

    // 弹框关闭
    close() {
      this.detailShow = false;
      this.getList();
    },
    // 导出订单
    handleExportDraw() {
      this.$confirm("是否确认导出记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.vloading = this.$loading({
            lock: true,
            text: "正在导出记录.....",
            background: "rgba(0, 0, 0, 0.7)",
          });
          return exchangeWithdrawExport(this.queryParams);
        })
        .then((response) => {
          this.vloading.close();
          this.download(response.msg);
        })
        .catch(() => {
          this.vloading.close();
        });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-tabs--border-card > .el-tabs__content {
  padding: 0;
}
</style>
