<template>
  <div class="x-content" @click="onHideBox()">
    <div class="container">
      <div class="w-over-top">
        <div class="w-over">
          <h3>绑定企业</h3>
          <hr>
          <div class="bind-search">
            <div class="search">
              <input type="search" class="form-control" placeholder="输入企业名称查询"
                     @click="onShowBox()"
                     v-model="keyword">
              <i class="delete" @click="deleteSearch()"></i><i class="icon-search"></i>
              <ul class="list-unstyled" v-show="isShowBox" @mouseenter="hasBox=true" @mouseleave="hasBox=false">
                <li class="hit" v-text="keyword" @click="selectInfo(keyword)"></li>
                <template v-for="item in searchName.content">
                  <li class="hit" @click="selectInfo(item)" v-text="item"></li>
                </template>
              </ul>
            </div>
            <div class="search-info">
              <h3 id="name"><i></i><span v-text="enterPrise">企业名称</span><em v-if="hasRegister && searchInfo.length <= 0">(未注册)</em></h3>
              <hr/>
              <div class="row">
                <div class="col-xs-3"><p>管理员</p></div>
                <div class="col-xs-9"><span v-text="searchInfo.admin ? searchInfo.admin.vipName : ''"></span></div>
              </div>
              <div class="row">
                <div class="col-xs-3"><p>企业法人</p></div>
                <div class="col-xs-9"><span v-text="searchInfo.corporation"></span></div>
              </div>
              <div class="row">
                <div class="col-xs-3"><p>营业执照</p></div>
                <div class="col-xs-9"><span v-text="searchInfo.businessCode"></span></div>
              </div>
              <div class="row">
                <div class="col-xs-3"><p>企业地址</p></div>
                <div class="col-xs-9"><span v-text="searchInfo.regAddress"></span></div>
              </div>
            </div>
          </div>
          <template v-if="hasRegister && !searchInfo">
            <a name="register" href="/register/enterpriseRegistration">立即注册</a>
          </template>
          <template v-if="hasRegister && searchInfo && searchInfo.admin">
            <a href="javascript:void(0)" @click="bingEnterPrise()">申请绑定</a>
            <p id="applyText">管理员审核通过后成功绑定</p>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
      layout: 'main',
      data () {
        return {
          keyword: '',
          number: 5,
          isShowBox: false,
          hasBox: false,
          clickInput: false,
          enterPrise: '企业名称',
          hasRegister: false
        }
      },
      watch: {
        'keyword': function () {
          this.handleChange()
        }
      },
      created () {
        this.$store.dispatch('restForName')
        this.$store.dispatch('restForInfo')
      },
      computed: {
        searchName () {
          return this.$store.state.search.keywords.data.content ? this.$store.state.search.keywords.data.content : ''
        },
        searchInfo () {
          return this.$store.state.search.enterprise.data.content ? this.$store.state.search.enterprise.data.content : ''
        }
      },
      methods: {
        handleChange: function () {
          if (this.keyword) {
            this.isShowBox = true
            this.$store.dispatch('searchForName', {keyword: this.keyword, number: this.number})
          }
        },
        onShowBox: function () {
          if (this.isShowBox) {
            this.onHideBox()
          } else {
            this.isShowBox = true
            this.clickInput = true
          }
        },
        onHideBox: function () {
          if (!this.hasBox && !this.clickInput) {
            this.isShowBox = false
          } else {
            this.clickInput = false
          }
        },
        selectInfo: function (item) {
          this.isShowBox = false
          this.enterPrise = item
          this.hasRegister = true
          this.$store.dispatch('searchForInfo', { name: item })
          this.$store.dispatch('restForName')
        },
        deleteSearch: function () {
          this.keyword = ''
          this.enterPrise = '企业名称'
          this.hasRegister = false
          this.$store.dispatch('restForName')
          this.$store.dispatch('restForInfo')
        },
        bingEnterPrise: function () {
          let param = new FormData()
          param.append('userUU', this.$route.params.uuid)
          param.append('spaceUU', this.searchInfo.spaceUU)
          let config = {
            headers: {'Context-type': 'multipart/form-data'}
          }
          this.$http.post('/api/user/apply/bind', param, config)
            .then(res => {
              if (res.data.success) {
                this.$message({
                  message: '申请成功，请耐心等待管理员审批',
                  type: 'success'
                })
              }
              if (res.data.error) {
                this.$message.error(res.data.errMsg)
              }
            })
        }
      }
    }
</script>

