<template>
  <div class="details-margin-top-120 register">
    <div class="register-body">
      <layout-register :headerTitle="'忘记密码'">
        <template v-slot:left>
          <slide-list :active="active" :list="FilterJson.list"></slide-list>
        </template>
        <template v-slot:right>
          <PageHeader :title="FilterJson[active].header" @goback="goback" />
          <z-form
            :formConfig="FilterJson[active]"
            ref="form"
            :defaultValue="FilterJson[active].ruleForm"
            @on-action="actionHandle"
          >
            <template v-slot:submit>
              <button-list :list="FilterJson[active].button" @buttnClick="buttnClick" />
            </template>
          </z-form>
        </template>
      </layout-register>
    </div>
  </div>
</template>

<script>
import LayoutRegister from '@/components/layout-register.vue'
import FilterJson from './filterType.json'
import ZForm from '@/components/form.vue'
import SlideList from '@/components/slide-list.vue'
import PageHeader from '@/components/page-header.vue'
import ButtonList from '@/components/button-list.vue'
import Login from '@/pages/login/components/action/index.js'
export default {
  name: 'forgetPsw',
  components: {
    LayoutRegister,
    ZForm,
    SlideList,
    PageHeader,
    ButtonList
  },
  data() {
    return {
      FilterJson,
      active: 0
    }
  },
  methods: {
    buttnClick(item) {
      item.loading = true
      const params = {
        keep: this.transferKeep
      }
      params[item.event](item)
    },
    actionHandle(queries) {
      FilterJson[this.active].ruleForm = Object.assign({}, queries)
    },
    goback() {
      this.$router.push({
        name: 'apply'
      })
    },
    transferKeep(item) {
      this.$refs.form.submit().then(state => {
        if (state) {
          Login.resetPwd(FilterJson[this.active].ruleForm)
            .then(res => {
              if (res.code === 0) {
                this.$message({
                  message: '修改密码成功',
                  type: 'success'
                })
                this.$router.push({
                  name: 'apply'
                })
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(e => {
              console.log(e)
            })
        } else {
          item.loading = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.register {
  height: 100%;
  width: 100%;
  .register-body {
    width: 80%;
    margin: 0 auto;
  }
}
</style>
