<template>
  <div id="myProfile" v-if="userInfo.length">
    <div class="lisStys" v-for="(item, index) in lists" :key="index">
      <span class="leftSty">{{ item.label }}</span>
      <template v-if="item.prop === 'roles'">
        <span
          v-for="(items, indexs) in userInfo[0].user[item.prop]"
          :key="indexs"
          >{{ items.roleName
          }}{{
            indexs === userInfo[0].user[item.prop].length - 1 ? "" : "、"
          }}</span
        >
      </template>
      <span v-else-if="item.value === 2">******</span>
      <span v-else-if="item.prop === 'status'">{{
        userInfo[0].user[item.prop] == 1 ? "有效" : "失效"
      }}</span>
      <img
        class="imgBoxs"
        v-else-if="item.prop === 'avatar'"
        :src="$methodsTools.splitImgHost(userInfo[0].user[item.prop])"
      />
      <span v-else>{{ userInfo[0].user[item.prop] }}</span>
      <span
        v-if="item.value !== 6 && item.value !== 7 && item.value !== 0"
        class="btnsty"
        @click="openBoxs(userInfo[0].user[item.prop], item.value)"
        >修改</span
      >
      <label v-if="item.value === 0"
        ><span class="btnsty">修改</span
        ><input
          ref="file"
          type="file"
          style="display: none"
          @change="uploadImgs"
      /></label>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="560px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ changeName }}
        </div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="close" />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="110px"
          :model="listData"
          :rules="rules"
          ref="listData"
        >
          <el-form-item v-if="int === 1" label="账号名称" prop="userName">
            <el-input v-model="listData.userName"></el-input>
          </el-form-item>
          <div v-if="int === 2">
            <el-form-item label="原密码" prop="oldPassword">
              <el-input v-model="listData.oldPassword"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="listData.password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="againPassword">
              <el-input v-model="listData.againPassword"></el-input>
            </el-form-item>
          </div>
          <el-form-item v-if="int === 3" label="真实姓名" prop="nickName">
            <el-input v-model="listData.nickName"></el-input>
          </el-form-item>
          <el-form-item v-if="int === 4" label="手机号码" prop="phonenumber">
            <el-input v-model="listData.phonenumber"></el-input> </el-form-item
          ><el-form-item v-if="int === 5" label="邮箱" prop="email">
            <el-input v-model="listData.email"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit('listData')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      var testPass = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{12,20}$/;
      if (!value) {
        callback(new Error("请输入密码"));
      } else if (!testPass.test(value)) {
        callback(new Error("密码至少为12位的数字和字母混合"));
      } else {
        if (this.listData.againPassword != "") {
          this.$refs.listData.validateField("againPassword");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      var testPass = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{12,20}$/;
      if (!value) {
        callback(new Error("请再次输入密码"));
      } else if (!testPass.test(value)) {
        callback(new Error("密码至少为12位的数字和字母混合"));
      } else if (value !== this.listData.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      lists: [
        {
          label: "账号头像：",
          prop: "avatar",
          value: 0,
        },
        {
          label: "账号名称：",
          prop: "userName",
          value: 1,
        },
        {
          label: "账号密码：",
          value: 2,
        },
        {
          label: "真实姓名：",
          prop: "nickName",
          value: 3,
        },
        {
          label: "手机号码：",
          prop: "phonenumber",
          value: 4,
        },
        {
          label: "E-mail：",
          prop: "email",
          value: 5,
        },
        {
          label: "角色：",
          prop: "roles",
          value: 6,
        },
        {
          label: "状态：",
          prop: "status",
          value: 7,
        },
      ],
      int: 0,
      rules: {
        userName: [
          { required: true, message: "请输入账号名称", trigger: "blur" },
        ],
        oldPassword: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],
        // password: [
        //   { required: true, validator: validatePass, trigger: "blur" },
        // ],
        // againPassword: [
        //   { required: true, validator: validatePass2, trigger: "blur" },
        // ],
        nickName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" },
        ],
      },
      listData: {},
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
    changeName() {
      var names;
      switch (this.int) {
        case 1:
          names = "修改账号名称";
          break;
        case 2:
          names = "修改密码";
          break;
        case 3:
          names = "修改真实姓名";
          break;
        case 4:
          names = "修改手机号码";
          break;
        case 5:
          names = "修改邮箱";
          break;
        default:
          break;
      }
      return names;
    },
  },
  methods: {
    uploadImgs(e) {
      var self = this;
      var file = self.$refs.file[0].files[0];
      if (file.size > 2 * 1024 * 1024) {
        self.$message.error("图片不得大于2MB");
        return;
      }
      var type = self.$refs.file[0].value.toLowerCase().split(".").splice(-1);
      if (
        type[0] != "jpg" &&
        type[0] != "png" &&
        type[0] != "jpeg" &&
        type[0] != "gif"
      ) {
        self.$message.error("上传格式需为：.jpg/.png/.jpeg/gif");
        return;
      }
      this.$upload.upload(file, 0).then((res) => {
        self.listData.avatar = res;
        self.roleForm();
      });
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.roleForm();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    roleForm() {
      var self = this;
      this.listData.userId = this.userInfo[0].user.userId;
      this.listData.userName = this.userInfo[0].user.userName;
      this.$api.editUser(this.listData).then((res) => {
        this.$message.success("修改成功");
        if (this.int === 2) {
          self.$store.dispatch("outLogin").then(() => {});
          return;
        }
        this.$store.dispatch("fenleiCart");
        this.dialogVisible = false;
      });
    },
    //删除cookie (名称)
    delCookie(name) {
      var exp = new Date();
      exp.setTime(exp.getTime() - 1);
      var cval = cookie.getCookie(name);
      if (cval != null)
        document.cookie =
          name + "=" + ";expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    },
    close() {
      this.dialogVisible = false;
    },
    openBoxs(value, int) {
      this.int = int;
      switch (int) {
        case 3:
          this.listData = {
            nickName: value,
          };
          break;
        case 4:
          this.listData = {
            phonenumber: value,
          };
          break;
        case 5:
          this.listData = {
            email: value,
          };
          break;
        default:
          this.listData = {};
          break;
      }
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.lisStys {
  font-size: 14px;
  color: #333;
  margin-bottom: 6px;
  .leftSty {
    display: inline-block;
    width: 80px;
    text-align: end;
  }
  .btnsty {
    transition: all 0.2s;
    cursor: pointer;
    font-size: 12px;
    color: #a4a4a4;
    &:hover {
      color: skyblue;
    }
  }
}
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
.imgBoxs {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #fff;
}
</style>
