<template>
  <div>
    <el-dialog
      :title="title"
      :destroy-on-close="true"
      :visible.sync="Visible"
      width="40%"
      @close="handleClose"
      :close-on-click-modal="false"
      top="10vh"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input placeholder="请输入系统名称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="父级菜单" prop="parentId">
          <el-select
            clearable
            v-model="form.parentId"
            placeholder="请选择父级菜单"
          >
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限类型" prop="authType">
          <el-select
            clearable
            v-model="form.authType"
            placeholder="请选择权限类型"
          >
            <el-option
              v-for="(item, key) in dict.authType"
              :key="key"
              :label="item"
              :value="Number(key)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="访问地址" prop="url">
          <el-input
            placeholder="请输入菜单访问地址"
            v-model="form.url"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="menuType">
          <el-radio-group v-model="form.menuType">
            <el-radio
              v-for="(item, key) in dict.menuType"
              :key="key"
              :label="Number(key)"
              >{{ item }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="(item, key) in dict.status"
              :key="key"
              :label="Number(key)"
              >{{ item }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="图标" prop="imgPath">
          <div class="icon-box grid grid-cols-2 gap-1">
            <div
              :class="[
                'icon-item',
                'cursor-pointer',
                { active: form.imgPath == '' },
              ]"
              @click="form.imgPath = ''"
            >
              不需要图标
            </div>
            <div
              :class="[
                'icon-item',
                'cursor-pointer',
                { active: form.imgPath == v },
              ]"
              v-for="(v, i) in iconJson"
              :key="i"
              @click="form.imgPath = v"
            >
              <i :class="v"></i>
              <span class="ml-2">{{ v }}</span>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleRest">重 置</el-button>
        <el-button size="small" type="primary" @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveMenu } from "@/api/system";
import iconJson from "@/assets/icon.json";
export default {
  components: {},
  props: {
    title: {
      type: String,
      required: true,
      default: "",
    },
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
    menuList: {
      required: true,
      type: Array,
      default: () => [],
    },
    dict: {
      required: true,
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      iconJson,
      form: {
        name: "",
        parentId: 0,
        menuType: 0,
        status: 1,
        url: "",
        authType: "",
        imgPath: "",
      },
      rules: {
        name: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
        url: [
          { required: true, message: "请输入菜单访问地址", trigger: "blur" },
        ],
        authType: [
          { required: true, message: "请输入选择权限类型", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      },
    },
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await saveMenu(this.form);
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("success");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = { ...row };
      }, 10);
    },
    // 重置
    handleRest() {
      this.$resetForm("form");
    },
    // 关闭
    handleClose() {
      this.$resetForm("form");
      this.Visible = false;
    },
  },
};
</script>

<style lang="less" scoped>
:deep(.el-dialog__body) {
  max-height: 600px;
  overflow: auto;
}
:deep(.el-select) {
  width: 100%;
}
.active {
  color: var(--primary);
}
</style>
