<template>
  <view class="repairEntrance">
    <view class="info page_padding">
      <view class="info_text">
        <view class="text_left text_justify">您还没有绑定工号，请填写您的工号进行绑定。</view>
      </view>
      <view class="info_form">
        <view class="info_form_item">
          <label for="account" class="label"><text class="required newicon newicon-bitian"></text>工号:</label>
          <uni-easyinput class="flex1" v-model="postData.account" placeholder="请输入工号" focus
            :styles="{borderColor: !postData.account.trim() && isSubmit ? '#f00' : ''}" :primaryColor="primaryColor" />
        </view>
      </view>
    </view>
    <view class="foot_common_btns">
      <button @click="bind" type="default" class="primaryButton btn">绑定</button>
    </view>
  </view>
</template>

<script setup>
  import { onLoad } from '@dcloudio/uni-app'
  import { api_bindAccount } from "@/http/api.js"
  import { defaultColor } from '@/static/js/theme.js'
  import { reactive, ref } from 'vue'
  import { useWechatAuth } from '@/share/useWechatAuth.js'
  import { useSetTitle } from '@/share/useSetTitle.js'
  
  useSetTitle()
  const { wechatAuth } = useWechatAuth()

  // 数据
  const postData = reactive({
    account: '',
  })

  // 传参
  const options = reactive({
    wechat: '',
  })

  // easyinput颜色
  const primaryColor = ref(defaultColor)

  // 是否提交
  const isSubmit = ref(false)

  /**
   * 绑定
   */
  function bind() {
    isSubmit.value = true;
    if(!postData.account.trim()){
      uni.showToast({
      	icon: 'none',
        title: '请填写工号'
      });
      return;
    }
    bindFn();
    
  }
  
  /**
   * 绑定工号
   */
  function bindFn(type) {
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    api_bindAccount({
      account: postData.account,
      // weChatAccount: options.wechat,
      type: type || undefined,
    }).then(res => {
      uni.hideLoading();
      if (res.status == 200) {
        if(type === undefined){
          // 查询
          uni.showModal({
            title: '提示',
            content: `您输入的工号为“${postData.account}”对应姓名“${res ? res.userName : ''}”，您确认绑定吗？`,
            confirmColor: defaultColor,
            confirmText: '确认',
            success: function(res) {
              if (res.confirm) {
                bindFn('bind');
              }
            }
          });
        }else{
          // 绑定
          wechatAuth();
        }
      }  else if (res.status == 500) {
        //已被绑定
        uni.showModal({
          title: '提示',
          content: res.data,
          showCancel: false,
          confirmColor: defaultColor,
          confirmText: '取消',
        });
      } else {
        uni.showToast({
          icon: 'none',
          title: res.data || '请求数据失败！'
        });
      }
    })
  }

  onLoad((option) => {
    console.log(option);
    options.wechat = option.wechat;
  })
</script>

<style lang="scss" scoped>
  .repairEntrance {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;

    .info {
      .info_text {
        line-height: 56rpx;
        font-size: 34rpx;
        padding: 90rpx 0 58rpx;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .info_form {
        .info_form_item {
          height: 86rpx;
          display: flex;
          align-items: center;
        }
      }
    }
  }
</style>