<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改机构' : '添加机构'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="92px">
      <el-form-item label="上级菜单:">
        <ele-tree-select
          clearable
          v-model="form.parentId"
          :data="organizationList"
          label-key="title"
          value-key="id"
          default-expand-all
          placeholder="请选择上级菜单"
        />
      </el-form-item>
      <el-form-item label="名称:" prop="title">
        <el-input clearable v-model="form.title" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="logo:" prop="logo">
        <upload-img
          v-model="form.logo"
          :images="form.logo"
          :limit="1"
          :multiple="true"
        />
      </el-form-item>
      <el-form-item label="来源:" prop="cfId" v-if="form.level < 2">
        <el-input clearable v-model="form.cfId" />
      </el-form-item>
      <el-form-item label="角色id:" prop="roleId" v-if="form.level > 1">
        <el-input clearable v-model="form.roleId" />
      </el-form-item>
      <el-form-item label="备注:" prop="meta">
        <el-input
          :rows="4"
          type="textarea"
          :maxlength="200"
          v-model="form.remark"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import {
    addOrganization,
    updateOrganization
  } from '@/api/organization/manage';
  import UploadImg from '@/components/UploadImg/index.vue';

  export default {
    components: { UploadImg },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      // 上级菜单id
      parentId: Number,
      // 全部菜单数据
      organizationList: Array
    },
    data() {
      const defaultForm = {
        id: null,
        parentId: '',
        title: '',
        logo: [],
        remark: '',
        cfId: '',
        level: 0,
        roleId: ''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          title: [
            {
              required: true,
              message: '请输入名称',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
            parentId: this.form.parentId || 0
          };
          const saveOrUpdate = this.isUpdate
            ? updateOrganization
            : addOrganization;
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },
      /* 判断是否是目录 */
      isDirectory(d) {
        return !!d.children?.length && !d.component;
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data,
              parentId: this.data.parentId === 0 ? '' : this.data.parentId ?? ''
            });
            this.isUpdate = true;
          } else {
            this.form.parentId = this.parentId ?? '';
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
