<template>
  <zj-page-layout :hasFooter="true">
    <view class="all-container">
      <view class="common-title">产品信息</view>
      <view class="product-container card">
        <view class="it" v-for="(item, index) in goodsList" :key="index">
          <view class="name"
            >({{ { INNER: '保内', OUTSIDE: '保外' }[item.repairFlag] }}){{ item.goodsName
            }}<text>×{{ item.num }}{{ item.goodsUnit }}</text></view
          >
          <view class="price">¥{{ (item.goodsAmount * item.num) | priceFilter }}</view>
        </view>
        <view class="total">
          <view class="text">合计：</view>
          <view class="price">¥{{ totalPrice | priceFilter }}</view>
        </view>
      </view>

      <block v-if="type == 'P'">
        <view class="common-title">服务费用</view>
        <view class="form-container card">
          <view class="item">
            <view class="label">其他费用</view>
            <u--input placeholder="请输入金额" inputAlign="right" v-model="otherPrice"></u--input>
          </view>
          <view class="item">
            <view class="label">服务费用</view>
            <u--input placeholder="请输入金额" inputAlign="right" v-model="servicePrice"></u--input>
          </view>
          <view class="total">
            <view class="text">合计：</view>
            <view class="price">¥{{ (Number(otherPrice) + Number(servicePrice)) | priceFilter }}</view>
          </view>
        </view>
      </block>

      <view class="common-title">备注</view>
      <view class="remark-container card">
        <u--textarea v-model="remark" placeholder="请输入内容" border="none" autoHeight></u--textarea>
      </view>
    </view>

    <template slot="footer">
      <view class="bottom-container">
        <view class="left">
          <view class="text">共{{ totalNum }}件，合计：</view>
          <view class="price"
            >¥{{ (Number(otherPrice) + Number(servicePrice) + Number(totalPrice)) | priceFilter }}</view
          >
        </view>
        <view class="btn">
          <u-button
            type="primary"
            text="确认订单"
            @click="submitData(1)"
            v-if="!wbId || (wbId && wbIsAllFee == 'NO' && wbPayType == 'SITE')"
          ></u-button>
          <u-button type="primary" text="提交申请" @click="submitData(2)" v-else></u-button>
        </view>
      </view>
    </template>
  </zj-page-layout>
</template>

<script>
export default {
  data() {
    return {
      type: 'M',
      userMobile: '',
      orderNo: '',
      salesType: '',
      brandId: '',
      brandName: '',
      categoryId: '',
      categoryName: '',
      websitId: '',
      websitName: '',
      wbId: null, // 维保工单
      wbIsAllFee: null, // 维保工单是否包含全部费用
      wbPayType: null, // 维保工单费用支付方式
      goodsList: [],
      remark: '',
      otherPrice: '',
      servicePrice: ''
    }
  },

  computed: {
    totalNum() {
      let val = 0
      this.goodsList.forEach(item => {
        val = val + item.num
      })
      return val
    },
    totalPrice() {
      let val = 0
      this.goodsList
        .filter(item => item.repairFlag == 'OUTSIDE')
        .forEach(item => {
          val = val + (item.num * (item.goodsAmount * 100)) / 100
        })
      return val
    }
  },

  onLoad() {
    let data = this.$getStorage('materialSaleDataZhiFu')
    if (data) {
      this.type = data.type
      this.userMobile = data.userMobile
      this.orderNo = data.orderNo
      this.categoryId = data.categoryId
      this.categoryName = data.categoryName
      this.websitId = data.websitId
      this.websitName = data.websitName
      this.wbId = data.wbId
      this.wbIsAllFee = data.wbIsAllFee
      this.wbPayType = data.wbPayType
      this.goodsList = data.goodsList
    }
  },

  onHide: function () {
    this.$removeStorage('materialSaleData')
  },

  methods: {
    submitData(type) {
      if (Number(this.otherPrice) + 0 !== Number(this.otherPrice)) return this.$toast(`其他费用需要为数字`)
      if (Number(this.servicePrice) + 0 !== Number(this.servicePrice)) return this.$toast(`服务费用需要为数字`)

      this.$api
        .postJson('/pay/buy', {
          settlementType: this.salesType,
          goodsType: this.type,
          remark: this.remark,
          userMobile: this.userMobile,
          workerOrderId: this.orderNo,
          categoryId: this.categoryId || '123',
          categoryName: this.categoryName || '123',
          websitId: this.websitId,
          websitName: this.websitName,
          workerOrderItems: this.goodsList,
          otherPrice: this.otherPrice,
          servicePrice: this.servicePrice
        })
        .then(res => {
          this.$navToPage(
            {
              url: `/packageMaterial/pages/newSale/pay?handleOrderId=${res.data}`
            },
            'redirectTo'
          )
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.all-container {
  padding: 0 20rpx;
}
.common-title {
  font-size: 32rpx;
  font-weight: 600;
  margin-top: 30rpx;
}
.card {
  @include zj-card;
  margin-top: 20rpx;
  padding: 30rpx;
}
.product-container {
  .it {
    display: flex;
    align-items: flex-end;
    margin-bottom: 20rpx;
    .name {
      flex: 1;
      font-size: 28rpx;
      font-weight: 500;
      text {
        color: $sec-font;
        margin-left: 12rpx;
        font-weight: normal;
      }
    }
    .price {
      margin-left: 30rpx;
      font-size: 28rpx;
      font-weight: 500;
    }
  }
  .total {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 30rpx;
    .text {
      font-size: 28rpx;
      color: $sec-font;
    }
    .price {
      font-size: 28rpx;
      color: $minor-color;
      font-weight: 500;
    }
  }
}

.remark-container {
  padding: 0;
  ::v-deep .u-textarea {
    padding: 20rpx 30rpx;
    min-height: 200rpx;
  }
}

.form-container {
  .item {
    height: 50rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 30rpx;
    &:first-child {
      margin-top: 0;
    }
    .label {
      margin-right: 30rpx;
      flex-shrink: 0;
      text {
        color: $sec-font;
        margin-left: 12rpx;
        font-weight: normal;
      }
    }
    .picker {
      display: flex;
      align-items: center;
      .placeholder {
        color: $sec-font;
      }
      .iconfont {
        margin-left: 12rpx;
        color: $sec-font;
      }
    }
    .value {
      flex: 1;
      text-align: right;
    }
  }
  .total {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 30rpx;
    .text {
      font-size: 28rpx;
      color: $sec-font;
    }
    .price {
      font-size: 28rpx;
      color: $minor-color;
      font-weight: 500;
    }
  }
}
.bottom-container {
  padding: 20rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    .text {
      font-size: 28rpx;
      color: $sec-font;
    }
    .price {
      font-size: 32rpx;
      color: $minor-color;
      font-weight: 500;
    }
  }
  .btn {
    width: 180rpx;
  }
}
</style>
