<template>
  <view>
    <Loading
    	:type="3"
    	:loadStatus="loadStatus"
    	:showText="errorText"
    />
    <zj-page-layout
      v-if="detail"
      :hasFooter="true">
      <view class="all-container">
        <view class="address-container card">
          <view class="left">
            <view class="icon"><text class="iconfont icon-dingwei1"></text></view>
            <view class="hasdata">
              <view class="name">{{ detail.userName }}<text>{{ detail.userMobile }}</text></view>
              <view class="address ellipsis-2">
                {{ detail.province }}{{ detail.city }}{{ detail.area }}{{ detail.street }}{{ detail.receAddress }}
              </view>
            </view>
          </view>
        </view>

        <view class="card goods-container">
          <view class="title">商品信息</view>
          <view class="item">
            <image :src="detail.goodsUrl" mode="aspectFill"></image>
            <view class="right">
              <view class="name ellipsis-2">{{ detail.esGoodsName }}</view>
              <view class="des">{{ detail.goodsNote }}</view>
              <view class="bottom">
                <view class="price">{{detail.goodsPrice | priceFilter2}}</view>
                <view class="num">x{{detail.num}}</view>
              </view>
            </view>
          </view>
          <view class="total">订单总金额<text>{{detail.totalAmount | priceFilter2}}</text></view>
        </view>

        <view class="info-container card">
          <view class="title">订单信息</view>
          <view class="row">
            <view class="label">订单编号</view>
            <view class="value">{{detail.esOrderId}}</view>
          </view>
          <view class="row">
            <view class="label">创建时间</view>
            <view class="value">{{detail.createTime}}</view>
          </view>
          <view class="row" v-if="detail.payType == 'WECHAT'">
            <view class="label">付款时间</view>
            <view class="value">{{detail.payTime}}</view>
          </view>
          <view class="row">
            <view class="label">支付方式</view>
            <view class="value">{{{WECHAT: '在线支付', CASH: '到店支付'}[detail.payType]}}</view>
          </view>
          <view class="row" v-if="detail.payType == 'WECHAT'">
            <view class="label">支付单号</view>
            <view class="value">{{detail.transactionId}}</view>
          </view>
          <view class="row" v-if="detail.sendTime">
            <view class="label">发货时间</view>
            <view class="value">{{detail.sendTime}}</view>
          </view>
          <view class="row" v-if="detail.overTime">
            <view class="label">收货时间</view>
            <view class="value">{{detail.overTime}}</view>
          </view>
        </view>

        <view class="form-container card">
          <view class="title">退货信息</view>
          <view class="row aic">
            <view class="label"><text>*</text>退货类型</view>
            <u-radio-group v-model="refundForm.type" placement="row">
            	<u-radio name="GOODS" label="仅退货"></u-radio>
            	<u-radio name="REFUND" label="仅退款"></u-radio>
            	<u-radio name="GOODS_REFUND" label="退货退款"></u-radio>
            </u-radio-group>
          </view>
          <view class="row aic">
            <view class="label"><text>*</text>退货原因</view>
            <view class="picker" @tap="openReasonDialog">
              <view class="value" v-if="refundForm.reason">{{refundForm.reason}}</view>
              <view class="placeholder" v-else>请选择<text class="iconfont icon-jinru"></text></view>
            </view>
          </view>
          <view class="row">
            <view class="label">上传凭证</view>
            <view class="images">
              <view class="img" v-for="(it, idx) in refundForm.imageList">
                <image :src="it" mode="aspectFill" @tap="prevImg(it)"></image>
                <text class="iconfont icon-guanbi1" @tap="delImage(idx)"></text>
              </view>
              <view class="add" @tap="addImage()" v-if="refundForm.imageList.length < 9">
                <text class="iconfont icon-jiahao"></text>
                <text class="text">上传图片</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <template slot="footer">
        <view class="footer-btn-group">
          <u-button type="primary" text="提交申请" @click="submitData"></u-button>
        </view>
      </template>

      <zjDialogPicker
        ref="reasonDialog"
        :isShow="isShowReasonDialog"
        :multiple="false"
        :styleType="2"
        isSearch
        :title="'退货原因'"
        :list="reasonList"
        :keyName="'dictValue'"
        @search="searchReason"
        @cancel="isShowReasonDialog = false"
        @confirm="confirmReasonDialog">
      </zjDialogPicker>

    </zj-page-layout>
  </view>
