<template>
  <div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <!--店员修改-->
      <div class="common-form">会员详情</div>
      <el-form-item label="昵称" prop="nickName">
        <el-input class="max-w460" v-model="form.nickName" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="会员号" prop="user_no">
        <el-input class="max-w460" v-model="form.user_no" placeholder="请输入会员号"></el-input>
      </el-form-item>
      <el-form-item label="头像">
        <el-row>
          <el-button icon="el-icon-picture-outline" @click="openUpload('avatarUrl')">选择图片</el-button>
          <div v-if="form.avatarUrl!=''" class="img">
            <img :src="form.avatarUrl" width="100" height="100" />
          </div>
        </el-row>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
          <el-radio :label="0">未知</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile" :rules="[{required: true,message: ' '}]">
        <el-input class="max-w460" v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="会员等级" prop="grade_id">
        <el-select v-model="form.grade_id" placeholder="会员等级">
          <el-option :label="item.name" :value="item.grade_id" v-for="(item,index) in gradelist" :key='index'></el-option>
        </el-select>
      </el-form-item>
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>
    </el-form>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">上传图片</Upload>
  </div>
</template>

<script>
  import UserApi from '@/api/user.js';
  import Upload from '@/components/file/Upload';
  import {
    formatModel
  } from '@/utils/base.js'
  export default {
    components: {
      /*上传组件*/
      Upload,
    },
    data() {
      return {
        form: {
          user_id: 0,
          nickName: '',
          avatarUrl: '',
          gender: '',
          mobile: '',
          user_no: '',
          grade_id:'',
        },
        /*等级列表*/
        gradelist: [],
        loading: false,
        /*是否上传图片*/
        isupload: false,
      };
    },
    created() {
      this.form.user_id = this.$route.query.user_id;
      this.getParams()
    },

    methods: {
      getParams() {
        let self = this;
        // 取到路由带过来的参数
        UserApi.getuser({
            user_id: self.form.user_id
          }, true)
          .then(data => {
            // 将数据放在当前组件的数据内
            self.form = formatModel(self.form,data.data.model);
            self.form.remobile = data.data.model.rephone;
            self.gradelist = data.data.grade;
            // self.areaList = data.data.regionData;
          })
          .catch(error => {

          });

      },
      onSubmit() {
        let self = this;
        let params = self.form;
        self.loading = true;
        UserApi.edituser(params, true)
          .then(data => {
            self.$message({
              message: '恭喜你，修改成功',
              type: 'success'
            });
            self.$router.push('/user/user/index');
          })
          .catch(error => {
            self.loading = false;
          });
      },
      //查找会员
      Member() {
        let self = this;
        let params ={};
        params.rephone = self.form.rephone;
        if(params.rephone == ''){
          self.loading = false;
          return false;
        }
        if(params.rephone == self.form.mobile){
          self.loading = false;
          self.$message.error('不能指定自己为推荐人');
          self.form.rephone   = self.form.remobile;
          return false;
        }
        UserApi.UserMember(params, true)
        .then(data => {
          self.form.rephone    = data.data.mobile;
          self.form.reuser_no  = data.data.user_no;
          self.form.renickName = data.data.nickName;
          return false;
        })
        .catch(error => {
          self.form.rephone   = self.form.remobile;
        });
      },
      /*上传*/
      openUpload(e) {
        this.type = e;
        this.isupload = true;
      },

      /*获取图片*/
      returnImgsFunc(e) {
        if (e != null && e.length > 0) {
          if (this.type == 'avatarUrl') {
            this.form.avatarUrl = e[0].file_path;
          }
        }
        this.isupload = false;
      },
      /*取消*/
      cancelFunc() {
        this.$router.back(-1);
      },
      /*初始化城市id*/
      initCity() {
        this.form.city_id = ''
      },
      /*初始化区id*/
      initRegion() {
        this.form.region_id = ''
      },

    }

  };
</script>

<style lang="scss" scoped>
  .basic-setting-content {}

  .product-add {
    padding-bottom: 50px;
  }
</style>
