<template>
  <div class="app-container">
    <el-page-header @back="goBack" content="拼团详情" style="margin-bottom: 30px"></el-page-header>

    <!-- 筛选条件 -->
    <div class="screen-container">
      <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="screenForm.phone" placeholder="请输入手机号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="table">
      <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>

        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column align="center" label="团长名称" prop="nickName" min-width="120"></el-table-column>
        <el-table-column align="center" label="所属网点" prop="websitName" min-width="120"></el-table-column>
        <el-table-column align="center" label="职务" prop="position" min-width="100"></el-table-column>
        <el-table-column align="center" label="手机号码" prop="phone" min-width="160"></el-table-column>
        <el-table-column align="center" label="首次分享时间" prop="firstTime" min-width="160"></el-table-column>
        <el-table-column align="center" label="分享次数">
          <template slot-scope="scope">
            <el-button type="text" @click="openShareDetail(scope.row.userId)">{{scope.row.shareNum}}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单数量" prop="orderNum" min-width="100">
          <template slot-scope="scope">
            {{scope.row.orderNum || 0}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单台数" prop="goodsNum" min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsNum || 0}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="成交金额" prop="orderTotalAmount" min-width="100">
          <template slot-scope="scope">
            {{scope.row.orderTotalAmount || 0}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="团长分佣" prop="shareTotalAmount" min-width="100">
          <template slot-scope="scope">
            {{scope.row.shareTotalAmount || 0}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="已分佣金额" prop="settledAmount" min-width="100">
          <template slot-scope="scope">
            {{scope.row.settledAmount || 0}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单管理" min-width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="openOrderDetail(scope.row.userId)">详情</el-button>
            <el-button type="text" @click="handleExport(scope.row.userId)">导出订单</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal">
        </el-pagination>
      </div>
    </div>
    
    <div class="page-footer">
      <div class="footer" :class="classObj">
        <el-button @click="goBack">返 回</el-button>
      </div>
    </div>

    <!-- 分享详情 -->
    <el-dialog title="分享详情" :visible.sync="shareDialog" :show-close="false" width="70%" :close-on-click-modal="false">
      <div class="table" style="margin: 10px 0 20px;">
        <el-table 
          v-loading="shareTable_listLoading" 
          :data="shareTable_dataList" 
          element-loading-text="Loading" 
          tooltip-effect="dark" 
          style="width: 100%" 
          max-height="270">
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column align="center" prop="userName" label="用户名称" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="goodsName" label="商品名称" min-width="200" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="userPhone" label="手机号码" min-width="120"></el-table-column>
          <el-table-column align="center" prop="createTime" label="访问时间" min-width="160"></el-table-column>
        </el-table>
      </div>
      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @current-change="shareTableCurrentChange"
            :current-page="shareTable_currentPage"
            :page-size="shareTable_pageSize"
            background
            layout="prev, pager, next"
            :total="shareTable_listTotal">
          </el-pagination>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="shareDialog = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 订单详情 -->
    <el-dialog title="订单详情" :visible.sync="orderDialog" :show-close="false" width="70%" :close-on-click-modal="false">
      <div class="table" style="margin: 10px 0 20px;">
        <el-table 
          v-loading="orderTable_listLoading" 
          :data="orderTable_dataList" 
          element-loading-text="Loading" 
          tooltip-effect="dark" 
          style="width: 100%" 
          max-height="270">
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column align="center" prop="orderId" label="订单编号" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="orderTitle" label="商品名称" min-width="200" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="totalAmount" label="订单金额" min-width="100"></el-table-column>
          <el-table-column align="center" prop="orderStatus" label="订单状态" min-width="100">
            <template slot-scope="scope">
              {{scope.row.orderStatus | statusFilter}}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="totalShareAmount" label="团长分佣" min-width="100"></el-table-column>
          <el-table-column align="center" prop="payTime" label="下单时间" min-width="160"></el-table-column>
          <el-table-column align="center" label="操作" min-width="80">
            <template slot-scope="scope">
              <el-button type="text" @click="toOrderDetail(scope.row.orderId)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @current-change="orderTableCurrentChange"
            :current-page="orderTable_currentPage"
            :page-size="orderTable_pageSize"
            background
            layout="prev, pager, next"
            :total="orderTable_listTotal">
          </el-pagination>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="orderDialog = false">关 闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { getDetailList, getShareList, getOrderList } from '@/api/groupbuy'
import { downloadFiles } from '@/utils/util'

export default {
  filters: {
    statusFilter(val) {
      const MAP = {
        NOPAY: '待付款',
        DFH: '待发货',
        YFH: '已发货',
        OVER: '已完成',
        CLOSE: '已关闭',
        REFUND: '售后',
      }
      return MAP[val];
    }
  },

  data() {
    return {
      id: '',
      detailUserId: '',
      screenForm: { // 筛选表单数据
        phone: '',
      },
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数

      shareDialog: false, // 分享详情 - 弹窗
      shareTable_dataList: null, // 分享详情 - 列表数据
      shareTable_listLoading: true, // 分享详情 - 列表加载loading
      shareTable_currentPage: 1, // 分享详情 - 当前页码
      shareTable_pageSize: 10, // 分享详情 - 每页数量
      shareTable_listTotal: 0, // 分享详情 - 列表总数

      orderDialog: false, // 订单详情 - 弹窗
      orderTable_dataList: null, // 订单详情 - 列表数据
      orderTable_listLoading: true, // 订单详情 - 列表加载loading
      orderTable_currentPage: 1, // 订单详情 - 当前页码
      orderTable_pageSize: 10, // 订单详情 - 每页数量
      orderTable_listTotal: 0, // 订单详情 - 列表总数

    }
  },
  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened
      }
    },
  },
  created() {
    const { id } = this.$route.query;
    this.id = id;

    this.getList();
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },

    // 获取详情
    getList() {
      getDetailList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        promotionGroupId: this.id,
        mobile: this.screenForm.phone
      }).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    // 分享详情 - 获取列表
    getShareList() {
      getShareList({
        pageNum: this.shareTable_currentPage,
        pageSize: this.shareTable_pageSize,
        userId: this.detailUserId,
        promotionGroupId: this.id
      }).then(res => {
        this.shareTable_dataList = res.data.records;
        this.shareTable_listTotal = res.data.total;
        this.shareTable_listLoading = false;
      })
    },

    // 分享详情 - 打开弹窗
    openShareDetail(id) {
      this.detailUserId = id;
      this.shareDialog = true;
      this.shareTable_currentPage = 1;
      this.getShareList();
    },

    // 分享详情 - 更改列表当前页
    shareTableCurrentChange(val) {
      this.shareTable_currentPage = val;
      this.getShareList();
    },

    // 订单详情 - 获取列表
    getOrderList() {
      getOrderList({
        pageNum: this.orderTable_currentPage,
        pageSize: this.orderTable_pageSize,
        userId: this.detailUserId,
        promotionGroupId: this.id
      }).then(res => {
        this.orderTable_dataList = res.data.records;
        this.orderTable_listTotal = res.data.total;
        this.orderTable_listLoading = false;
      })
    },

    // 订单详情 - 打开弹窗
    openOrderDetail(id) {
      this.detailUserId = id;
      this.orderDialog = true;
      this.orderTable_currentPage = 1;
      this.getOrderList();
    },

    // 订单详情 - 更改列表当前页
    orderTableCurrentChange(val) {
      this.orderTable_currentPage = val;
      this.getOrderList();
    },

    // 订单详情
    toOrderDetail(orderId) {
      let {href} = this.$router.resolve({path: `/order/detail?orderId=${orderId}`});
      window.open(href, '_blank');
    },

    // 导出
    handleExport(id) {
      let screenData = {
        promotionGroupUserId: id,
        promotionGroupId: this.id
      };
      downloadFiles('order/export', screenData);
    },

  }
}
</script>

