<template>
  <sail-page type="realNameCheck" title="绑定房">
    <div slot="content">
      <!-- <p>"</p> -->
        <van-nav-bar
          title="绑定房"
          left-arrow
          :right-text="loginInfo.zhName || lastPage ? '' : '跳过'"
          @click-left="back"
          @click-right="jump"
        />
        <p class="real-desc">进行实名认证可直接绑定房源</p>
        <div class="real-form">
            <div class="weui-cells zero-top">
              <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
                    <div class="weui-cell__bd">
                        <input ref="name" :value="loginInfo.zhName" :disabled="loginInfo.zhName" class="weui-input" type="text" placeholder="请输入姓名">
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">身份证号</label></div>
                    <div class="weui-cell__bd">
                        <input ref="code" :value="loginInfo.cardCode" :disabled="loginInfo.cardCode" class="weui-input" placeholder="请输入身份证号码">
                    </div>
                </div>
            </div>
        </div>
        <van-button :loading="isLoading" size="large" class="submit-btn" @click="submit">确定</van-button>
    </div>
  </sail-page>
</template>

<script>
function isEmptyObject(e) {
  var t;
  for (t in e)
    return !1;
  return !0;
}
import sailPage from "@/components/sail-page.vue";
import { mapState } from "vuex";
import { Toast } from "vant";
export default {
  components: {
    sailPage
  },
  computed: {
    ...mapState({
      loginInfo: state => state.loginModule.loginInfo
    }),
    lastPage() {
      if(localStorage.getItem("lastPage").indexOf("MyPage") > -1) {
        return true;
      }
      return false;
    }
  },
  mounted() {
    
  },
  data: function() {
    return {
      isLoading: false,
      userInfo: {
        userName: "",
        cardCode: ""
      }
    };
  },
  methods: {
    back() {
      this.$router.back();
    },
    jump() {
      this.$router.push({ path: "/MyPage" });
    },
    submit() {
      let name = this.$refs.name.value;
      let code = this.$refs.code.value;
      //
      if(!name ) {
        Toast.fail({
          duration: 1000,
          message: "请填写姓名！"
        });
        return false; 
      }
      if(!code || code.length !== 18) {
        Toast.fail({
          duration: 1000,
          message: "身份证号格式错误，请重填！"
        });
        return false; 
      }
      this.isLoading = true;
      
      this.userInfo = {
        userName: name,
        cardCode: code
      };
      this.$store.dispatch("bindHouseModule/bindHouseFirst", this.userInfo).then(res => {
        //
        //
        this.isLoading = false;
        if(res) {
          res.Toast.success({
            duration: 1000,
            message: "实名认证成功"
          });
          this.$store.commit("loginModule/INIT_LOGIN_INFO",res.data.data.loginInfo);
          if(isEmptyObject(res.data.data.houseInfo)) {
            this.$router.push({ path: "/switchBindHouse" }); // -> 编程式跳转
            return;
          }
          this.$store.commit("bindHouseModule/INIT_BIND_HOUSE_INFO", res.data.data);
          this.$router.push({path: "/autoBindHouse"});
          //
        }
      });
    }
  }
};
</script>
<style lang="less">

</style>
<style lang="less" scoped>
.real-desc {
  text-align: left;
  padding-left: 11px;
  font-size: 11px;
  margin-top: 11.5px;
  margin-bottom: 11.5px;
}
</style>1
