import { mapState } from "vuex";
import { POST, Delete } from "../../api/http";
import { getPrivateMsg } from "../../api/messageBoard"
import { $addStorageEvent } from '../../util/processingData'
import { getPrivateTicket, getReadTicket, GetUserAccount } from "../../api/help"
export default {
  data() {
    return {
      personalLoading: true,
      personalList: {},
      tableShow: false,
      workorderShow: false,
      title: "login.set",//设置按钮
      modifyShow: false,
      ConfirmNewPassword: "",
      dialogNewPassword: false,
      passwordForm: {
        oldPassword: "",
        email: "",
        password: "",
        resetPwdCode: "",
      },
      dialogPasswordVisible: false,
      passwordCode: {
        email: "",
      },
      dialogEmailVisible: false,
      resetEmail: {
        oldEmail: "",
        resetPwdCode: "",
        password: "",
      },
      resetNewEmail: {
        newEmail: "",
        code: "",
      },
      buttonLoading: false,
      tableData0: [],
      emailWarningShow: false,
      codeWarningShow: false,
      roleName: "",
      accountLevelList: [
        // {
        //   id:"registered",
        //   label:"注册用户",
        //   charges:"$0",          
        // },
        {//月度付费用户
          id: "advance",
          label: "login.monthlyPlay",
          charges: "10(USDT、USDC、BUSD)",
          amount: "10",
          value: 0,
          level:"L3",
          disabledValue:1,
        },
        {//年度付费用户
          id: "advance",
          label: "login.yearsPlay",
          charges: "100(USDT、USDC、BUSD)",
          amount: "100",
          value: 1,
          level:"L3",
          disabledValue:1,
        },
        {//vip年度付费用户
          id: "vip",
          label: "login.vipYearsPlay",
          charges: "500(USDT、USDC、BUSD)",
          amount: "500",
          value: 2,
          level:"L4",
          disabledValue:2,
        },

      ],
      activeName: "first1",
      dialogNewEmailVisible: false,
      tabelMsg: {//未读留言标签的数据
        status: 0
      },
      tableData1: [],
      tableData: [],
      tableData2: [],
      BTCMenu: [],
      ETHmenu: [],
      stableMenu: [],
      unreadNum: "",//未读留言数量
      //历史工单列表
      privateTicket: [{
        id: 1,
        time: "2022-09-01 16:00",
        type: "账户问题",
        title: "账户不能登录",
        status: "已解决",
        userName: "",//用户名
        date: "",//提交时间戳
      }],
      //已回复未读
      privateTicket1: [
        //   {
        //   id: 1,
        //   time: "2022-09-01 16:00",
        //   type: "账户问题",
        //   title: "账户不能登录",
        //   status: "已解决",
        //   userName: "",//用户名
        //   date: "",//提交时间戳
        // }


      ],
      //已处理
      privateTicket2: [{
        id: 1,
        time: "2022-09-01 16:00",
        type: "账户问题",
        title: "账户不能登录",
        status: "已解决",
        userName: "",//用户名
        date: "",//提交时间戳
      }],
      //已关闭
      privateTicket12: [{
        id: 1,
        time: "2022-09-01 16:00",
        type: "账户问题",
        title: "账户不能登录",
        status: "已解决",
        userName: "",//用户名
        date: "",//提交时间戳
      }],
      dialogVisible: false,
      WithdrawalList: {
        amount: "",
        emailCode: "",

      },
      options: [{
        value: 'USDT',
        label: 'USDT'
      }, {
        value: 'USDC',
        label: 'USDC'
      }, {
        value: 'BUSD',
        label: 'BUSD'
      }],
      value: "",
      codeBtn: {
        btnStatus: false,
        btnText: "login.getCode",
        btnType: "primary",
      },
      fraction: 3,
      max: 1,
      max1: 2,
      fraction1: 3,
      max2: 3,
      fraction2: 3,
      balances: {
        total: "",
        usdc: "",
        usdt: "",
        userId: "",
        userName: ""

      },
      role: {
        level: "",
        levelExpireDate: "",
        roleId: "",
        roleKey: "",
        roleName: "",
      },
      bthDisabled:"",
      level:""


    }
  },

  mounted() {

    console.log("更新检查");
    this.fetchPersonal()
    this.fetchPrivateTicket()
    this.fetchPrivateTicket4({ status: 4 })
    this.fetchPrivateTicket5({ status: 5 })
    this.fetchPrivateTicket12({ status: 12 })
    this.BTCMenu = JSON.parse(localStorage.getItem("BTCMenu"));
    this.ETHmenu = JSON.parse(localStorage.getItem("ETHmenu"));
    this.stableMenu = JSON.parse(localStorage.getItem("stableMenu"));
    //监听localstorage改变赋值
    window.addEventListener("setItem", () => {
      this.BTCMenu = JSON.parse(localStorage.getItem("BTCMenu"));
      this.ETHmenu = JSON.parse(localStorage.getItem("ETHmenu"));
      this.stableMenu = JSON.parse(localStorage.getItem("stableMenu"));
    });
    this.activeName=sessionStorage.getItem("personalActiveName")
    this.handelMenu(this.BTCMenu)
    this.fetchPrivateMsg0({ status: 0 }) //拿到未读留言数量
    this.fetchPrivateTicket5({ status: 5 })//拿到未读工单数量、
    this.fetchUserAccount() //查询账户余额
    console.log(this.activeName,"科技破防卡佛");
    if (!this.activeName) {
      this.activeName = `first1`
    }

  },
  methods: {

    //请求当前余额
    async fetchUserAccount(params) {
      const { data } = await GetUserAccount(params)
      this.balances = data.data

    },

    //请求未读留言标签数据 未处理留言
    async fetchPrivateMsg0(data) {
      const list = await getPrivateMsg(data)
      this.tableData0 = list.data.data.data
      this.tableData0.forEach(item => {
        item.userDate = `${item.userDate.split("T")[0]} ${item.userDate.split("T")[1].split(".")[0]}`
      })

      this.unreadNum = this.tableData0.length


    },
    //已处理留言
    async fetchPrivateMsg1(data) {
      const list = await getPrivateMsg(data)
      this.tableData1 = list.data.data.data
      this.tableData1.forEach(item => {
        item.userDate = `${item.userDate.split("T")[0]} ${item.userDate.split("T")[1].split(".")[0]}`
      })
    },
    //所有留言
    async fetchPrivateMsg2(data) {
      const list = await getPrivateMsg(data)
      this.tableData2 = list.data.data.data
      this.tableData2.forEach(item => {
        item.userDate = `${item.userDate.split("T")[0]} ${item.userDate.split("T")[1].split(".")[0]}`
      })
    },
    //请求个人资料
    async fetchPersonal() {
      this.personalLoading = true
      const { data } = await this.$API.getpersonal()
      this.personalList = data.data

      this.roleName = data.data.role.roleName
      this.level =data.data.role.level
      //判断等级 禁用相应按钮

      if (data.data.role.roleName == "月度付费用户") {
        this.bthDisabled = 0
      }else if(data.data.role.roleName == "年度付费用户"){
        this.bthDisabled = 1
      }else if(this.level== "L4"){
        this.bthDisabled = 2
      }else if(this.level== "L2" || this.level== "L1"){
        this.bthDisabled = -1
      }else{
        this.bthDisabled = 10
      }
      
      
      



   

      this.role = data.data.role
      if (this.role.levelExpireDate) {
        this.role.levelExpireDate = `${this.role.levelExpireDate.split(`T`)[0]} ${this.role.levelExpireDate.split(`T`)[1].split(`.`)[0]}`

      }
      this.personalList.loginDate = `${this.personalList.loginDate.split("T")[0]} ${this.personalList.loginDate.split("T")[1].split(".")[0]}`
      if (this.personalList.sex == "0") {
        this.personalList.sex = "男"
      } else if (this.personalList.sex == "1") {
        this.personalList.sex = "女"
      }

      this.passwordForm.email = this.personalList.email
      this.resetEmail.oldEmail = this.personalList.email
      this.personalLoading = false


    },
    //请求个人已提交工单列表 历史
    async fetchPrivateTicket(data) {
      const list = await getPrivateTicket(data)
      this.privateTicket = list.data.data
      this.privateTicket.forEach(item => {
        item.date = `${item.date.split(`T`)[0]} ${item.date.split(`T`)[1].split(`.`)[0]}`
      })
      console.log(list, "已提交工单列表");
    },
    //请求已回复未读
    async fetchPrivateTicket5(data) {
      const list = await getPrivateTicket(data)
      this.privateTicket1 = list.data.data
      this.privateTicket1.forEach(item => {
        item.date = `${item.date.split(`T`)[0]} ${item.date.split(`T`)[1].split(`.`)[0]}`
      })
      console.log(list, "已回复未读");

    },
    //请求已处理工单列表
    async fetchPrivateTicket4(data) {
      const list = await getPrivateTicket(data)
      this.privateTicket2 = list.data.data
      this.privateTicket2.forEach(item => {
        item.date = `${item.date.split(`T`)[0]} ${item.date.split(`T`)[1].split(`.`)[0]}`
      })
      console.log(list, "已处理工单列表");

    },
    //请求已关闭工单列表
    async fetchPrivateTicket12(data) {
      const list = await getPrivateTicket(data)
      this.privateTicket12 = list.data.data
      this.privateTicket12.forEach(item => {
        item.date = `${item.date.split(`T`)[0]} ${item.date.split(`T`)[1].split(`.`)[0]}`
      })
      console.log(list, "请求已关闭工单列表");

    },


    //请求已回复未读工单更改状态
    async fetchReadTicket(data) {
      const list = await getReadTicket(data)

    },
    //点击查看工单汇总标签
    handelSetPersonal() {
      this.title = "修改资料"
      this.modifyShow = true
    },
    //点击修改密码
    handelPassword() {
      this.dialogPasswordVisible = true
    },
    //调用登出接口
    async handelDeleter(address, data) {
      let list = await Delete(address);
      this.$addStorageEvent(1, "maskShow", false);
    },
    //请求重置密码验证码
    async fetchPasswordCode(address, email) {
      this.buttonLoading = true
      let list = await POST(address, email);
      //  let list = await this.$API.getpasswrodCode()
      if (list.data.code == 200) {
        this.$message({
          message: this.$t(`login.sendEmailCode`),
          type: "warning",
          customClass: "messageClass"
        });
        this.dialogNewPassword = true;
        this.dialogPasswordVisible = false;
      } else {
        this.$message({
          message: this.$t(`login.newPasswordCodeFail`),
          type: "warning",
          customClass: "messageClass"
        });
        this.dialogNewPassword = false;
        this.dialogPasswordVisible = true;
      }

      this.buttonLoading = false
    },
    //请求重置密码
    async fetchNewPassword(address, data) {
      let list = await POST(address, data);
      //  let list = await this.$API.getpasswrodCode()
      if (list.data.code == 200) {
        this.$message({
          message: this.$t(`login.newPasswordSuccess`),
          type: "warning",
          customClass: "messageClass"

        });
        //输入新密码弹窗隐藏 显示登录弹窗
        this.dialogNewPassword = false;
        this.dialogLoginVisible = true;
        this.passwordForm.email = "";
        this.passwordForm.password = "";
        this.passwordForm.resetPwdCode = "";
        this.handelDeleter("/auth/logout"); //重置成功退出账户 重新登陆
        this.$addStorageEvent(1, "token", "");
        localStorage.setItem("identity", JSON.stringify({}))
        this.$addStorageEvent(1, "permissionMask", JSON.stringify(false))

        // this.dialogPasswordVisible = false;
      } else {
        // this.$message({
        //   message: this.$t(`login.newPasswordCodeFail`),
        //   type: "warning",
        // });
        this.dialogNewPassword = true;
      }
    },
    //点击获取重置密码验证码 旧邮箱
    handelResetPasswordEmail() {
      this.buttonLoading = true
      if (!this.passwordForm.email) return;
      this.passwordCode.email = this.passwordForm.email;
      this.fetchPasswordCode(`/auth/resetPwdCode`, this.passwordCode);
      this.buttonLoading = false

    },
    //获取邮箱验证码的弹窗关闭的回调
    handelClose() {

      this.buttonLoading = false
    },
    //输入新密码，确认重置密码
    handelNewPassword() {
      // 验证两次密码是否一致
      if (this.passwordForm.password !== this.ConfirmNewPassword) {
        this.$message({
          message: this.$t(`login.inconsistency`),
          type: "warning",
          customClass: "messageClass"
        });
        return;
      }
      if (!this.passwordForm.resetPwdCode) {
        this.$message({
          message: this.$t(`login.CodeFailNull`),
          type: "warning",
          customClass: "messageClass"
        });
        return;
      }
      this.fetchNewPassword("/auth/resetPwd", this.passwordForm);
    },
    //点击修改邮箱
    handelModifyEmail() {
      this.dialogEmailVisible = true
    },
    //提交验证旧邮箱验证码
    handelOldEmail() {
      if (!this.resetEmail.oldEmail) {
        this.emailWarningShow = true
      }
      if (!this.resetEmail.resetPwdCode) {
        this.codeWarningShow = true
      }
      //填写新邮箱框展示
      this.dialogNewEmailVisible = true
      this.dialogEmailVisible = false

    },
    //邮箱地址输入失焦就请求发送邮箱验证码
    handelFocus() {

    },
    //重置邮箱弹窗关闭
    handelResetEmailClose() {

      this.emailWarningShow = false
      this.codeWarningShow = false
    },
    handelNewEmail() {

    },

    //点击标签页
    handleClick(tab, event) {
      // console.log(tab);
      sessionStorage.setItem("personalActiveName", tab.name);
      switch (tab.name) {
        case "second"://点击未读留言标签 请求未读留言数据        
          this.fetchPrivateMsg0({ status: 0 })
          this.fetchPrivateMsg1({ status: 1 })
          this.fetchPrivateMsg2({ status: 2 })
          break;
        case "third"://点击未读工单汇总 请求    
          this.fetchPrivateTicket()
          this.fetchPrivateTicket4({ status: 4 })
          this.fetchPrivateTicket5({ status: 5 })
          this.fetchPrivateTicket12({ status: 12 })
          break;

        default:
          break;
      }
    },

    //点击未读留言列表的某一行 跳转对应页面
    handelClickRow(row, column, event) {
      let obj = row
      if (this.$route.path !== "/") {
        this.$router.push({
          path: "/",
          // query: { res: params },
        });
      }

      let params
      //根据币种筛选不同导航数据对象
      switch (obj.coinType) {
        case "BTC":
          params = this.handelMenu(this.BTCMenu, obj.msgPage)
          this.$addStorageEvent(1, "radiusSrc", JSON.stringify(`https://studio.glassnode.com/images/crypto-icons/btc.png`));
          this.$addStorageEvent(1, "radius", `BTC`);
          break;
        case "ETH":

          params = this.handelMenu(this.ETHmenu, obj.msgPage)
          console.log(params, "跳转ETH");
          this.$addStorageEvent(1, "radiusSrc", JSON.stringify(`https://studio.glassnode.com/images/crypto-icons/eth.png`));
          this.$addStorageEvent(1, "radius", `ETH`);
          this.$addStorageEvent(1, "btcFlag", false);

          break;
        case "稳定币":
          params = this.handelMenu(this.stableMenu, obj.msgPage)
          this.$addStorageEvent(1, "radiusSrc", JSON.stringify(`https://studio.glassnode.com/images/crypto-icons/稳定币.png`));
          this.$addStorageEvent(1, "radius", `稳定币`);
          break;

        default:
          break;
      }



      //存数据到VUX
      this.$store.commit("selectMenu", params);
      this.$addStorageEvent(1, "show", "component");
      this.$addStorageEvent(1, "currentTabComponent", params.navigation);//组件名称 、open、币种  
      // this.$addStorageEvent(1, "params", JSON.stringify(params));
      this.$addStorageEvent(1, "open", params.id);
      this.$addStorageEvent(1, "introduce", params.introduce);
      this.$addStorageEvent(1, "interval", params.interval);
      //存入每个页面在导航显示的名称
      this.$addStorageEvent(1, "navName", JSON.stringify(params.label));





    },
    //点击已回复未读工单的某一行
    handelPrivateTicket1Row(row) {
      this.$addStorageEvent(1, "detailsID", row.id);
      this.$router.push({
        path: "/WorkOrderDetails",
        name: "WorkOrderDetails",
        params: row
      })
      this.fetchReadTicket({ id: row.id })//跳转工单详情页面 改变状态
    },
    //点击除了已回复未读工单的某一行
    handelPrivateTicket1Row2(row) {
      this.$addStorageEvent(1, "detailsID", row.id);
      this.$router.push({
        path: "/WorkOrderDetails",
        name: "WorkOrderDetails",
        params: row
      })

    },
    //处理页面名称显示
    formatPage(data) {

      return this.$t(data)
    },
    //根据点击的表格找到页面的obj
    handelMenu(data, label) {
      let menuObj
      let page = label
      let newMenu = []
      data.forEach(item => {
        newMenu.push(item.children)
      })
      let subMenu = []
      newMenu.forEach(item => {

        item.forEach(subItem => {
          if (subItem.children) {

            subItem.children.forEach(subItemCln => {
              subMenu.push(subItemCln)
            })
          } else {
            subMenu.push(subItem)
          }


        })
      })
      menuObj = subMenu.find(item => item.label == page)


      return menuObj
    },
    //点击账户升级
    handelUpgrade(data) {

      this.$router.push({
        path: "/AccountUpgrade",
        name: "AccountUpgrade",
        params: {
          amount: data,
        }
      })
    },
    //处理时间格式
    handelTime(time) {
      return `${time.split("T")[0]} ${time.split("T")[1].split(".")[0]}`
    },
    //点击跳转页面
    jumpRecharge(path) {
      this.$router.push(path)
    },



  }
}



