<template>
  <div class="my-order">
    <div class="my-order__header">
      <el-tabs :value="activeName" @tab-click="tabChange">
        <el-tab-pane
          v-for="(tab, tabIndex) in list"
          :key="tabIndex"
          :label="tab.count ? `${tab.label}(${tab.count})` : `${tab.label}`"
          :name="tab.name"
        ></el-tab-pane>
      </el-tabs>
    </div>

    <div class="my-order__body">
      <div class="list" v-if="orderList.length == 0">
        <div class="no-data">您暂无相关订单哦~</div>
      </div>
      <div class="list" v-else>
        <template v-if="activeName == '2' && orderList.length">
          <el-button
            type="primary"
            class="invoice"
            @click="go('/person-center/my-invoice')"
            >发票申请</el-button
          >
        </template>
        <div class="order-item" v-for="(item, index) in orderList" :key="index">
          <div class="order-item__header">
            <div class="state">
              订单号：
              <div class="note">{{ item.orderSn }}</div>
            </div>
            <div class="time">
              {{ $tools.timestampToTime(item.createTime) }}
            </div>
          </div>
          <div class="order-item__body clearfix">
            <template v-if="activeName != '4'">
              <div class="order-item__body__goodslist">
                <div
                  class="goods-item clearfix"
                  v-for="(items, itemIndex) in item.orderGoodsList"
                  :key="itemIndex"
                >
                  <div class="img">
                    <img
                      :src="$tools.splitImgHost(items.coverUrl, true)"
                      alt=""
                    />
                  </div>
                  <div class="text">
                    <div class="title">{{ items.goodsName }}</div>
                    <div class="desc" v-if="item.orderFrom !== 6">
                      应付金额：
                      <span class="note">￥{{ items.goodsRealPrice }}</span>
                    </div>
                  </div>

                  <div
                    class="btn btn--nobg"
                    @click="refund(item.orderSn, items.goodsId)"
                    v-if="
                      items.refundStatus === 0 &&
                      (item.orderFrom === 2 || item.orderFrom === 3) &&
                      (item.orderStatus === 1 ||
                        item.orderStatus === 2 ||
                        item.orderStatus === 3) &&
                      (items.goodsType == '1' ||
                        items.goodsType == '2' ||
                        items.goodsType == '6') &&
                      items.goodsPrice > 0
                    "
                  >
                    申请退款
                  </div>
                  <div class="state" v-if="items.refundStatus == 2">已退款</div>
                </div>
              </div>
              <div class="price-wrap">
                <div class="btns" v-if="item.orderFrom !== 6">
                  <div class="price-text">实付金额</div>
                  <div class="price-number">￥{{ item.payPrice }}</div>
                </div>
              </div>
              <div
                class="btns-wrap"
                v-if="
                  (item.orderStatus === 0 &&
                    (item.orderFrom === 2 || item.orderFrom === 3)) ||
                  ((item.orderStatus === -1 || item.orderStatus === -2) &&
                    (item.orderFrom === 2 || item.orderFrom === 3))
                "
              >
                <div class="btns">
                  <el-button
                    type="primary"
                    class="btn"
                    v-if="
                      item.orderStatus === 0 &&
                      (item.orderFrom === 2 || item.orderFrom === 3)
                    "
                    @click="pay(item)"
                  >
                    立即支付
                  </el-button>
                  <div
                    class="btn btn--nobg"
                    v-if="
                      item.orderStatus === 0 &&
                      (item.orderFrom === 2 || item.orderFrom === 3)
                    "
                    @click="cancelOrder(item)"
                  >
                    取消订单
                  </div>
                  <div
                    class="btn btn--nobg"
                    v-if="
                      (item.orderStatus === -1 || item.orderStatus === -2) &&
                      (item.orderFrom === 2 || item.orderFrom === 3)
                    "
                    @click="delOrder(item)"
                  >
                    删除订单
                  </div>
                  <!-- <div class="btn btn--warm">开始学习</div> -->
                </div>
              </div>
            </template>
            <template v-else>
              <div class="order-item__body__goodslist">
                <div class="goods-item clearfix">
                  <div class="img">
                    <img
                      :src="$tools.splitImgHost(item.coverUrl, true)"
                      alt=""
                    />
                  </div>
                  <div class="text">
                    <div class="title">{{ item.goodsName }}</div>
                    <div class="desc">
                      <!-- 应付金额：
                      <span class="note">￥99.99</span> -->
                    </div>
                  </div>
                </div>
              </div>
              <div class="refund-wrap">
                <div class="btns">
                  <template v-if="!item.wxpayRefundId"> 退款失败 </template>
                  <template v-else>
                    <div class="price-text">已退款</div>
                    <div class="price-number">￥{{ item.refundFee }}</div>
                  </template>
                </div>
              </div>
            </template>
          </div>
        </div>

        <div class="pagination">
          <el-pagination
            @current-change="currentChange"
            background
            layout="prev, pager, next"
            :total="total"
            :pager-count="5"
            :page-size="formData.pageSize"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  name: "MyOrder",
  data() {
    return {
      orderList: [],
      activeName: "1",
      formData: {
        status: "0,1",
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      loading: null,
      list: [
        {
          label: "待支付",
          count: 0,
          name: "1",
        },
        {
          label: "已支付",
          count: 0,
          name: "2",
        },
        {
          label: "已取消",
          count: 0,
          name: "3",
        },
        {
          label: "已退款",
          count: 0,
          name: "4",
        },
      ],
    };
  },
  mounted() {
    if (this.$route.query.state) {
      this.activeName = this.$route.query.state;
    }
    this.getOrderNum();
    this.getOrderList();
  },
  methods: {
    getOrderNum() {
      this.$request
        .getorderlists({
          status: "0,1",
          pageNum: 1,
          pageSize: 1,
          orderStatus: "0",
        })
        .then((res) => {
          this.list[0].count = res.total;
        });

      this.$request
        .getorderlists({
          status: "0,1",
          pageNum: 1,
          pageSize: 1,
          orderStatus: "1,2,3,4",
        })
        .then((res) => {
          this.list[1].count = res.total;
        });

      this.$request
        .getorderlists({
          status: "0,1",
          pageNum: 1,
          pageSize: 1,
          orderStatus: "-1,-2",
        })
        .then((res) => {
          this.list[2].count = res.total;
        });

      this.$request
        .orderRefundList({
          pageNum: 1,
          pageSize: 1,
        })
        .then((res) => {
          this.list[3].count = res.total;
        })
        .catch(this.hideLoading);
    },
    currentChange(e) {
      this.formData.pageNum = e;

      if (this.activeName == "4") {
        //退款订单
        this.formData.status = "";
        this.orderRefundList();
      } else {
        //其他订单

        this.formData.status = "0,1";
        this.getOrderList();
      }
    },
    tabChange(e) {
      if (this.activeName == e.name) {
        return;
      }

      this.formData.pageNum = 1;
      this.activeName = e.name;
      if (this.activeName == "4") {
        //退款订单
        this.formData.status = "";
        this.formData.pageNum = 1;
        this.orderRefundList();
      } else {
        //其他订单

        this.formData.status = "0,1";
        this.getOrderList();
      }
    },
    pay(item) {
      localStorage.removeItem("checkGoodsList");
      this.$router.push({
        path: "/payment",
        name: "支付",
        params: {
          total: item.payPrice,
          orderSn: item.orderSn,
        },
      });
    },

    refund(orderSn, goodsId) {
      this.$confirm("确定要退款吗？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        closeOnPressEscape: false,
        distinguishCancelAndClose: false,
        showClose: false,
      })
        .then((_) => {
          this.showLoading();
          this.$request
            .refundSmallOrder({ orderSn: orderSn, goodsId: goodsId })
            .then((res) => {
              this.hideLoading();
              this.$message({
                type: "success",
                message: "退款成功",
              });
              this.getOrderNum();
              this.getOrderList();
            })
            .catch((err) => {
              this.hideLoading();
              this.$message({
                type: "warning",
                message: "不可以退款哦",
              });
            });
        })
        .catch((err) => {});
    },

    delOrder(item) {
      this.$confirm("确定要删除该订单吗？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        closeOnPressEscape: false,
        distinguishCancelAndClose: false,
        showClose: false,
      })
        .then((_) => {
          this.showLoading();
          this.$request
            .eddOrder({ orderId: item.orderId, status: -1 })
            .then((res) => {
              this.$message({
                type: "success",
                message: "订单删除成功",
              });
              this.hideLoading();
              this.getOrderNum();
              this.getOrderList();
            })
            .catch((err) => {
              this.hideLoading();
              this.$message({
                type: "warning",
                message: err.msg,
              });
            });
        })
        .catch((err) => {
          this.hideLoading();
        });
    },

    orderRefundList() {
      this.showLoading();
      this.formData.orderStatus = "";
      this.$request
        .orderRefundList(this.formData)
        .then((res) => {
          this.hideLoading();
          this.orderList = res.rows;
          this.total = res.total;
        })
        .catch(this.hideLoading);
    },

    /**
     * 取消订单
     */
    cancelOrder(item) {
      this.$confirm("确定要取消该订单吗？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        closeOnPressEscape: false,
        distinguishCancelAndClose: false,
        showClose: false,
      })
        .then((_) => {
          this.showLoading();
          this.$request
            .eddOrder({ orderId: item.orderId, orderStatus: -1 })
            .then((res) => {
              this.hideLoading();
              this.getOrderNum();
              this.$message({
                type: "success",
                message: "订单取消成功",
              });
              this.getOrderList();
            })
            .catch((err) => {
              this.hideLoading();
              this.$message({
                type: "warning",
                message: err.msg,
              });
            });
        })
        .catch((_) => {});
    },

    showLoading(obj) {
      this.loading = this.$loading({
        lock: true,
        text: (obj && obj.text) || "loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0)",
        target: document.querySelector(".my-order__body"),
      });
    },

    hideLoading() {
      this.loading.close();
    },

    //获取订单
    getOrderList() {
      this.showLoading();
      if (this.activeName == "1") {
        this.formData.orderStatus = "0";
      }
      if (this.activeName == "2") {
        this.formData.orderStatus = "1,2,3,4";
      }
      if (this.activeName == "3") {
        this.formData.orderStatus = "-1,-2";
      }
      this.$request
        .getorderlists(this.formData)
        .then((res) => {
          this.orderList = res.rows;
          this.total = res.total;
          this.hideLoading();
        })
        .catch(this.hideLoading);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.my-order {
  &__header {
    /deep/ .el-tabs__header {
      margin-bottom: 0;
    }
    /deep/ .el-tabs__item {
      padding: 0 30px !important;
    }
  }
  &__body {
    .invoice {
      margin-top: 10px;
    }
    .list {
      .order-item {
        margin-top: 24px;
        background: #fafbfc;
        border: 1px solid #eeeeee;
        box-shadow: 0px 0px 7px 1px rgba(0, 0, 0, 0.04);
        border-radius: 8px;
        overflow: hidden;

        &__header {
          height: 40px;
          border-bottom: 1px solid #eee;
          padding: 0 18px;

          .state {
            margin-top: 8px;
            float: left;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;

            .note {
              vertical-align: middle;
              display: inline-block;
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #3f8dfd;
              line-height: 24px;
            }
          }

          .time {
            float: right;
            line-height: 40px;
            text-align: right;
            font-size: 12px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;
          }
        }

        &__body {
          display: flex;
          &__goodslist {
            flex: 1;
            .goods-item {
              margin-bottom: 20px;
              border-top: 1px solid #eee;
              border-bottom: 1px solid #eee;

              &:nth-last-of-type(1) {
                margin: 0;
                border-bottom: 0;
              }

              &:nth-of-type(1) {
                border-top: 0;
              }
              .img {
                float: left;
                width: 160px;
                height: 90px;
                display: table-cell;

                img {
                  max-width: 100%;
                  max-height: 100%;
                  display: inline-block;
                  vertical-align: middle;
                }
              }

              .text {
                float: left;
                margin-left: 12px;
                .title {
                  margin-top: 10px;
                  font-size: 16px;
                  font-family: Microsoft YaHei;
                  font-weight: bold;
                  color: #333333;
                }

                .desc {
                  margin-top: 30px;
                  font-size: 14px;

                  .note {
                    color: #ff3b30;
                  }
                }
              }

              .state {
                font-size: 14px;
                margin: 30px 0 0 0;
                float: right;
                width: 122px;
                height: 32px;
                line-height: 32px;
                text-align: center;
              }

              .btn {
                float: right;
                cursor: pointer;
                margin: 30px 0 0 0;
                width: 122px;
                height: 32px;
                font-size: 14px;
                background: #3f8dfd;
                border-radius: 16px;
                text-align: center;
                line-height: 32px;
                color: #fff;

                &--warm {
                  background: #ff3b30;
                }

                &--nobg {
                  background: none;
                  color: #333;
                }
              }
            }
          }

          .refund-wrap {
            width: 320px;
            display: table;
            border-left: 1px solid #eee;

            .btns {
              display: table-cell;
              vertical-align: middle;
              text-align: center;

              .price-text {
                margin: 2px 0;
                width: 100%;
                height: 32px;
                font-size: 14px;
                display: inline-block;
                text-align: center;
                line-height: 32px;
                color: #333333;
              }

              .price-number {
                margin: 2px 0;
                width: 100%;
                height: 32px;
                font-weight: bold;
                display: inline-block;
                text-align: center;
                line-height: 32px;
                font-size: 16px;
                color: #333333;
              }
            }
          }

          .price-wrap {
            display: table;
            width: 160px;
            border-left: 1px solid #eee;

            .btns {
              display: table-cell;
              vertical-align: middle;
              text-align: center;

              .price-text {
                margin: 2px 0;
                width: 150px;
                height: 32px;
                font-size: 14px;
                display: inline-block;
                text-align: center;
                line-height: 32px;
                color: #333333;
              }

              .price-number {
                font-weight: bold;
                margin: 2px 0;
                width: 150px;
                height: 32px;
                font-size: 14px;
                display: inline-block;
                text-align: center;
                line-height: 32px;
                color: #ff3b30;
              }
            }
          }

          .btns-wrap {
            display: table;
            width: 160px;
            border-left: 1px solid #eee;

            .btns {
              display: table-cell;
              vertical-align: middle;
              text-align: center;

              .btn {
                cursor: pointer;
                margin: 2px 0;
                width: 122px;
                height: 32px;
                font-size: 14px;
                padding: 0;
                border-radius: 16px;
                display: inline-block;
                text-align: center;
                line-height: 32px;
                color: #fff;

                &--warm {
                  background: #ff3b30;
                }

                &--nobg {
                  background: none;
                  color: #333;
                }
              }
            }
          }
        }
      }
    }

    .no-data {
      text-align: center;
      padding: 50px 0;
      color: #666;
      font-size: 16px;
    }

    .pagination {
      padding: 30px 0;
      text-align: center;
    }
  }
}
</style>