<style scoped type="text/scss" lang="scss">
  .x-content {
    background: #eee;
  }
  .w-over-top {
    margin-top:50px;
    padding:50px 0;
    background: #fff;
  }
  .w-over-top .w-over>h3{
    font-size: 30px;
    text-align: center;
    color:#11b200;
    margin:0;
  }
  .w-over-top hr{
    border-top:1px solid #dcdcdc;
  }
  .w-over-top .w-over>p#applyText{
    text-align: center;
    font-size: 14px;
    color:#b5b5b5;
    margin-top:20px;
  }
  .w-over-top .w-over>strong{
    font-size: 14px;
    color:#b5b5b5;
    padding-top:20px;
  }
  .w-over-top .link-list .item{
    padding:15px 25px 0;
    width:170px;
    height:100px;
    box-shadow: 0 0 10px rgba(0,0,0,.5);
  }
  .w-over-top .link-list .item:hover{
    position:relative;
    top:-3px;
    cursor:pointer;
  }
  .w-over-top .link-list .item img{
    display:block;
    height:40px;
  }
  .w-over-top .link-list .item span{
    font-size: 16px;
    color:#333;
    font-weight: bold;
  }
  .w-over-top .link-list .item .caption{
    padding-top:12px;
    text-align: center;
  }
  .w-over-top .w-over>a{
    display: block;
    margin:0 auto;
    margin-top:60px;
    width:220px;
    height:42px;
    line-height: 42px;
    text-align: center;
    color:#fff;
    font-size: 14px;
    background: #0076ad;
    border-radius:5px;
  }

  .w-over-bottom {
    padding-top:35px;
    padding-bottom:50px;
    background: #fff;
  }
  .w-over-bottom .w-over>p{
    text-align: center;
    margin-bottom:25px;
    font-size: 16px;
    color:#646464;
  }
  .w-over-bottom .w-over>a{
    display:block;
    margin:0 auto;
    margin-bottom:45px;
    width:130px;
    height:32px;
    line-height: 30px;
    text-align: center;
    border-radius:15px;
    border:1px solid #0076ad;
    color:#0076ad;
    font-size: 14px;
  }
  .w-over-bottom .media{
    padding:10px;
  }
  .w-over-bottom .media-left{
    padding:10px 30px 10px 10px;
    width:190px;
    height:120px;
  }
  .w-over-bottom .media-left a{
    display:block;
    text-align: center;
    width:170px;
    height:100px;
    padding-top:20px;
    box-shadow: 0 0 10px rgba(0,0,0,.4);
  }
  .w-over-bottom .media-left a img{
    display:block;
    width:80px;
    height:30px;
    margin:0 auto;
    margin-bottom:16px;
  }
  .w-over-bottom .media-left a span{
    font-size: 16px;
    color:#333;
    font-weight: bold;
  }
  .w-over-bottom .media .media-body{
    padding-top:10px;
  }
  .w-over-bottom .media .media-body .media-heading{
    color:#333;
    font-weight: bold;
    font-size: 16px;
    margin-bottom:15px;
  }
  .w-over-bottom .media .media-body p{
    font-size: 14px;
    color:#8c8c8c;
    line-height: 25px;
    margin:0;
  }
  .w-over{
    width:880px;
    margin:0 auto;
  }
  .w-over-top .bind-search{
    width:500px;
    margin:0 auto;
  }
  .w-over-top .bind-search .search{
    position:relative;
    margin-top:40px;
    margin-bottom:40px;
  }
  .w-over-top .bind-search .search input{
    padding-right: 65px;
  }
  .w-over-top .bind-search .search i{
    position:absolute;
    top:7px;
    right:0;
    display:block;
    width:30px;
    height:30px;
    font-size: 20px;
    color: #cbcbcb;
  }
  .w-over-top .bind-search .search i.delete {
    right: 30px;
    top:9px;
    background: url('/img/over_account/delete.png')no-repeat;
  }
  .w-over-top .bind-search .search i.icon-search {
    right: 0;
    top:9px;
    background: url('/img/over_account/search.png')no-repeat;
  }
  .w-over-top .bind-search .search ul{
    position:absolute;
    top:100%;
    left:0;
    z-index:100;
    width:100%;
    max-height:226px;
    overflow-y:auto;
    border:1px solid #dcdcdc;
    background: #fff;
    padding:10px 0;
  }
  .w-over-top .bind-search .search li{
    height:34px;
    line-height: 34px;
    padding-left:12px;
    color:#010101;
    width:480px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .w-over-top .bind-search .search .list-name li em{
    color:#ff5c5c;
  }
  .w-over-top .bind-search .search li:hover{
    cursor:pointer;
    background: #f7f7f7;
  }
  .w-over-top .bind-search .search li.hit{
    color:#8c8c8c;
  }
  .w-over-top .bind-search .search-info{
    padding:20px;
    background: #f7f7f7;
    overflow: hidden;
  }
  .w-over-top .bind-search .search-info h3{
    font-size: 18px;
    color:#000;
    line-height: 20px;
    font-weight: bold;
  }
  .w-over-top .bind-search .search-info h3 i{
    display:inline-block;
    position:relative;
    top:5px;
    width:30px;
    height:20px;
    background:url('/img/over_account/business.png')no-repeat;
  }
  .w-over-top .bind-search .search-info h3 span{
    color:#787878;
    display:inline-block;
    vertical-align: bottom;
    width:310px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .w-over-top .bind-search .search-info h3 em{
    margin-left:10px;
    color:#e87405;
    font-style:normal;
  }
  .w-over-top .bind-search .search-info .row{
    margin-bottom:15px;
  }
  .w-over-top .bind-search .search-info .row p{
    font-weight: bold;
    font-size: 14px;
    color:#000;
    margin:0;
  }
  .w-over-top .bind-search .search-info .row span{
    font-size: 14px;
    color:#787878;
  }
</style>
