<template>
  <view class="page__view">
    
		<navbar leftClick @leftClick="$utils.navigateBack">
      <image class="icon-nav" src="@/pages_order/static/partner/icon-nav.png" mode="widthFix"></image>
    </navbar>

    <view class="main">
      <view class="advantage">
        <view class="flex advantage-content">
          <view class="flex advantage-item" v-for="(item, aIdx) in advantages" :key="aIdx">
            <image class="icon" src="@/static/image/icon-checkmark-circle-fill.png" mode="widthFix"></image>
            <view>{{ item }}</view>
          </view>
        </view>
      </view>

      <view class="card">
        <view class="card-header">申请合伙人</view>
        <view class="form">
          <uv-form 
            ref="form"
            :model="form"
            :rules="rules"
            errorType="toast"
          >
            <view class="form-item">
              <uv-form-item prop="name" :customStyle="formItemStyle">
                <view class="form-item-label">
                  <image class="icon" src="@/pages_order/static/icon-require.png" mode="widthFix"></image>
                  姓名
                </view>
                <view class="form-item-content">
                  <formInput v-model="form.name" :disabled="readonly"></formInput>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="phone" :customStyle="formItemStyle">
                <view class="form-item-label">
                  <image class="icon" src="@/pages_order/static/icon-require.png" mode="widthFix"></image>
                  电话
                </view>
                <view class="form-item-content">
                  <formInput v-model="form.phone" :disabled="readonly"></formInput>
                </view>
              </uv-form-item>
            </view>
            <!-- <view class="form-item">
              <uv-form-item prop="inviteId" :customStyle="formItemStyle">
                <view class="form-item-label">推荐人</view>
                <view class="form-item-content">
                  <formInput v-model="form.inviteId"></formInput>
                </view>
              </uv-form-item>
            </view> -->
          </uv-form>
        </view>
      </view>
    </view>

    <view class="bottom" v-if="!readonly">
      <view class="flex btn" @click="onSubmit">提交</view>
    </view>
  </view>
</template>

<script>
  import formInput from '@/pages_order/components/formInput.vue'

  export default {
    components: {
      formInput,
    },
    data() {
      return {
        advantages: ['收益高', '品类全', '到账快', '城市多'],
        form: {
          name: null,
          phone: null,
          // inviteId: null,
        },
        rules: {
          'name': {
            type: 'string',
            required: true,
            message: '请输入姓名',
          },
          'phone': {
            type: 'string',
            required: true,
            message: '请输入电话',
          },
        },
        formItemStyle: { padding: 0 },
        readonly: false,
      }
    },
    onLoad(arg) {
      const { readonly } = arg

      this.readonly = Boolean(parseInt(readonly))
      console.log('readonly', readonly, parseInt(readonly), this.readonly)

      this.fetchPartner()
    },
    methods: {
      async fetchPartner() {
        try {
          const result = await this.$fetch('queryPartner')

          const {
            name,
            phone,
          } = result || {}

          this.form = {
            name: name || null,
            phone: phone || null,
          }
        } catch (err) {

        }
      },
      async onSubmit() {
        try {
          await this.$refs.form.validate()

          const {
            name,
            phone,
          } = this.form

          const params = {
            name,
            phone,
          }

          await this.$fetch('applyPartner', params)

          uni.showToast({
            icon: 'success',
            title: '提交成功',
          });

          setTimeout(() => {
            this.$utils.navigateBack()
          }, 800)

        } catch (err) {
          console.log('onSave err', err)
        }
      },
    },
  }
</script>

<style scoped lang="scss">

  .page__view {
    min-height: 100vh;
    background: linear-gradient(to right, #21FEEC, #019AF9);

    /deep/ .nav-bar__view {
      position: fixed;
      top: 0;
      left: 0;
    }
    
    .icon-nav {
      width: 168rpx;
      height: auto;
    }
  }

  .main {
    // min-height: 100vh;
    // padding: calc(var(--status-bar-height) + 130rpx) 0 calc(120rpx + env(safe-area-inset-bottom)) 0;
    padding-top: calc(var(--status-bar-height) + 130rpx);
    box-sizing: border-box;
  }

  .advantage {
    padding: 0 40rpx 32rpx 40rpx;

    &-content {
      justify-content: space-between;
      padding: 16rpx;
      background: #1FB2FD99;
      border: 2rpx solid #FFFFFF4D;
      border-radius: 16rpx;
    }

    &-item {
      column-gap: 8rpx;
      padding-right: 16rpx;
      font-size: 26rpx;
      color: #FFFFFF;
      
      .icon {
        width: 40rpx;
        height: auto;
      }
    }
  }

  .card {
    width: 100%;
    // height: 100%;
    $advantage-height: 54px;
    // min-height: calc(100vh - #{$advantage-height} - (var(--status-bar-height) + 130rpx) - (120rpx + env(safe-area-inset-bottom)));
    min-height: calc(100vh - #{$advantage-height} - (var(--status-bar-height) + 130rpx));
    padding: 40rpx;
    box-sizing: border-box;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 1.4;
    background: linear-gradient(#DAF3FF, #FBFEFF 400rpx, #FBFEFF);
    border: 2rpx solid #FFFFFF;
    border-top-left-radius: 48rpx;
    border-top-right-radius: 48rpx;
    
    &-header {
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1.4;
      color: #191919;
    }
  }

  .form {
    &-item {
      margin-top: 32rpx;
      border-bottom: 2rpx solid #EEEEEE;

      &-label {
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 26rpx;
        line-height: 1.4;
        color: #181818;

        .icon {
          margin-right: 8rpx;
          width: 16rpx;
          height: auto;
        }
      }

      &-content {
      }
    }
  }

  .bottom {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100vw;
		background: #FFFFFF;
		box-sizing: border-box;
    padding: 32rpx 40rpx;
    padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);
    box-sizing: border-box;

    .btn {
      width: 100%;
      padding: 14rpx 0;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1.4;
      color: #FFFFFF;
      background: linear-gradient(to right, #21FEEC, #019AF9);
      border: 2rpx solid #00A9FF;
      border-radius: 41rpx;
    }

	}
</style>