<template>
  <div class="app-container">
    <el-row :gutter="20" v-if="user">
      <el-col :span="6" :xs="24">
        <el-card style="margin-bottom: 20px">
          <div slot="header" class="clearfix"><span>关于我</span></div>
          <div class="user-profile">
            <div class="box-center">
              <pan-thumb
                image="http://statics.evmiot.com/evue-logo.png"
                :height="'100px'"
                :width="'100px'"
                :hoverable="false"
                ><div>Hello</div>
                {{ user.role }}</pan-thumb
              >
            </div>
            <div class="box-center">
              <div class="user-name text-center">{{ user.name }}</div>
              <div class="user-role text-center text-muted">
                未知
              </div>
            </div>
          </div>
          <div class="user-bio">
            <div class="user-education user-bio-section">
              <div class="user-bio-section-header">
                <svg-icon icon-class="education" /><span>个性签名</span>
              </div>
              <div class="user-bio-section-body">
                <div class="text-muted">{{ user.sign | wrapperSign }}</div>
              </div>
            </div>
            <div class="user-skills user-bio-section">
              <div class="user-bio-section-header">
                <svg-icon icon-class="skill" /><span>所属公司</span>
              </div>
              <div class="user-bio-section-body">{{ user.company }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="账号信息" name="account">
              <el-form
                size="mini"
                :model="user"
                status-icon
                :rules="rules"
                ref="post"
                label-width="80px"
              >
                <el-form-item label="账号">
                  <el-col :md="8" :xs="24">
                    <el-input v-model.trim="currentValue.account" disabled />
                  </el-col>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                  <el-col :md="8" :xs="24">
                    <el-radio-group v-model="user.gender">
                      <el-radio :label="1">男</el-radio>
                      <el-radio :label="2">女</el-radio>
                    </el-radio-group>
                  </el-col>
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                  <el-col :md="8" :xs="24">
                    <el-date-picker
                      v-model="user.birthday"
                      type="date"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      placeholder="选择日期"
                    >
                    </el-date-picker>
                  </el-col>
                </el-form-item>
                <el-form-item label="手机" prop="contact">
                  <el-col :md="8" :xs="24">
                    <el-input v-model.trim="user.contact" />
                  </el-col>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                  <el-col :md="8" :xs="24">
                    <el-input v-model.trim="user.email" />
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('post')"
                    >更新资料</el-button
                  >
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="activity">
              <el-form size="mini" label-width="100px">
                <el-form-item label="原密码">
                  <el-col :md="8" :xs="24">
                    <el-input
                      v-model="form.password"
                      type="password"
                      autocomplete="off"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item label="新密码">
                  <el-col :md="8" :xs="24">
                    <el-input
                      v-model="form.newPassword"
                      type="password"
                      autocomplete="off"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item label="确认新密码">
                  <el-col :md="8" :xs="24">
                    <el-input
                      v-model="form.confirmPassword"
                      type="password"
                      autocomplete="off"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updatePassword"
                    >更新密码</el-button
                  >
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="接口密钥" name="apiKey">
              <el-form size="mini" label-width="100px">
                <el-form-item label="AccessKey">
                  <el-col :md="10" :xs="24">
                    <el-input v-model="user.uuid" type="text" autocomplete="off" disabled />
                  </el-col>
                  <el-col :md="4" :xs="24">
                      <el-button @click="copyAccessKey">复制</el-button>
                  </el-col>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { updateUser, updateUserPassword } from "@/api/index";
import PanThumb from "@/components/PanThumb";

export default {
  name: "Profile",
  components: { PanThumb },
  filters: {
    wrapperSign(sign) {
      return sign === "" || !sign ? "此人非常懒，一个字都不写" : sign;
    },
  },
  data() {
    return {
      user: {
        birthday: "",
        email: "",
        contact: "",
        hometown: "",
        gender: 1,
        uuid: "",
      },
      form: {
        password: "",
        newPassword: "",
        confirmPassword: "",
      },
      rules: {
        email: [
          {
            type: "string",
            required: false,
            message: "邮箱不能为空",
            trigger: "blur",
          },
        ],
        contact: [
          {
            type: "string",
            required: false,
            message: "手机不能为空",
            trigger: "blur",
          },
        ],
        hometown: [
          {
            type: "string",
            required: false,
            message: "邮箱不能为空",
            trigger: "blur",
          },
        ],
      },
      currentValue: {
        uuid: null,
        account: null
      },
      activeTab: "account",
    };
  },
  computed: {
    ...mapGetters(["name", "avatar", "role"]),
  },
  created() {
    this.getUser();
  },
  methods: {
    getUser() {
      const user = JSON.parse(window.sessionStorage.getItem("user"));
      if (user) {
        this.currentValue = user;
        this.user.birthday = user.birthday;
        this.user.email = user.email;
        this.user.contact = user.contact;
        this.user.hometown = user.hometown;
        this.user.gender = user.gender;
        this.user.uuid = user.uuid;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        let result = true;
        if (valid) {
          updateUser(this.currentValue.uuid, this.user)
            .then((res) => {
              this.user = res.data;
              this.$message({ type: "success", message: "更新成功" });
              const profile = Object.assign({}, this.currentValue, res.data);
              window.sessionStorage.setItem("user", JSON.stringify(profile));
            })
            .catch((err) => {
              this.$message.error(err.message);
            });
        } else {
          result = false;
        }
        this.dialogVisible = false;
        return result;
      });
    },
    copyAccessKey() {
        let oInput = document.createElement('input')
        oInput.value = this.user.uuid
        document.body.appendChild(oInput)
        oInput.select()
        document.execCommand("Copy")
        this.$message({ message: '复制成功', type: 'success' })
        oInput.remove()
    },
    updatePassword() {
      if (
        this.form.password.trim().length > 0 &&
        this.form.newPassword.trim().length > 0 &&
        this.form.confirmPassword.trim().length > 0
      ) {
        if (this.form.newPassword === this.form.confirmPassword) {
          if (this.form.newPassword.trim().length < 6) {
            return this.$message.error("新密码长度不能少于6位");
          }

          // if (!/\d/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含数字");
          // } // 如果用户输入的密码 包含了数字

          // if (!/[a-z]/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含小写字母");
          // } // 如果用户输入的密码 包含了小写的a到z

          // if (!/[A-Z]/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含大写字母");
          // } // 如果用户输入的密码 包含了大写的A到Z

          // if (!/_|\W/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含字符");
          // } // 如果是非数字 字母

          updateUserPassword({
            uuid: this.currentValue.uuid,
            password: this.form.password,
            newPassword: this.form.newPassword,
          })
            .then((res) => {
              console.log(res);
              this.$message({ type: "success", message: "密码更新成功" });
            })
            .catch((err) => {
              this.$message.error(err.message);
            });
        } else this.$message.error("新密码和确认新密码不一致");
      } else this.$message.error("原密码、新密码、确认新密码都不能为空");
    },
  },
};
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
