<template>
  <div>
    <van-nav-bar
      title="选择项目"      
      left-arrow
      @click-left="onClickLeft"
      
      style="background-color:#efefef;"
    />
    <van-radio-group v-model="radio">
        <div v-for="item in companys" style="margin-top:15px;">
            <div class="bgGray">{{item.companyName}}</div>
            <ul  v-for="item1 in projects.filter(function(item2,index,arr){return item2.companyName==item.companyName})">
                <li class="height" >
                    <van-radio :name="item1.projectId" style="float:left;" @click="routerTo(item1.projectName)"></van-radio>
                    <div style="float:left;padding-left:10px;"  >{{item1.projectName}}</div>
                 </li>
            </ul>
        </div>
    </van-radio-group>
  </div>
</template>

<script>
import {selectProject} from '@/common/api/loginApi.js'
export default {
  name: 'login',
  data () {
    return {
      companys: [],     //公司信息
      projects: [],     //项目信息
      radio:'',          //单选框
      projectname:''
    }
  },
    created:function(){
        console.info('created');
        //console.info(this.$util);
        this.$util.persistLogin(this);
        this.getProjectList();
    },
  methods: {
     getProjectList:function(){
        var that=this;
        var data={userCode:that.$store.state.data.userCode};
        console.info('data',data);
        selectProject(data).then((res)=>{
            console.info('selectProject',res);
            if(!res.result){
                that.$toast(res.msg);
                return;
            }
            res.data.companys.forEach(function(item,index,array){
                that.$set(that.companys,index,item);
            });
            res.data.projects.forEach(function(item,index,array){
                that.$set(that.projects,index,item);
            });
           
        });
    },
    // 单选按钮点击事件
    routerTo:function(projectname){
       console.info('routerto radio',this.radio);
       console.info('routerTo projectname',projectname);
       this.$store.commit('setProjectId',this.radio);   //给项目ID赋值
       this.$store.commit('setProjectName',projectname);   //给项目名赋值
       this.$router.push('/saleindex');
    },
    onClickLeft () {
      // this.$toast('返回')
      console.info('left')
      this.$router.go(-1);
    }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .bgGray{       
        background-color: #cfcfcf;
        font-size:15px;
        font-weight:900;
        height:30px;
        padding-top:10px;
        padding-bottom: 10px;
        text-align: left;
        padding-left:5px;
    }

    .height{
        height:20px;
        font-size:15px;
        padding-top:10px;
        padding-bottom: 10px;
        border-bottom: #cfcfcf solid 1px;
        text-align: left;
        padding-left:10px;
    }
</style>
