<template>
  <!-- 个人中心 -->
  <div id="personalCenter">
    <el-form label-width="80px" style="margin-left: 20px">
      <el-form-item
        v-for="(item, index) in lists"
        :key="index"
        :label="item.label"
      >
        <span v-if="item.scope === 'password'">******</span>
        <span v-else>{{ formLabelAlign[item.prop] }}</span>
      </el-form-item>
    </el-form>
    <el-button
      type="success"
      size="small"
      style="margin-left: 100px"
      @click="changeUserInfo"
      >修改</el-button
    >
    <el-dialog
      :visible.sync="dialogVisible"
      width="560px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">修改</div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="close"/>
        </div>
      </div>
      <div>
        <el-form label-width="80px">
          <el-form-item
            v-for="(items, indexs) in lists"
            :key="indexs"
            :label="items.label"
          >
            <el-input
              :disabled="items.scope === 'user'"
              style="width: 280px"
              :placeholder="items.placeholder"
              v-model="bfdata[items.prop]"
            ></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">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formLabelAlign: {},
      dialogVisible: false,
      lists: [
        {
          label: "账号",
          prop: "userName",
          scope: "user",
        },
        {
          label: "账号名称",
          prop: "nickName",
          placeholder: "请输入账号名称",
        },
        {
          label: "密码",
          prop: "password",
          scope: "password",
          placeholder: "请输入新密码",
        },
        {
          label: "手机号",
          prop: "phonenumber",
          placeholder: "请输入新手机号",
        },
      ],
      bfdata: {},
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.formLabelAlign = {
        nickName: this.$store.state.userInfo[0].user.nickName,
        userName: this.$store.state.userInfo[0].user.userName,
        phonenumber: this.$store.state.userInfo[0].user.phonenumber,
      };
    },
    changeUserInfo() {
      var bf = JSON.stringify(this.formLabelAlign);
      this.bfdata = JSON.parse(bf);
      this.dialogVisible = true;
    },
    close() {
          this.dialogVisible = false;
          this.init();
    },
    submit() {
      var self = this
      var testPhone = /^1[3-9](\d){9}$/;
      if (this.bfdata.userName.length === 0) {
        this.$message.error("请输入账号名称");
        return;
      }
      if (!testPhone.test(this.bfdata.phonenumber)) {
        console.log(this.bfdata);
        this.$message.error("请输入正确手机号");
        return;
      }
      if (this.formLabelAlign.phonenumber === this.bfdata.phonenumber) {
        delete this.bfdata.phonenumber;
      }
      this.bfdata.userId = this.$store.state.userInfo[0].user.userId;
      this.$api.editsystemuser(this.bfdata).then((res) => {
        if (res.code === 200) {
          self.$message.success("修改成功!");
          self.dialogVisible = false;
          self.$store.dispatch('GetUserInfo').then(() => {
            self.init();
      })
        } else {
          self.dialogVisible = false;
          self.init();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
/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;
  }
}
</style>
