<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="所属上级" prop="parent_id">
          <el-cascader
            v-model="form.model.parent_id"
            :options="role.select"
            :props="{
              label: 'name',
              value: 'id',
              checkStrictly: true,
              emitPath: false,
            }"
            clearable
            placeholder="请选择上级角色组"
            :disabled="props.modal.params.type == 'edit'"
            @change="isChangeParentId = true"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.model.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色标识" prop="code">
          <el-input v-model="form.model.code" placeholder="请输入角色标识"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input
            type="textarea"
            maxlength="40"
            show-word-limit
            v-model="form.model.description"
            placeholder="请输入角色描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.model.status">
            <el-radio label="normal">启用</el-radio>
            <el-radio label="hidden">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限信息">
          <sa-access
            type="select"
            :isChangeParentId="isChangeParentId"
            :role_id="form.model.parent_id"
            :multiple="true"
            v-model="form.model.rules"
          >
          </sa-access>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button
        v-if="modal.params.type == 'add'"
        v-auth="'admin.auth.role.add'"
        type="primary"
        @click="confirm"
        >保存</el-button
      >
      <el-button
        v-if="modal.params.type == 'edit'"
        v-auth="'admin.auth.role.edit'"
        type="primary"
        @click="confirm"
        >更新</el-button
      >
    </el-footer>
  </el-container>
</template>
<script setup>
  import { reactive, ref, unref, onMounted } from 'vue';
  import admin from '@/app/admin/api';
  import SaAccess from '../access/components/sa-access.vue';
  import { cloneDeep } from 'lodash';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });

  // 添加 编辑 form
  const formRef = ref(null);
  const form = reactive({
    model: {
      parent_id: '',
      name: '',
      description: '',
      code: '',
      status: 'normal',
      rules: [],
    },
    rules: {
      parent_id: [{ required: true, message: '选择上级角色组', trigger: 'blur' }],
      name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
      code: [{ required: true, message: '请输入角色标识', trigger: 'blur' }],
    },
  });

  // 获取详情
  async function getDetail(id) {
    const { error, data } = await admin.auth.role.detail(id);
    error === 0 && (form.model = data);
  }

  // 所属上级列表
  const role = reactive({
    select: [],
  });
  // 获取所属上级数据
  async function getRoleSelect() {
    ({ data: role.select } = await admin.auth.role.select());
  }

  const isChangeParentId = ref(false);

  // 表单关闭时提交
  function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { error } =
        props.modal.params.type == 'add'
          ? await admin.auth.role.add(submitForm)
          : await admin.auth.role.edit(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }

  onMounted(async () => {
    if (props.modal.params.type == 'edit') {
      await getDetail(props.modal.params.id);
    }
    await getRoleSelect();
  });
</script>
<style lang="scss" scoped>
  .sa-access {
    height: 500px;
    :deep() {
      .el-main {
        background: transparent;
      }
      .el-scrollbar {
        border-top: 1px solid var(--sa-border);
        border-bottom: 1px solid var(--sa-border);
        &:first-of-type {
          margin-left: 0;
        }
      }
    }
  }
</style>
