<template>
  <div id="login">
    <admin-frame topType="2" @back="back" :rightArrow="false" :usePage="false" :useNav="false">
      <template v-slot:info>
        <van-form @submit="onSubmit" label-width="4em">
          <van-field
            v-model="form.password"
            type="password"
            name="password"
            label="新密码"
            placeholder="请填写密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit">提交修改</van-button>
          </div>
        </van-form>
      </template>
    </admin-frame>
  </div>
</template>

<script>
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: personal } = createNamespacedHelpers('personal');
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  name: 'login',
  props: {},
  components: {
    adminFrame,
  },
  data: function () {
    return {
      form: {},
    };
  },
  async created() {},
  methods: {
    ...personal(['updatePassword']),
    ...adminLogin(['updatePwd']),
    async onSubmit(values) {
      if (this.user.role == '1' || this.user.role == '2') {
        let data = { id: this.user.id, passwd: values.password };
        let res = await this.updatePwd(data);
        if (this.$checkRes(res)) {
          let token = localStorage.removeItem('token');
          let openid = sessionStorage.removeItem('openid');
          if (token == undefined && openid == undefined) {
            this.$toast({ type: 'fail', message: '退出登录成功' });
            this.$router.push({ path: '/login', query: { path: '/account/index', type: '1' } });
          }
        } else {
          this.$toast({ type: `fail`, message: `${res.errmsg}` });
        }
      } else if (this.user.role == '3') {
        let data = { id: this.user.id, password: values.password };
        let res = await this.updatePassword(data);
        if (this.$checkRes(res)) {
          let token = localStorage.removeItem('token');
          let openid = sessionStorage.removeItem('openid');
          if (token == undefined && openid == undefined) {
            this.$toast({ type: 'fail', message: '退出登录成功' });
            this.$router.push({ path: '/login', query: { path: '/account/index', type: '1' } });
          }
        } else {
          this.$toast({ type: `fail`, message: `${res.errmsg}` });
        }
      }
    },
    back() {
      this.$router.push({ path: '/account/index' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped></style>