</template>

<script>
  import { uploadImgFull } from '@/common/utils/util.js';
	import zjDialogPicker from "@/components/zj-dialog/zj-dialog-picker.vue";

  export default {
    components: {
    	zjDialogPicker
    },

    data() {
      return {
        id: null,
        detail: null,
        loadStatus: 0,
        errorText: '',
        refresherTriggered: false,

        refundForm: {
          type: 'GOODS',
          reason: '',
          imageList: [],
        },
        reasonList: [],
        isShowReasonDialog: false,
      }
    },

    async onLoad({id}) {
      this.id = id;
      this.getDetail();
    },

    methods: {
      getDetail() {
        return new Promise((resolve, reject) => {
          this.$api.post('/es/orderPay/detail', {
            esOrderId: this.id
          }).then(res => {
            this.detail = res.data;
            this.loadStatus = 0;
          }).catch(res => {
            this.errorText = res.message;
            this.loadStatus = 2;
          }).finally(res => {
            this.refresherTriggered = false;
            resolve(1);
          })
        })
      },

      getReasonList(keyword = '') {
        this.$api.post('/pay/sysDict', {
          dictType: 'REFUND_REASON'
        }).then(res => {
          this.reasonList = keyword ? res.data.filter(o => o.dictValue.indexOf(keyword) >= 0) : res.data;
        })
      },

      openReasonDialog() {
        this.getReasonList();
        this.isShowReasonDialog = true;
      },

      searchReason(keyword) {
        this.getReasonList(keyword);
      },

      confirmReasonDialog(e) {
        const item = this.reasonList[e[0]];
        this.refundForm.reason = item.dictValue;
        this.isShowReasonDialog = false;
      },

      // 添加图片
      async addImage() {
      	uni.chooseImage({
      		count: 9 - this.refundForm.imageList.length,
          sizeType: ['compressed'],
      		success: res => {
      			uni.showLoading();
      			res.tempFilePaths.forEach(async item => {
      				let data = await uploadImgFull(item);
              this.refundForm.imageList.push(data.url);
      			});
      			uni.hideLoading();
      		},
      		fail: err => {}
      	})
      },

      // 删除图片
      delImage(idx) {
        this.refundForm.imageList.splice(idx, 1);
      },

      // 预览图片
      prevImg(url) {
      	uni.previewImage({
      		current: url,
      		urls: this.refundForm.imageList
      	})
      },

      submitData() {
        if(!this.refundForm.type) return this.$toast('请选择退货类型');
        if(!this.refundForm.reason) return this.$toast('请选择退货原因');

        this.$api.post('/es/orderPay/refund', {
          esOrderId: this.id,
          refundType: this.refundForm.type,
          refundNote: this.refundForm.reason,
          fileUrls: this.refundForm.imageList.join(','),
        }).then(res => {
          this.$successToast();
          this.crossPage.$emit('refreshRecycleOrderList');
          this.crossPage.$emit('refreshRecycleOrderDetail');
          this.$navToPage({
          	delta: 1
          }, 'navigateBack')
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
.all-container {
  padding: 0 20rpx 20rpx;
}

.card {
  @include zj-card;
  background: #FFFFFF;
  border-radius: 20rpx;
  margin-top: 20rpx;
  padding: 0 20rpx;
}

.address-container {
  background: #FFFFFF;
  border-radius: 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 150rpx;
  padding: 0 20rpx;

  .right {
    font-size: 32rpx;
    color: $sec-font;
    flex-shrink: 0;
  }

  .left {
    display: flex;
    align-items: center;
    margin-right: 20rpx;

    .icon {
      width: 52rpx;
      height: 52rpx;
      border-radius: 50%;
      background: $theme-color;
      display: flex;
      flex-shrink: 0;
      justify-content: center;
      align-items: center;
      margin-right: 20rpx;

      .iconfont {
        font-size: 36rpx;
        color: #ffffff;
      }
    }

    .nodata {
      font-size: 28rpx;
      color: #999999;
    }

    .hasdata {
      .name {
        font-size: 32rpx;
        color: #333333;

        text {
          color: #999999;
          font-size: 28rpx;
          margin-left: 16rpx;
        }
      }

      .address {
        font-size: 28rpx;
        color: #666666;
        line-height: 34rpx;
        margin-top: 10rpx;
      }
    }
  }
}

.goods-container {
  .title {
    font-size: 32rpx;
    margin-top: 20rpx;
    font-weight: 500;
  }
  .item {
    padding: 20rpx 0;
    display: flex;
    align-items: center;
    background: #FFFFFF;
    border-bottom: 1px solid #eaeaea;
    image {
      width: 140rpx;
      height: 140rpx;
      display: block;
      margin-right: 20rpx;
      flex-shrink: 0;
    }
    .right {
      flex: 1;
      .name {
        font-size: 28rpx;
        color: #333333;
        line-height: 36rpx;
      }
      .des {
        font-size: 24rpx;
        color: #999999;
        margin-top: 8rpx;
      }
      .bottom {
        display: flex;
        align-items: center;
        margin-top: 12rpx;
        .price {
          font-size: 28rpx;
          color: $minor-color;
          font-weight: 600;
        }
        .num {
          font-size: 28rpx;
          margin-left: 12rpx;
        }
      }
    }
  }
  .total {
    height: 80rpx;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    font-size: 28rpx;
    text {
      font-size: 32rpx;
      color: $minor-color;
      font-weight: 500;
      margin-left: 8rpx;
    }
  }
}

.info-container {
  padding-bottom: 30rpx;
  .title {
    font-size: 32rpx;
    margin-top: 20rpx;
    font-weight: 500;
  }
  .row {
    display: flex;
    align-items: center;
    margin-top: 20rpx;
    .label {
      font-size: 28rpx;
      color: $sec-font;
      margin-right: 20rpx;
      flex-shrink: 0;
    }
    .value {
      font-size: 28rpx;
    }
  }
}

.form-container {
  padding-bottom: 30rpx;
  .title {
    font-size: 32rpx;
    margin-top: 20rpx;
    font-weight: 500;
  }
  .row {
    display: flex;
    margin-top: 30rpx;
    &.aic {
      align-items: center;
      .label {
        line-height: 1 !important;
      }
    }
    .label {
      width: 140rpx;
      text-align: right;
      font-size: 28rpx;
      margin-right: 20rpx;
      line-height: 38px;
      flex-shrink: 0;
      text {
        color: red;
      }
    }
    ::v-deep .u-radio {
      margin-right: 30rpx;
    }
    .picker {
      .placeholder {
        color: $theme-color;
        font-size: 28rpx;
      }
      .value {
        font-size: 28rpx;
      }
    }
    .images {
      display: flex;
      flex-wrap: wrap;
      .add {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 146rpx;
        height: 146rpx;
        border: 2rpx dashed #dadada;
        border-radius: 10rpx;
        .iconfont {
          font-size: 48rpx;
          color: #999999;
        }
        .text {
          font-size: 24rpx;
          color: #999999;
          margin-top: 10rpx;
        }
      }
      .img {
        position: relative;
        margin-right: 20rpx;
        margin-bottom: 20rpx;
        &:nth-child(3n) {
          margin-right: 0;
        }
        image {
          width: 150rpx;
          height: 150rpx;
          border-radius: 10rpx;
          overflow: hidden;
          display: block;
        }
        text {
          position: absolute;
          right: -10rpx;
          top: -10rpx;
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          background: #FF3F42;
          font-size: 24rpx;
          color: #FFFFFF;
          text-align: center;
          line-height: 40rpx;
          display: block;
        }
      }
    }
  }
}

</style>
