<commit_before><template>
  <div class="login-wrap">
      <div class="login-box">
          <i-form ref="form" :model="user" :rules="relues">
            <FormItem prop="name">
              <!-- <i-input type="text" v-model="user.name" placeholder="请输入用户名"></i-input> -->
              <i-select>

              </i-select>
            </FormItem>

            <FormItem prop="pwd">
              <i-input type="password" v-model="user.pwd" placeholder="请输入密码"></i-input>
            </FormItem>
            <FormItem class="text-center">
              <i-button type="primary" @click="login" style="width:100%">登录</i-button>
            </FormItem>
            <FormItem class="text-center login-other">
              <i-button type="text" @click="signup">注册</i-button>
              <i-button type="text" @click="forgetPwd">忘记密码</i-button>
            </FormItem>
          </i-form>
      </div>
  </div>
</template>

<script>
import Form from 'iview/src/components/form/form';
import FormItem from 'iview/src/components/form/form-item';
import Input from 'iview/src/components/input/input';
import Button from 'iview/src/components/button/button';
import { Select, Option } from 'iview/src/components/select';
import AV from 'leancloud-storage';
import userApi from '@/api/user';

export default {
  name: 'login',
  components: {
    'i-form': Form,
    FormItem,
    'i-input': Input,
    'i-button': Button,
    'i-select': Select,
    'i-option': Option
  },
  data() {
    return {
      user: {
        name: '',
        pwd: ''
      },
      relues: {
        name: [
          {
            required: true,
            message: '请填写姓名',
            trigger: 'blur'
          }
        ],
        pwd: [
          {
            required: true,
            message: '请填写密码',
            trigger: 'blur'
          }
        ]
      }
    };
  },

  created() {
    let user = AV.User.current();
    if (user !== null) {
      this.$router.push(
        user.attributes.groupName ? { name: 'input' } : { name: 'summary' }
      );
    }
  },

  methods: {
    login() {
      this.$refs.form.validate(isValidated => {
        userApi.logIn(this.user.name, this.user.pwd).then(user => {
          console.log(user);
          if (user.attributes.groupName) {
            this.$router.push('/main/input');
          } else {
            this.$router.push('/main/summary');
          }
        });
      });
    },
    signup() {<commit_msg>新增选择登录<commit_after><template>
  <div class="login-wrap">
      <div class="login-box">
          <i-form ref="form" :model="user">

            <Form-item >
              <i-select v-model="groupIndex" style="margin-bottom:10px;">
                <!-- <i-option value="-1">请选择所在小组</i-option> -->
                <i-option v-for="item in groups" :value="item.index">{{ item.name }}</i-option>
              </i-select>
            </Form-item>

            <Form-item>
              <i-select v-model="user.name" style="margin-bottom:10px;">
                <!-- <i-option value="-1">请选择所在小组</i-option> -->
                <i-option v-for="item in group.member" :value="item.name">{{ item.name }}</i-option>
              </i-select>
            </Form-item>

            <FormItem prop="pwd">
              <i-input type="password" v-model="user.pwd" placeholder="请输入密码"></i-input>
            </FormItem>
            <FormItem class="text-center">
              <i-button type="primary" @click="login" style="width:100%">登录</i-button>
            </FormItem>
            <FormItem class="text-center login-other">
              <i-button type="text" @click="signup">注册</i-button>
              <i-button type="text" @click="forgetPwd">忘记密码</i-button>
            </FormItem>
          </i-form>
      </div>
  </div>
</template>

<script>
import Form from 'iview/src/components/form/form';
import FormItem from 'iview/src/components/form/form-item';
import Input from 'iview/src/components/input/input';
import Button from 'iview/src/components/button/button';
import { Select, Option } from 'iview/src/components/select';
import AV from 'leancloud-storage';
import api from '@/api/index';
import Promise from 'bluebird';

function getAllUser() {
  return Promise.all([
    api.getData('Group', false, {
      sort: 'asc',
      field: 'index'
    }),
    api.getAllUser()
  ]).then(result => {
    let groups = [];
    let users = [];

    result[0].forEach(item => {
      groups.push({
        id: item.id,
        name: item.attributes.name,
        index: item.attributes.index,
        member: []
      });
    });

    result[1].forEach(item => {
      let i = users.push({
        id: item.id,
        name: item.attributes.username
      });

      groups[item.attributes.groupIndex].member.push(users[i - 1]);
    });

    return {
      users,
      groups
    };
  });
}

export default {
  name: 'login',
  components: {
    'i-form': Form,
    FormItem,
    'i-input': Input,
    'i-button': Button,
    'i-select': Select,
    'i-option': Option
  },
  data() {
    return {
      user: {
        name: localStorage.getItem('localUserName') || '',
        pwd: ''
      },
      groups: [],
      groupIndex: 0,
      users: []
    };
  },
  computed: {
    group() {
      return this.groups[this.groupIndex] || {};
    }
  },
  watch: {
    groupIndex() {
      if (this.group.member) {
        this.user.name = this.group.member[0].name;
      }
    }
  },

  created() {
    // 已经登录则自动登录并跳转
    let user = AV.User.current();
    if (user !== null) {
      this.$router.push(
        user.attributes.groupName ? { name: 'input' } : { name: 'summary' }
      );
    } else {
      getAllUser().then(data => {
        this.$set(this, 'groups', data.groups);
        this.$set(this, 'users', data.users);
        this.groupIndex =
          parseInt(localStorage.getItem('localGroupIndex'), 10) || 0;
        this.user.name = this.group.member[0].name;
      });
    }
  },

  methods: {
    login() {
      this.$refs.form.validate(isValidated => {
        localStorage.setItem('localGroupIndex', this.groupIndex);
        localStorage.setItem('localUserName', this.user.name);
        api.logIn(this.user.name, this.user.pwd).then(user => {
          console.log(user);
          if (user.attributes.groupName) {
            this.$router.push('/main/input');
          } else {
            this.$router.push('/main/summary');
          }
        });
      });
    },
    signup() {
{'fraction_non_alphanumeric': 0.1758412156923554, 'fraction_numerical': 0.003256318809117693, 'mean_word_length': 1.8910802330793366, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2111272', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2122, 'n_words': 355}