<template>
  <el-drawer :visible.sync="drawer" direction="rtl" size="40%" @close="close">
    <div class="box-card" style="height: 100vh">
      <el-form
        ref="add_form"
        :model="add_form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input
            v-model="add_form.name"
            placeholder="请输入菜单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="别名" prop="alias">
          <el-input
            v-model="add_form.alias"
            placeholder="请输入别名"
          ></el-input>
        </el-form-item>
        <el-form-item label="父级菜单" prop="pid">
          <el-cascader
            v-model="pid"
            :show-all-levels="false"
            clearable
            :props="{ checkStrictly: true, label: 'name', value: 'id' }"
            :options="moduleList"
            @change="pidChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="父级id">
          {{ add_form.pid }}
        </el-form-item>
        <el-form-item label="关联权限">
          <el-cascader
            v-model="add_form.associatePath"
            :options="moduleList"
            :props="{ multiple: true, label: 'name', value: 'id' }"
            collapse-tags
            clearable
            @change="asseccArrChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="链接" prop="extend">
          <el-input
            v-model="add_form.extend"
            placeholder="请输入链接"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="subData">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script>
  import { editModule, addModule, ModuleGetModuleInfo } from "@/api/System";

  export default {
    name: "MenuAdmin",
    props: {
      drawer: {
        type: Boolean,
        default: false,
      },
      moduleList: {
        type: Array,
        default: () => {
          return [];
        },
      },
      modelId: {
        type: [Number, String],
        default: 0,
      },
    },
    data() {
      return {
        pid: [],
        add_form: {
          pid: "",
          name: "",
          alias: "",
          extend: "",
          associate: [],
          associatePath: [],
        },
        rules: {},
        model_id: 0,
      };
    },
    created() {
      if (this.modelId) {
        this.getDetail();
      }
    },
    methods: {
      pidChange(val) {
        this.add_form.pid = val[val.length - 1];
      },
      // 提交
      async subData() {
        const params = {
          ...this.add_form,
        };
        let target = {};
        if (this.modelId) {
          target = await editModule(this.modelId, params);
        } else {
          target = await addModule(params);
        }
        const data = target;

        this.$message.success("提交成功");
        this.$emit("subSuccess");
      },
      // 获取详情
      async getDetail() {
        const { data } = await ModuleGetModuleInfo(this.modelId);

        this.add_form = {
          pid: data.pid,
          name: data.name,
          alias: data.alias,
          extend: data.extend,
          associate: data.associate,
          associatePath: data.associatePath,
        };
      },
      close() {
        this.$emit("close");
      },
      asseccArrChange(arr) {
        this.add_form.associate = arr.map((item) => {
          return item[item.length - 1];
        });
      },
    },
  };
</script>

<style scoped></style>
