<template>
  <div v-if="form">
    <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" />
      </template>
    </editcon>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import vmanage from "./componets/manage";

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

export default {
  components: {
    vmanage
  },
  data() {
    
    return {
      type: this.$route.params.type,
      id: this.$route.params.id,
      form: ''
    };
  },
  computed: {
    ...mapGetters({
      activeMenu: "activeMenu",
    }),
    cp() {
      return "v" + this.type;
    },
    rule() {
      return rules[this.type];
    },
  },
  mounted() {
    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();
          }
        }
      });
    },
    determine() {
      console.log(1111);
      this.validate(() => {
        let params = {
          "resources": [],
          "roleDesc": "",
          "roleKey": "",
          "roleName": "",
          "id": this.id=='none'?null :this.id,
          ...this.form
        }
        saveSys('role',params,()=>{
          this.$alert('操作成功', '提示', {
              confirmButtonText: '确定',
              callback: () => {
                this.backList()
              }
            })
        })
      });
    },

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

    },

    async getLimit () {
      let res = await this.$http({
        method: 'get',
        url: 'sys/resource/getTreePermissions'
      })
      this.form =  {
        ...this.form,
        resourceslist:res.data
      }
    },
    submit() {},
  },
};
</script>
