<template>
  <div class="app-container">

    <div v-show="!isShowForm">

      <el-radio-group v-model="status" size="medium" @change="changeStatus()">
        <el-radio-button :label="''">全部({{total.all}})</el-radio-button>
        <el-radio-button :label="1">进行中({{total.jxz}})</el-radio-button>
        <el-radio-button :label="2">未开始({{total.wks}})</el-radio-button>
        <el-radio-button :label="3">已过期({{total.ygq}})</el-radio-button>
      </el-radio-group>
      
      <div class="mymain-container">
        <div class="btn-group clearfix">
          <div class="fl">
            <el-button size="small" type="primary" icon="el-icon-plus" @click="toForm()">新增</el-button>
          </div>
        </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="活动名称" prop="name" min-width="120"></el-table-column>
            <el-table-column align="center" label="参与产品数量" prop="goodsNum" min-width="120"></el-table-column>
            <el-table-column align="center" label="活动时间" min-width="200">
              <template slot-scope="scope">
                {{ scope.row.startTime | dateToDayFilter }} 至 {{ scope.row.endTime | dateToDayFilter }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="状态" prop="status"></el-table-column>
            <el-table-column align="center" label="订单数量" prop="orderNums" min-width="100"></el-table-column>
            <el-table-column align="center" label="订单总金额" prop="orderTotalAmount" min-width="100"></el-table-column>
            <el-table-column align="center" label="创建人" prop="createBy" min-width="100"></el-table-column>
            <el-table-column align="center" label="创建时间" prop="createTime" min-width="160"></el-table-column>
            <el-table-column align="center" label="更新人" prop="updateBy" min-width="100"></el-table-column>
            <el-table-column align="center" label="更新时间" prop="updateTime" min-width="160"></el-table-column>

            <el-table-column align="center" label="操作" fixed="right" width="160">
              <template slot-scope="scope">
                <el-button type="text" @click="toForm(scope.row)">编辑</el-button>
                <el-button type="text" @click="openOrderDetail(scope.row.id)">详情</el-button>
                <el-button type="text" @click="handleExport(scope.row.id)">导出订单</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>
    </div>

    <!-- 订单详情 -->
    <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="400">
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column align="center" prop="orderId" label="订单编号" min-width="180" 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="num" label="件数" min-width="100"></el-table-column>
          <el-table-column align="center" prop="payAmount" label="订单金额" min-width="100"></el-table-column>
          <el-table-column align="center" prop="shareRate" 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="createTime" 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>
    
    <FullpieceDiscountForm :listItem="queryItem" v-if="isShowForm" @backList="backList" />
    
  </div>
</template>

<script>
import { getList, getTotal, getOrderList } from "@/api/fullpiece_discount";
import { downloadFiles } from '@/utils/util'
import FullpieceDiscountForm from "@/views/activity/components/fullpiece_discount_form";

export default {
  components: {
    FullpieceDiscountForm
  },
  filters: {
    statusFilter(val) {
      const MAP = {
        NOPAY: '待付款',
        DFH: '待发货',
        YFH: '已发货',
        OVER: '已完成',
        CLOSE: '已关闭',
        REFUND: '售后',
        TIMEOUT: '已超时',
      }
      return MAP[val];
    }
  },
  data() {
    return {
      status: '',
      total: {
        all: 0,
        jxz: 0,
        wks: 0,
        ygq: 0
      },
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数

      queryItem: {},
      isShowForm: false,

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

  created() {
    this.getTotal();
    this.getList();
  },

  methods: {

    getTotal() {
      getTotal().then(res => {
        this.total = res.data;
      })
    },
    
    getList() {
      this.listLoading = true;
      getList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        status: this.status
      }).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

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

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

    changeStatus() {
      this.currentPage = 1;
      this.getList();
    },

    // 进入表单
    toForm(item) {
      this.queryItem = item;
      this.isShowForm = true;
    },

    backList(needRefresh) {
      this.queryItem = {};
      this.isShowForm = false;

      if(needRefresh) {
        this.currentPage = 1;
        this.getList();
      }
    },

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

    // 订单详情 - 打开弹窗
    openOrderDetail(id) {
      this.detailId = 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 = {
        promotionFullPieceId: id
      };
      downloadFiles('order/export', screenData);
    },
    
  }
}
</script>

<style scoped lang="scss">
  .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;
    }
  }
  .goods-container {
    .item {
      margin-bottom: 10px;
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  .couponDetail {
    height: 60vh;
    overflow-y: scroll;
    padding-right: 20px;
    .item {
      font-size: 16px;
      margin-bottom: 20px;
      &>span {
        float: left;
        &:first-child {
          width: 140px;
          text-align: right;
          margin-right: 10px;
          font-weight: 500;
        }
      }
      .table-span {
        width: calc(100% - 170px);
      }
    }
  }

  .addMember {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #EBEEF5;
    border-top: none;
    height: 50px;
  }

  // .memberTable {
  //   ::v-deep .el-table__header-wrapper .el-checkbox {
  //     display: none;
  //   }
  // }
    
    
</style>
