<template>
  <div v-if="form">
    <main-top></main-top>
    <editcon
      :key="type"
      ref="child"
      :type="type"
      :rule="rule"
      :form="form"
      @cancel="backList"
      @save="determine"
    >
      <template slot-scope="{ form }" slot="content">
        <component :data="form" :is="cp" :roleList="roleList" />
      </template>
    </editcon>
  </div>
</template>

<script>
import vmanage from "./componets/manage";

import { formKey, rules } from "./data";
import {saveSys,getRoleList,getSysDetail} from "@/configue/api";

export default {
  components: {
    vmanage
  },
  data() {
    let { type, id } = this.$route.params;

    return {
      val:'user',
      type,
      id:String(id),
      form: '',
      roleList:[]
    };
  },
  computed: {
    cp() {
      return "v" + this.type;
    },
    rule() {
      return rules[this.type];
    },
  },
  mounted() {
    this.getRoleList()
    this.getDetail()
  },
  methods: {
    validate(cb) {
      let child = this.$refs.child;
      child.$refs[this.type].validate(async (valid) => {
        if (valid) {
          if (status == 2) {
            this.$confirm("请再次确认是否进行此审核操作？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(async () => {
                cb();
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消",
                });
              });
          } else {
            cb();
          }
        }
      });
    },
    cancel() {
      this.$router.back();
    },
    determine() {
      this.validate(() => {
        let params = {
          "address": "",
          "img": "",
          "nickName": "",
          "phone": "",
          "realName": "",
          "roleId": "",
          "sex": 0,
          "status": 0,
          "thumb": "",
          "unit": "",
          "userName": "",
          "id": this.id=='none'?null :this.id,
          ...this.form,
          isEnabled:Number(this.form.isEnabled)
        }
        saveSys(this.val,params,()=>{
          this.$alert(this.id=='none'?'新增成功（默认密码123456）':'编辑成功', '提示', {
              confirmButtonText: '确定',
              callback: () => {
                this.backList()
              }
            })
        })
      });
    },

    getRoleList() {
      getRoleList(
        (data) => {
          this.roleList = data.data.filter(item=>{
              return item.roleKey != 'sys_admin'
          });

          console.log(this.roleList);
        }
      );
    },

    async getDetail(){
      if(this.id == 'none'){
        this.form = this.cloneObject(formKey[this.$route.params.type])
      } else{
        await getSysDetail(this.val,{id:this.id},res=>{
          this.form = {...this.form,...res.data,isEnabled:Boolean(res.data.isEnabled)}

          if (this.form.roleKey == 'sys_visitor') {
            this.form.roleId = ''
          }
        })
      }

    }
  },
};
</script>
