<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号码" />
      <el-button v-permission="['GET /admin/commission/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <!-- <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download">导出</el-button>--->
    </div>
    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border="" fit highlight-current-row>
      <el-table-column align="center" label="合伙人" prop="user.nickname" />
      <el-table-column align="center" label="手机" prop="user.tel" />
      <el-table-column align="center" label="总提现额" prop="total" />
      <el-table-column align="center" label="已提现" prop="cashout" />
      <el-table-column align="center" label="提现审核中" prop="checking" />
      <el-table-column align="center" label="待生效金额" prop="frozen" />
      <el-table-column align="center" label="可提现" prop="blance" />

      <!-- <el-table-column align="center" label="已有下线人数" prop="people" /> -->
      <el-table-column align="center" label="购买总单次" prop="orders" />
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['GET /admin/commission/detail']" type="primary" size="mini" @click="handleDetail(scope.row)">订单</el-button>
          <el-button v-permission="['GET /admin/commission/relation']" type="primary" size="mini" @click="handleRelation(scope.row)">客户明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 下线 -->
    <el-dialog :visible.sync="detailDialogVisible" title="订单明细" width="800">
      <el-table :data="detail" border="" fit highlight-current-row>
        <el-table-column align="center" label="订单编号" prop="order.orderSn" />
        <el-table-column align="center" label="下单时间" prop="order.addTime" />
        <el-table-column align="center" label="商品" prop="orderGoodsName" />
        <el-table-column align="center" label="数量" prop="orderGoodsNum" />
        <el-table-column align="center" label="单价" prop="orderGoodsPrice" />
        <el-table-column align="center" label="佣金" prop="commission" />
        <el-table-column align="center" label="状态" prop="status">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.status | detailStatusFilter }}</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 下线星星明细 -->
    <el-dialog :visible.sync="relationDialogVisible" title="客户明细" width="800">
      <el-table :data="relation" border="" fit highlight-current-row>
        <el-table-column align="center" label="" type="expand">
          <template slot-scope="scope">
            <el-table :data="scope.row.commissionDetails" border="" fit highlight-current-row>
              <el-table-column align="center" label="订单编号" prop="order.orderSn" />
              <el-table-column align="center" label="下单时间" prop="order.addTime" />
              <el-table-column align="center" label="商品" prop="orderGoodsName" />
              <el-table-column align="center" label="数量" prop="orderGoodsNum" />
              <el-table-column align="center" label="单价" prop="orderGoodsPrice" />
              <el-table-column align="center" label="佣金" prop="commission" />
              <el-table-column align="center" label="状态" prop="status">
                <template slot-scope="scope">
                  <el-tag type="success">{{ scope.row.status | detailStatusFilter }}</el-tag>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>

        <el-table-column align="center" label="客户" prop="user.customerName" />
        <el-table-column align="center" label="手机号码" prop="user.tel" />
        <el-table-column align="center" label="关联时间" prop="createtime" />
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listCommission, detail, relation } from "@/api/commission";
import BackToTop from "@/components/BackToTop";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination

const statusMap = {
  0: "未生效",
  1: "已生效",
  2: "退单驳回",
};

export default {
  name: "Commission",
  components: { BackToTop, Pagination },
  filters: {
    detailStatusFilter(status) {
      return statusMap[status];
    },
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        mobile: undefined,
        sort: "id",
        order: "desc",
      },
      detail: [],
      detailDialogVisible: false,
      relation: [],
      relationDialogVisible: false,
      downloadLoading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      listCommission(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleDetail(row) {
      detail(row.id).then((response) => {
        this.detail = response.data.data;
      });
      this.detailDialogVisible = true;
    },
    handleRelation(row) {
      relation(row.userId).then((response) => {
        this.relation = response.data.data;
      });
      this.relationDialogVisible = true;
    },
  },
};
</script>

<style>
.el-table th.gutter {
  display: table-cell !important;
}
.fixed-width .el-button--mini {
  width: auto;
}
</style>
