<template>
  <div>
    <FormAccount ref="entinfo" :fminfo="entforminfo" style="height:100%">
      <template v-slot:default="slotProps">
        <Button @click="upentinfo">保存</Button>
      </template>
    </FormAccount>
    <br>
    <FormAccount ref="uinfo" :fminfo="forminfo">
      <template v-slot:default="slotProps">
        <Button @click="upuserinfo">保存</Button>
      </template>
    </FormAccount>
    <br>
    <FormAccount ref="pwdinfo" :fminfo="pwdinfo" :refvalidatemethod="validmethod">
      <template v-slot:default="slotProps">
        <Button @click="uppwdinfo">保存</Button>
      </template>
    </FormAccount>
  </div>
</template>
  <script>
import FormAccount from "../components/tables/forms/forms";
import { saveFormData } from "@/api/data";
export default {
  name: "accountinfo_page",
  data() {
    return {
      // curuser: this.deepclone(this.$store.state.user.userobj),
      forminfo: {
        name: "用户资料",
        main: [
          {
            title: "基本信息",
            key: "baseinfo",
            ctls: [
              {
                type: "input",
                label: "帐户名称",
                prop: "userName",
                placeHolder: "请输入帐户名",
                style: "",
                disabled: true,
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              },
              {
                type: "input",
                label: "昵称",
                prop: "nickName",
                placeHolder: "请输入昵称",
                style: "",
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              },
              {
                type: "input",
                label: "邮件地址",
                prop: "mail",
                placeHolder: "请输入邮件",
                style: "",
                rules: [
                  { required: true, message: "  ", trigger: "blur" },
                  {
                    type: "email",
                    message: "不正确的地址格式",
                    trigger: "blur"
                  }
                ]
              },
              {
                type: "upload",
                label: "头像",
                prop: "headUrl",
                placeHolder: "请上传头像",
                style: "",
                rules: []
              }
            ]
          }
        ],
        lists: []
      },
      pwdinfo: {
        name: "密码设置",
        main: [
          {
            title: "密码修改",
            key: "baseinfo",
            cols: 1,
            ctls: [
              {
                type: "password",
                label: "新设密码",
                prop: "onepassword",
                placeHolder: "请输入新密码",
                style: "",
                rules: [
                  { required: true, message: "  ", trigger: "blur" },
                  { validator: "validatex", trigger: "blur" }
                ]
              },
              {
                type: "password",
                label: "密码确认",
                prop: "repeatpassword",
                placeHolder: "请重新输入新密码",
                style: "",
                rules: [
                  { required: true, message: "  ", trigger: "blur" },
                  { validator: "validatex", trigger: "blur" }
                ]
              }
            ]
          }
        ],
        lists: []
      },
      entforminfo: {
        name: "企业资料",
        main: [
          {
            title: "基本信息",
            key: "baseinfo",
            ctls: [
              {
                type: "input",
                label: "名称",
                prop: "name",
                placeHolder: "请输入企业名称",
                style: "",
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              },
              {
                type: "input",
                label: "企业KEY",
                prop: "companykey",
                disabled: true,
                placeHolder: "请输入昵称",
                style: "",
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              },
              {
                type: "input",
                label: "电话",
                prop: "phone",
                placeHolder: "请输入昵称",
                style: "",
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              },
              {
                type: "input",
                label: "企业邮件",
                prop: "mail",
                placeHolder: "请输入邮件",
                style: "",
                rules: [
                  { required: true, message: "  ", trigger: "blur" },
                  {
                    type: "email",
                    message: "不正确的地址格式",
                    trigger: "blur"
                  }
                ]
              },
              {
                type: "input",
                label: "地址",
                prop: "address",
                placeHolder: "请输入邮件",
                style: "",
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              },
              {
                type: "upload",
                label: "营业执照",
                prop: "licenseUrl",
                placeHolder: "请上传头像",
                style: "",
                rules: [{ required: true, message: "  ", trigger: "blur" }]
              }
            ]
          }
        ],
        lists: []
      }
    };
  },
  components: {
    FormAccount
  },
  created() {},
  mounted() {
    this.$refs.entinfo.formModel = this.entdata;
    this.$refs.uinfo.formModel = this.userdata;
  },
  computed: {
    curuser() {
      return this.deepclone(this.$store.state.user.userobj);
    },
    entdata() {
      return this.curuser.company;
    },
    userdata() {
      return this.curuser;
    }
  },
  methods: {
    validmethod(rule, value, callback) {
      let rp = this.$refs.pwdinfo.formModel["repeatpassword"];
      let op = this.$refs.pwdinfo.formModel["onepassword"];
      console.log(rp, op);
      if (rp != op) {
        return callback(new Error("两次输入的密码不一致"));
      }
      return callback();
    },
    deepclone(objin) {
      let rtnstr = JSON.stringify(objin);
      return JSON.parse(rtnstr);
    },
    upuserinfo() {
      this.$refs.uinfo.validate(v => {
        if (v) {
          let d = this.$refs.uinfo.formModel;
          saveFormData("/web/auth/userCtl/update", d).then(res => {
            let rtn = res.data;
            console.log(rtn);
            if (rtn.status == 0) {
              let u = rtn.data;
              console.log(
                "........................................................................user.",
                u
              );
              this.$store.state.user.userobj = u;
              this.$Message.success("当前操作成功.");
            } else {
              this.$Message.error("当前操作失败，请重试或联系管理员.");
            }
          });
        }
      });
    },
    upentinfo() {
      this.$refs.entinfo.validate(v => {
        if (v) {
          let d = this.$refs.entinfo.formModel;
          saveFormData("/web/common/companyCtl/update", d).then(res => {
            let rtn = res.data;
            if (rtn.status == 0) {
              let cmp = rtn.data;
              console.log("............................cmp", cmp);
              this.$store.state.user.userobj.company = cmp;
              this.$Message.success("当前操作成功.");
            } else {
              this.$Message.error("当前操作失败，请重试或联系管理员.");
            }
          });
        }
      });
    },
    uppwdinfo() {
      this.$refs.pwdinfo.validate(v => {
        if (v) {
          let d = this.$refs.pwdinfo.formModel;
          saveFormData("/web/auth/userCtl/resetPassword", d).then(res => {
            let rtn = res.data;
            if (rtn.status == 0) {
              let cmp = rtn.data;
              this.$Message.success("当前操作成功.");
              this.$refs.pwdinfo.resetForm();
            } else {
              this.$Message.error("当前操作失败，请重试或联系管理员.");
            }
          });
        }
      });
    }
  }
};
</script>
  <style>
</style>
