<template>
  <el-dialog :title="!dataForm.id ? '新增用户' : '修改用户'" :close-on-click-modal="false" :visible.sync="visible"
    @close="handlereset">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="120px">
      <el-form-item label="电话" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="电话" type="number"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!dataForm.id">
        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <div v-if="dataForm.id">
        <el-form-item label="年龄" prop="age">
          <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <!-- <el-input v-model="dataForm.sex" placeholder="性别"></el-input> -->
          <el-radio v-model="dataForm.sex" :label=2>保密</el-radio>
          <el-radio v-model="dataForm.sex" :label=1>男</el-radio>
          <el-radio v-model="dataForm.sex" :label=0>女</el-radio>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <!-- <el-input v-model="dataForm.avatar" placeholder="头像"></el-input> -->
          <el-upload :action="baseUrl + '/oss/fileoss'" list-type="picture-card" :on-preview="handlePictureCardPreview"
            :file-list="fileList" :on-success="handlePicSuccess" :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible" :append-to-body="true">
            <img width="100%" :src="dataForm.avatar" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item> -->
        <!-- <el-form-item label="会员类型" prop="vip">
        <el-input v-model="dataForm.vip" placeholder="0-普通 1-vip"></el-input>
      </el-form-item> -->
        <!-- <el-form-item label="vip 有效期" prop="vipValidtime">
        <el-date-picker
          v-model="dataForm.vipValidtime"
          type="datetime"
          placeholder="选择日期时间"
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
         <el-input v-model="dataForm.vipValidtime" placeholder="vip 有效期"></el-input>
      </el-form-item> -->
        <el-form-item label="花生币" prop="peanutCoin">
          <!-- <el-input v-model="dataForm.peanutCoin" placeholder="花生币"></el-input> -->
          <el-input-number disabled v-model="dataForm.peanutCoin" label="花生币"></el-input-number>
        </el-form-item>
        <!-- <el-form-item label="阅读时间" prop="readTime">
      <el-input v-model="dataForm.readTime" placeholder="阅读时间"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="最后登录时间"></el-input>
    </el-form-item> -->
        <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标记" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="删除标记"></el-input>
    </el-form-item> -->
        <el-form-item label="脉六权限" prop="pointPower">
          <el-radio v-model="dataForm.pointPower" :label='0'>否</el-radio>
          <el-radio v-model="dataForm.pointPower" :label='1'>是</el-radio>
        </el-form-item>
        <el-form-item label="时辰取穴权限" prop="tgdzPower">
          <el-radio v-model="dataForm.tgdzPower" :label='0'>否</el-radio>
          <el-radio v-model="dataForm.tgdzPower" :label='1'>是</el-radio>
        </el-form-item>
        <el-form-item label="五运六气权限" prop="wylqPower">
          <el-radio v-model="dataForm.wylqPower" :label='0'>否</el-radio>
          <el-radio v-model="dataForm.wylqPower" :label='1'>是</el-radio>
        </el-form-item>
        <el-form-item label="吴门验方权限" prop="prescriptAPower">
          <el-radio v-model="dataForm.prescriptAPower" :label='0'>否</el-radio>
          <el-radio v-model="dataForm.prescriptAPower" :label='1'>是</el-radio>
        </el-form-item>
        <el-form-item label="肿瘤古方权限" prop="prescriptBPower">
          <el-radio v-model="dataForm.prescriptBPower" :label='0'>否</el-radio>
          <el-radio v-model="dataForm.prescriptBPower" :label='1'>是</el-radio>
        </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handlereset">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import global from '../../common/common.vue' //引入共用组间
  export default {
    data() {
      return {
        baseUrl: global.baseUrl,
        visible: false,
        dialogVisible: false,
        fileList: [],
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        dataForm: {
          id: 0,
          name: '',
          age: '',
          email: '',
          sex: 2,
          avatar: '',
          nickname: '',
          tel: '',
          password: '',
          vip: '',
          vipValidtime: '',
          peanutCoin: '',
          readTime: '',
          lastLoginTime: '',
          createTime: '',
          updateTime: '',
          delFlag: '',
          pointPower: 0,
          tgdzPower: 0,
          wylqPower: 0,
          prescriptAPower: 0,
          prescriptBPower: 0,
        },
        dataRule: {
          password: [{
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          }],
          // tel: [{
          //   validator: function(rule, value, callback) {
          //     if (/^1[34578]\d{9}$/.test(value) == false) {
          //       callback(new Error("手机号格式错误"));
          //     } else {
          //       callback();
          //     }
          //   },
          //   trigger: "blur"
          // }],
          // email: [{
          //   validator: function(rule, value, callback) {
          //     if (/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/.test(value) == false) {
          //       callback(new Error("邮箱格式错误"));
          //     } else {
          //       callback();
          //     }
          //   },
          //   trigger: "blur"
          // }],
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/book/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.user.name
                this.dataForm.email = data.user.email
                this.dataForm.age = data.user.age
                this.dataForm.sex = data.user.sex
                this.dataForm.avatar = data.user.avatar
                this.dataForm.nickname = data.user.nickname
                this.dataForm.tel = data.user.tel
                this.dataForm.password = data.user.password
                this.dataForm.vip = data.user.vip
                this.dataForm.vipValidtime = data.user.vipValidtime
                this.dataForm.peanutCoin = data.user.peanutCoin
                this.dataForm.readTime = data.user.readTime
                this.dataForm.lastLoginTime = data.user.lastLoginTime
                this.dataForm.createTime = data.user.createTime
                this.dataForm.updateTime = data.user.updateTime
                this.dataForm.delFlag = data.user.delFlag
                this.dataForm.pointPower = data.user.pointPower
                this.dataForm.tgdzPower = data.user.tgdzPower
                this.dataForm.wylqPower = data.user.wylqPower
                this.dataForm.prescriptAPower = data.user.prescriptAPower
                this.dataForm.prescriptBPower = data.user.prescriptBPower
                if (data.user.avatar && data.user.avatar != "") {
                  var img = {
                    name: '',
                    url: data.user.avatar
                  }
                  var attr = []
                  attr.push(img)
                  this.fileList = attr
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
       // if (!this.dataForm.email && !this.dataForm.tel) {
       ///   this.$message.error("手机号或邮箱不为空！")
       //   return
       // }

        // if (this.dataForm.email) {
        //   if (/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(this.dataForm.email) == false){          
        //     this.$message.error("邮箱格式错误！")
        //     return
        //   }
        // }

        // if (this.dataForm.tel) {
        //   if (/^1[3456789]\d{9}$/.test(this.dataForm.tel) == false) {
        //     this.$message.error("手机格式错误！")
        //     return
        //   }
        // }

        let myData = {}
        if (!this.dataForm.id) {
          myData = {
            'name': this.dataForm.name,
            'tel': this.dataForm.tel,
          }
        } else {
          myData = {
            'id': this.dataForm.id,
            'name': this.dataForm.name,
            'email': this.dataForm.email,
            'age': this.dataForm.age,
            'sex': this.dataForm.sex,
            'avatar': this.dataForm.avatar,
            'nickname': this.dataForm.nickname,
            'tel': this.dataForm.tel,
            'pointPower': this.dataForm.pointPower,
            'tgdzPower': this.dataForm.tgdzPower,
            'wylqPower': this.dataForm.wylqPower,
            'prescriptAPower': this.dataForm.prescriptAPower,
            'prescriptBPower': this.dataForm.prescriptBPower,
            // 'password': this.dataForm.password,
            // 'vip': this.dataForm.vip,
            // 'vipValidtime': this.dataForm.vipValidtime,
            // 'peanutCoin': this.dataForm.peanutCoin,
            // 'readTime': this.dataForm.readTime,
            // 'lastLoginTime': this.dataForm.lastLoginTime,
            // 'createTime': this.dataForm.createTime,
            // 'updateTime': this.dataForm.updateTime,
            // 'delFlag': this.dataForm.delFlag
          }
        }

        console.log('myData', myData)

        this.$http({
          url: this.$http.adornUrl(`/book/user/${!this.dataForm.id ? 'save' : 'update'}`),
          method: 'post',
          data: this.$http.adornData(myData)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })


      },
      handlePictureCardPreview(file) {
        this.dataForm.avatar = file.url;
        this.dialogVisible = true;
      },
      handlePicSuccess(res, file) {
        if (res.msg == "success") {
          this.dataForm.avatar = res.url;
          this.$message.success("上传成功");
        } else {
          this.$message.error("上传失败");
        }
      },
      handleRemove(file) {
        this.dataForm.avatar = '';
      },
      handlereset() {
        // console.log('关闭了')
        this.$refs['dataForm'].resetFields()
        this.dataForm.password = ''
        this.fileList = [],
          this.visible = false
      },
    }
  }
</script>
