<template>
  
  <view class="page__view">

		<navbar title="绑定申请" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

    <view class="main">

      <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="bindId" :customStyle="formItemStyle">
                <view class="form-item-label">绑定人ID</view>
                <view class="form-item-content">
                  <view class="flex search">
                    <uv-search 
                      v-model="keyword" 
                      placeholder="请输入" 
                      color="#181818"
                      bgColor="transparent" 
                      :showAction="true"
                      @custom="search" 
                      @search="search"
                    >
                    </uv-search>
                  </view>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item" v-for="item in list" :key="item.id">
              <view 
                :class="['flex', 'list-item', item.id === form.bindId ? 'is-active' : '']"
                @click="onSelect(item.id)"
              >
                <view class="avatar">
							    <image class="img" :src="item.headImage" mode="scaleToFill"></image>
                </view>
                <view>
                  <view class="title">{{ item.nickName }}</view>
                  <view class="desc">{{ `ID：${item.id}` }}</view>
                </view>
              </view>
            </view>
					</uv-form>
				</view>
			</view>

    </view>
  
    <view class="bottom">
      <button class="btn" @click="onSubmit">申请</button>
    </view>

  </view>
</template>

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

  export default {
    components: {
      formInput,
    },
    data() {
      return {
				keyword: '',
        list: [],
        form: {
          bindId: null,
        },
        rules: {
          'bindId': {
            type: 'string',
            required: true,
            message: '请选择绑定人',
          },
        },
        formItemStyle: { padding: 0 },
      }
    },
    methods: {
      async search() {
        try {
          const result = await this.$fetch('queryBindUser', { bindUserId: this.keyword })

          this.list = [result]
          this.form.bindId = result.id
        } catch (err) {

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

          const {
            bindId,
          } = this.form

          const params = {
            bindId,
            // userId: this.userInfo.id,
          }

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

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

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

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

<style lang="scss" scoped>

	.page__view {
		width: 100vw;
		min-height: 100vh;
    background: $uni-bg-color;
		position: relative;
		
		/deep/ .nav-bar__view {
			position: fixed;
			top: 0;
			left: 0;
		}
	}

	.main {
		padding: calc(var(--status-bar-height) + 160rpx) 32rpx calc(env(safe-area-inset-bottom) + 198rpx) 32rpx;
	}

	.card {
    min-height: calc(100vh - (var(--status-bar-height) + 160rpx) - calc(env(safe-area-inset-bottom) + 198rpx));
		padding: 40rpx 32rpx;
    box-sizing: border-box;
		background: #FFFFFF;
		border: 2rpx solid #FFFFFF;
		border-radius: 32rpx;

		&-header {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1.4;
			color: #252545;
		}
	}

	.form {
		padding: 8rpx 0 0 0;

		&-item {
      margin-top: 40rpx;
			border-bottom: 2rpx solid #EEEEEE;

			&:last-child {
				border: none;
			}

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

			&-content {
			}
		}
	}

  .search {
    padding: 16rpx 0 8rpx 0;
    border-bottom: 2rpx solid #EEEEEE;

    /deep/ .uv-search__content {
      padding: 0;
    }

    /deep/ .uv-search__content__icon {
      display: none;
    }

    /deep/ .uv-search__content__input {
      margin: 0;
    }

    /deep/ .uv-search__action {
      color: #00A9FF;
    }
  }

  .list-item {
    justify-content: flex-start;
    column-gap: 24rpx;
    padding: 10rpx;
    border: 2rpx solid #EEEEEE;
    border-radius: 24rpx;

    .avatar {
      width: 96rpx;
      height: 96rpx;
      border-radius: 50%;
      
      .img {
        width: 100%;
        height: 100%;
      }
    } 

    .title {
      font-size: 32rpx;
      font-weight: 600;
      color: #000000;
    }

    .desc {
      margin-top: 8rpx;
      font-size: 24rpx;
      color: #939393;
    }
    
    &.is-active {
      background: #E9F8FF;
      border-color: #00A9FF;
    }
  }

	.bottom {
		position: fixed;
		left: 0;
		bottom: 0;

		width: 100vw;
		// height: 200rpx;
		padding: 32rpx 40rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);
		background: #FFFFFF;
		box-sizing: border-box;

		.btn {
			width: 100%;
			padding: 14rpx 0;
			box-sizing: border-box;
			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>