<template>
  <Page name="paySubmit" flank>
    <template #header>
      <view class="row">
        <view class="label">支付数量：</view>
        <view>合计运费：{{ form.totalFeeAmount }}，合计服务费：{{ form.totalServiceAmount }}，支付总额：{{ form.totalPayAmount }}</view>
      </view>
      <view class="row" style="justify-content: space-between; color: #999999">
        <view>服务费率：{{ form.serviceRate }}</view>
        <view>油卡返点率：{{ form.oilRate }}</view>
      </view>
    </template>
    <template #content>
      <view class="card" v-for="(item, key) in form.detail" :key="key">
        <view class="item">
          <view class="label"><render-dict dict="ONLINE_FREIGHT_ORDER_FEE_TYPE" :value="item.feeType"></render-dict></view>
          <view class="content"
            ><text class="amount">￥{{ item.feeAmount }}</text></view
          >
        </view>
        <view class="item">
          <view class="label">服务费</view>
          <view class="content"
            ><text class="amount">￥{{ item.serviceAmount }}</text></view
          >
        </view>
        <view class="item">
          <view class="label">支付总额</view>
          <view class="content"
            ><text class="amount">￥{{ item.payAmount }}</text></view
          >
        </view>
        <view class="item">
          <view class="label">承运司机</view>
          <view class="content">{{ item.driverName }}{{ item.driverMobile }}</view>
        </view>
        <view class="item">
          <view class="label">收款人</view>
          <view class="content">{{ item.recAccountName }}{{ item.recMobile }}</view>
        </view>
        <view class="item">
          <view class="label">承运车辆</view>
          <view class="content">{{ item.vehicleLicenseNum }}</view>
        </view>
        <view class="item">
          <view class="label">订单号</view>
          <view class="content" style="color: #0066ff">{{ item.orderCode }}</view>
        </view>
      </view>
    </template>
    <template #footer>
      <u-button :loading="loading" type="primary" @tap="pwdShow = true">继续支付</u-button>
    </template>
    <u-popup :show="pwdShow" :closeable="false" mode="center" :round="10">
      <view class="popup">
        <view class="title">输入密码</view>
        <view class="row">
          <u-input v-model="pwdStr" type="password" input-align="center" placeholder="请输入支付密码"></u-input>
        </view>
        <view class="but">
          <u-button @tap="pwdShow = false">取消</u-button>
          <u-button :loading="loading" type="primary" @tap="onSubmit">确定支付</u-button>
        </view>
      </view>
    </u-popup>
  </Page>
</template>
<script>
export default {
  name: 'paySubmit',
  data() {
    return {
      loading: false,
      pwdShow: false,
      pwdStr: '',
      selection: [],
      form: {
        oilRate: '',
        payCount: '',
        serviceRate: '',
        totalFeeAmount: '',
        totalPayAmount: '',
        totalServiceAmount: '',
        detail: [
          {
            customerOrderCode: '',
            driverMobile: '',
            driverName: '',
            feeAmount: '',
            feeType: '',
            orderCode: '',
            payAmount: '',
            payCode: '',
            recAccountName: '',
            recMobile: '',
            serviceAmount: '',
            vehicleLicenseNum: '',
          },
        ],
      },
    };
  },
  onLoad() {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('payPlanDate', data => {
      this.form = data.form || {};
      this.selection = data.selection || [];
    });
  },
  methods: {
    onSubmit() {
      if (this.loading) {
        return;
      }
      if (!this.pwdStr) {
        uni.showToast({ title: '支付密码不能为空' });
        return;
      }
      this.loading = true;
      uni.$u.api.paymentPlan
        .checkPayPassword({ payPassword: this.pwdStr })
        .then(res => {
          uni.$u.api.paymentPlan
            .pay(this.selection)
            .then(res => {
              this.pwdShow = false;
              uni.showToast({ title: '提交成功' });
              this.getOpenerEventChannel().emit('refreshData');
              setTimeout(() => uni.navigateBack(), 500);
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style lang="scss">
.page-paySubmit {
  &__header {
    padding: 0 !important;
    .row {
      margin: 10px;
      display: flex;
      .label {
        width: 140rpx;
      }
    }
  }
  &__content {
    .card {
      background-color: $color-white;
      color: $color-text;
      padding: $padding-base $padding-sm;
      margin-bottom: $padding-sm;
      border-radius: $radius-md;
      box-shadow: $shadow-normal;
      font-size: $font-md;
      .item {
        margin: $padding-sm 0;
        display: flex;
        justify-content: space-between;
        .label {
          width: 170upx;
          font-weight: 400;
          font-size: 28upx;
          color: #999999;
          line-height: 28upx;
          text-align: left;
          font-style: normal;
        }
        .content {
          font-weight: 400;
          font-size: 28upx;
          color: #2b2a27;
          line-height: 28upx;
          text-align: right;
          font-style: normal;
          .amount {
            color: #ff4f3b;
          }
          &--warning {
            color: #f52723;
            font-size: 20rpx;
            margin-left: 20upx;
            padding-left: 10upx;
            padding-right: 10upx;
            border-radius: 8upx;
            border: 2rpx solid #f52723;
          }
        }
      }
    }
  }
  &__footer {
    background-color: $color-white;
  }
  .popup {
    padding-left: $padding-md;
    padding-right: $padding-md;
    width: 70vw;
    .title {
      padding-top: $padding-xs;
      padding-bottom: $padding-xs;
      display: flex;
      align-content: center;
      justify-content: center;
      font-weight: 400;
      font-size: 32rpx;
      color: #2b2a27;
      line-height: 45px;
      text-align: left;
      font-style: normal;
    }
    .alert {
      color: orange;
      font-size: 24rpx;
      display: flex;
      padding-bottom: 20rpx;
    }
    .row {
      display: flex;
      padding: 10upx 0;
      .text {
        width: 130rpx;
      }
    }
    .but {
      margin-top: $padding-md;
      display: flex;
      gap: 20px;
    }
  }
}
</style>