<style scoped lang="scss">
  .diy-table {
    overflow-x: scroll;
    border: 1px solid #f5f5f5;
    margin-top: 20px;
    .table-head {
      display: flex;
      .item {
        flex-shrink: 0;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 500;
        font-size: 14px;
        background: #f5f5f5;
      }
      .goods {
        width: 200px;
      }
      .name {
        width: 120px;
      }
      .input {
        width: 160px;
      }
      .opera {
        width: 180px;
      }
    }
    .goods-item {
      display: flex;
      align-content: center;
      .goods-info {
        flex-shrink: 0;
        width: 200px;
        padding: 0 10px;
        display: flex;
        align-items: center;
        border-bottom: 1px solid #f5f5f5;
        border-right: 1px solid #f5f5f5;
        &:last-child {
          border-bottom: none;
        }
      }
      .spec-list {
        border-bottom: 1px solid #f5f5f5;
        border-right: 1px solid #f5f5f5;
        &:last-child {
          border-bottom: none;
        }
        .spec-item {
          display: flex;
          align-items: center;
          border-bottom: 1px solid #f5f5f5;
          &:last-child {
            border-bottom: none;
          }
          .col {
            flex-shrink: 0;
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0 10px;
            border-right: 1px solid #f5f5f5;
            &:last-child {
              border-right: none;
            }
          }
          .name {
            width: 120px;
            font-size: 14px;
          }
          .input {
            width: 160px;
          }
          .btn {
            width: 100px;
          }
        }
      }
      .operation {
        flex-shrink: 0;
        width: 80px;
        padding: 0 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-bottom: 1px solid #f5f5f5;
      }
    }
  }

  .dialog-container {
    .left {
      width: 140px;
      height: 350px;
      overflow-y: scroll;
      .group {
        margin-top: 10px;
      }
      .child {
        margin-top: 5px;
        .item {
          padding-left: 18px;
        }
      }
      .item {
        cursor: pointer;
        line-height: 24px;
      }
    }
    .right {
      width: calc(100% - 140px);
      height: 350px;
      box-sizing: border-box;
      padding-left: 20px;
    }
  }
</style>