<template>
  <div class="refund-order-item-con">
    <div class="header flex-default">
      <div class="shop flex-default">
        <m-icon icon="icon-shop" :size="16" />
        <span class="name">{{ detail.enterpriseName }}</span>
        <m-icon icon="icon-more" :size="9" />
      </div>
      <div class="status" :style="{ color: detail.statusColor }">
        {{ detail.statusLabel }}
      </div>
    </div>
    <div class="content flex-default" @click="handleDetail">
      <div class="info flex-default" v-if="singleOrder">
        <img class="thumb" :src="detail.order1st.thumb" />
        <div class="spu">
          <div class="name">{{ detail.order1st.name }}</div>
          <div class="sku">{{ detail.order1st.propList | propFilter }}</div>
        </div>
      </div>
      <div class="info-list" v-else>
        <img
          class="thumb"
          v-for="(item, index) of detail.orders"
          :key="`thumb-${detail.oid}-${index}`"
          :src="item.thumb"
        />
      </div>
      <div class="right-con flex-default">
        <div class="price">￥{{ data.amount }}</div>
        <div class="amt" v-show="detail.totalNum > 1">
          共{{ detail.totalNum }}件
        </div>
      </div>
    </div>
    <div class="opt flex-default">
      <van-button
        v-show="showCancel"
        class="opt-btn"
        type="primary"
        size="small"
        plain
        round
        @click="handleCancel"
      >
        撤销
      </van-button>
    </div>
  </div>
</template>

<script>
import { RefundStatus, RefundStatusObj } from "@/enums";
export default {
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    detail() {
      let totalNum = 0;
      this.data.orders.forEach((o) => {
        totalNum += o.number || 0;
      });
      let statusObj = RefundStatusObj[this.data.orderState] || {}
      return {
        ...this.data,
        order1st: this.data.orders[0],
        statusLabel: statusObj.label,
        statusColor: statusObj.color,
        totalNum,
      };
    },
    singleOrder() {
      return this.data.orders.length == 1;
    },
    showCancel() {
      return this.data.orderState == RefundStatus.APPLY;
    },
  },
  data() {
    return {};
  },
  methods: {
    handleDetail() {
      let query = { oid: this.data.oid };
      this.$router.push({ name: "refund-money-detail", query });
    },
    handleCancel() {
      this.$showLoading();
      this.$api({
        apiName: "cancelOrderRefund",
        postData: this.data.oid,
        success: () => {
          this.data.orderState = RefundStatus.CANCEL;
        },
        complete: () => {
          this.$hideLoading();
        },
      });
    },
  },
  filters: {
    propFilter(props) {
      return props.map((item) => item.value).join("+");
    },
  },
};
</script>

<style lang="less">
.refund-order-item-con {
  background: #ffffff;
  width: 100%;
  padding: 14px 16px 18px 16px;
  border-radius: 8px;
  margin-top: 10px;
  > .header {
    color: #140603;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
    .shop {
      align-items: center;
      flex: 1;
    }
    .shop .name {
      font-size: 14px;
      margin-left: 6px;
      margin-right: 5px;
      font-weight: bold;
    }
    .status {
      font-size: 12px;
      color: #dd2e41;
    }
  }
  > .content {
    align-items: center;
    margin-bottom: 20px;
    justify-content: space-between;
    .thumb {
      height: 84px;
      width: 84px;
      border-radius: 3px;
      background-color: lightgray;
      margin-right: 4px;
    }
    > .info-list {
      width: ~"calc(100% - 60px)";
      white-space: nowrap;
      overflow-x: auto;
      .thumb {
        display: inline-block;
      }
    }
    > .info {
      align-items: center;
      flex: 1;
    }
    > .info .spu {
      flex: 1;
      margin-left: 10px;
      width: 73px;
    }
    > .info .spu .name {
      color: #140603;
      font-size: 13px;
      line-height: 15px;
      margin-bottom: 6px;
    }
    > .info .spu .sku {
      color: #72767d;
      font-size: 11px;
      line-height: 13px;
    }
    > .right-con {
      flex-direction: column;
      align-items: flex-end;
      margin-left: 5px;
    }
    > .right-con .price {
      color: #140603;
      font-size: 14px;
      font-weight: bold;
      line-height: 16px;
      margin-bottom: 5px;
    }
    > .right-con .amt {
      color: #aeaeae;
      font-size: 10px;
      line-height: 12px;
      font-weight: bold;
    }
  }
  > .opt {
    justify-content: flex-end;
    .opt-btn {
      margin-left: 8px;
      width: 70px;
      padding: 0;
      height: 25px;
      &.black {
        border-color: #aeaeae;
        color: #140603;
      }
    }
  }
}
</style>