<template>
<view id="distribution">
    <view class="distribution">
        <view class="distributeconent">
            <view class="cardInfo selectName">
                <input type="text" placeholder="请输入姓名" @input="getName"></input>
            </view>
            <view class="cardInfo selectPhone">
                <input type="text" placeholder="请输入手机号码" @input="getPhone"></input>
            </view>
            <view class="cardInfo selectOther">
                <input type="text" placeholder="备注" @input="getInfo"></input>
            </view>
        </view>
        <view class="saveCard">
            <button :style="'background: ' + config.mainColor" @tap="checkValue">提交申请</button>
        </view>
    </view>
    <view class="home" @tap="jumpHome" :style="'background: ' + config.mainColor">
        <i class="iconfont icon--shouye"></i>
        <view>
            首页
        </view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      name: '',
      phone: '',
      info: '',
      message: '',
      config: ''
    };
  },

  onLoad() {
    // 第三方平台配置颜色
    var bgConfig = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: bgConfig
    });
  },

  components: {},
  props: {},
  methods: {
    getName(e) {
      this.setData({
        name: e.detail.value
      });
    },

    getPhone(e) {
      this.setData({
        phone: e.detail.value
      });
    },

    getInfo(e) {
      this.setData({
        info: e.detail.value
      });
    },

    jumpHome() {
      wx.switchTab({
        url: '/pages/index/index/index'
      });
    },

    checkValue(e) {
      var token = this.$cookieStorage.get('user_token'); //获取用户的token

      var message = '';

      if (!this.name) {
        message = '请输入您的姓名';
      } else if (!this.phone) {
        message = '请输入您的手机号码';
      } else if (this.phone != "" && !is.mobile(this.phone)) {
        message = '手机号码格式不正确';
      }

      if (message) {
        wx.showModal({
          content: message,
          showCancel: false
        });
        return;
      } else {
        wx.showLoading({
          title: '加载中',
          mask: true
        });
        this.$http.post({
          api: 'api/distribution/register',
          header: {
            Authorization: token
          },
          data: {
            name: this.name,
            mobile: this.phone,
            note: this.info
          }
        }).then(res => {
          if (res.statusCode == 200) {
            res = res.data;

            if (res.status) {
              wx.showModal({
                content: res.message,
                showCancel: false,
                success: res => {
                  if (res.confirm || !res.cancel && !res.confirm) {
                    wx.switchTab({
                      url: '/pages/user/personal/personal'
                    });
                  }
                }
              });
            } else {
              wx.showModal({
                content: res.message || '请求失败',
                showCancel: false
              });
            }
          } else {
            wx.showModal({
              content: '请求失败，请稍后重试',
              showCancel: false
            });
          }

          wx.hideLoading();
        }).catch(rej => {
          wx.showModal({
            content: '请求失败，请稍后重试',
            showCancel: false
          });
          wx.hideLoading();
        });
      }
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "applyDistribution";
</style>