import * as API from 'api/virtual/company_accounts';
import {
  isPhone,
  isPsw,
  isUserName
} from 'validate';

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    isUpdate: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default() {
        return null;
      }
    }
  },
  watch: {
    show(bool) {
      this.my_show = bool;
      if (bool) {
        if (this.data) {
          for (let k in this.data) {
            if (k in this.params) {
              this.params[k] = this.data[k];
            }
          }
        }
      }
    }
  },
  data() {
    return {
      my_show: false,
      loading: false,
      params: {
        company_id: null,
        username: null,
        nickname: null,
        mobile: null,
        password: null,
        current_password: null,
        confirm_password: null
      }
    };
  },
  methods: {
    ok() {
      if (!this.params.username) {
        this.$Message.info('请填写用户名');
        return;
      }

      if (!isUserName(this.params.username)) {
        this.$Message.info('用户名填写不合法');
        return;
      }

      if (!this.params.nickname) {
        this.$Message.info('请填写昵称');
        return;
      }

      if (!(/[\s\S]{2,32}/.test(this.params.nickname))) {
        this.$Message.info('昵称长度在2-32之间');
        return;
      }

      if (this.params.mobile && !isPhone(this.params.mobile)) {
        this.$Message.info('手机号填写不正确');
        return;
      }

      this.params.password = null;

      if (!this.isUpdate && !this.params.current_password) {
        this.$Message.info('请填写密码');
        return;
      }

      if (!this.isUpdate && !this.params.confirm_password) {
        this.$Message.info('请填写确认密码');
        return;
      }

      if (this.params.current_password && !isPsw(this.params.current_password)) {
        this.$Message.info('密码长度在6-18之间，只能包含字母、数字和下划线');
        return;
      }

      if (this.params.current_password != this.params.confirm_password) {
        this.$Message.info('密码与确认密码填写不一致');
        return;
      }

      if (this.params.current_password) {
        this.params.password = md5(this.params.current_password);
      }

      this.params.current_password = null;
      this.params.confirm_password = null;

      let data = new FormData();

      for (let k in this.params) {
        if (this.params[k] != undefined) {
          data.append(k, this.params[k]);
        }
      }

      if (this.isUpdate) {
        // 编辑
        API.update(data, this.data.id).then(res => {
          this.loading = false;
          if (res.code == 0) {
            this.$emit('update-success');
            this.$Message.success('更新成功');
            this.clear();
          }
        }).catch(err => {
          this.loading = false;
        });
      } else {
        // 添加
        API.create(data).then(res => {
          this.loading = false;
          if (res.code == 0) {
            this.$emit('add-success');
            this.$Message.success('添加成功');
            this.clear();
          }
        }).catch(err => {
          this.loading = false;
        });
      }
    },

    visibleChange(bool) {
      if (!bool) {
        this.$emit('update:show', false);
      }
    },

    clear() {
      for (let k in this.params) {
        this.params[k] = '';
      }

      this.my_show = false;
    }
  }
};
