<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :visible="visible"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改菜单' : '添加菜单'"
    @update:visible="updateVisible"
    :maxable="true"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="上级菜单:">
            <ele-tree-select
              clearable
              v-model="form.parentId"
              :data="menuList"
              label-key="name"
              value-key="id"
              default-expand-all
              placeholder="请选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称:" prop="name">
            <el-input
              clearable
              v-model="form.name"
              placeholder="请输入菜单名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="菜单来源:" prop="source">
            <el-radio-group v-model="form.source" @input="sourceChange">
              <el-radio :label="1">内部地址</el-radio>
              <el-radio :label="2">报表地址</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="路由地址:" prop="url">
            <el-input
              :disabled="form.source == 2"
              clearable
              v-model="form.url"
              placeholder="请输入路由地址"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="组件地址"
            prop="component"
          >
            <el-input
              :disabled="form.source == 2"
              clearable
              v-model="form.component"
              :placeholder="
                form.source == 1 ? '请输入组件地址' : '请输入外链地址'
              "
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权限编码:" prop="permissionCode">
            <el-input
              clearable
              v-model="form.permissionCode"
              placeholder="请输入路由地址"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item v-if="form.rootId != 99999" label="菜单图标:">
            <ele-icon-picker
              v-model="form.icon"
              placeholder="请选择菜单图标"
              :disabled="form.type === 2"
            />
          </el-form-item>

          <el-form-item v-if="form.rootId == 99999" label="菜单图标:">
            <el-input
              clearable
              v-model="form.icon"
              placeholder="请输入pda图标"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密级:" prop="secretLevel">
            <el-select v-model="form.secretLevel" style="width: 100%">
              <el-option
                v-for="item in secretLevelList()"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序号:" prop="sort">
            <el-input-number
              :min="0"
              v-model="form.sort"
              placeholder="请输入排序号"
              controls-position="right"
              class="ele-fluid ele-text-left"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :rules="[
      { required: form.source === 2 ? true : false, message: '字段必填', trigger: 'blur' }
    ]" :label="form.source === 2 ? '报表ID:': '扩展属性:'"  :prop="form.source == 2 ? 'extend' : ''">
            <el-input
              clearable
              v-model="form.extend"
              :placeholder="form.source === 2 ? '请输入报表ID' : '请输入扩展属性'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单类型:" prop="type">
            <el-radio-group v-model="form.type" @change="onMenuTypeChange">
              <el-radio :label="1">菜单</el-radio>
              <el-radio :label="2">按钮</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <div style="margin: 6px 0 28px 0">
        <el-divider />
      </div> -->
    </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 { EleIconPicker } from 'ele-admin';
import { saveOrUpdate } from '@/api/system/menu';
import { secretLevelList } from '@/enum/dict';

export default {
  components: { EleIconPicker },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    // 上级菜单id
    parentId: [Number, String],
    // 全部菜单数据
    menuList: Array
  },
  data() {
    const defaultForm = {
      id: null,
      parentId: '',
      name: '',
      type: 1,
      source: 1,
      icon: '',
      url: '/',
      component: '',
      sort: null,
      rootId: null,
      permissionCode: 1,
      secretLevel: 1,
      extend: null, //报表ID
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {
        name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序号', trigger: 'blur' }],
        url: [{ required: true, message: '请输入菜单路由', trigger: 'blur' }],
        component: [
          { required: true, message: '请输入组件地址', trigger: 'blur' }
        ],
        type: [{ required: true, message: '请选择菜单类型', trigger: 'blur' }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  computed: {
    // pathPlaceholder() {
    //   return this.form.openType === 2 ? '请输入外链地址' : '请输入路由地址';
    // },
    // componentPlaceholder() {
    //   return this.form.openType === 1 ? '请输入内链地址' : '请输入组件路径';
    // },
    // // 是否开启响应式布局
    // styleResponsive() {
    //   return this.$store.state.theme.styleResponsive;
    // }
  },
  methods: {
    secretLevelList() {
      return secretLevelList;
    },
    sourceChange(e) {
      if (e == 2) {
        this.form.url =
          '/reportForms/sourceLink';
        this.form.component = '/reportForms/sourceLink'
      } else {
        this.form.url = '/';
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        const data = {
          ...this.form,
          // menuType 对应的值与后端不一致在前端处理
          // type: this.form.type === 2 ? 1 : 0,
          parentId: this.form.parentId || 0
        };
        // const saveOrUpdate = this.isUpdate ? updateMenu : addMenu;
        if (!this.isUpdate) {
          delete data.id;
        }
        saveOrUpdate(data)
          .then((res) => {
            this.loading = false;
            if (this.isUpdate) {
              this.$message.success('菜单编辑成功');
            } else {
              this.$message.success('菜单新增成功');
            }
            this.updateVisible(false);
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
            // this.$message.error(e.message);
          });
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /* menuType选择改变 */
    onMenuTypeChange() {
      // if (this.form.menuType === 0) {
      //   this.form.authority = null;
      //   this.form.openType = 0;
      //   this.form.component = null;
      // } else if (this.form.menuType === 1) {
      //   if (this.form.openType === 2) {
      //     this.form.authority = null;
      //   }
      // } else {
      //   this.form.openType = 0;
      //   this.form.icon = null;
      //   this.form.path = null;
      //   this.form.component = null;
      //   this.form.hide = 0;
      // }
    },
    /* openType选择改变 */
    onOpenTypeChange() {
      if (this.form.openType === 2) {
        this.form.component = null;
        this.form.authority = null;
      }
    }
    /* 判断是否是外链 */
    // isUrl(url) {
    //   return !!(
    //     url &&
    //     (url.startsWith('http://') ||
    //       url.startsWith('https://') ||
    //       url.startsWith('//'))
    //   );
    // },
    /* 判断是否是目录 */
    // isDirectory(d) {
    //   return !!d.children?.length && !d.component;
    // }
  },
  watch: {
    visible(visible) {
      if (visible) {
        if (this.data) {
          // const isInnerLink = this.isUrl(this.data.component) ? 1 : 0;
          // menuType 对应的值与后端不一致在前端处理
          // const menuType =
          //   this.data.type === 1
          //     ? 2
          //     : this.isDirectory(this.data)
          //     ? 0
          //     : 1;

          this.$util.assignObject(this.form, {
            ...this.data,
            // openType: this.isUrl(this.data.path) ? 2 : isInnerLink,
            parentId: this.data.parentId == 0 ? '' : this.data.parentId ?? ''
          });
          this.isUpdate = true;
          // this.sourceChange(this.form.source);
        } else {
          this.form.parentId = this.parentId ?? '';
          this.isUpdate = false;
        }
      } else {
        this.$refs.form.clearValidate();
        this.form = { ...this.defaultForm };
        // this.sourceChange(this.form.source);
      }
    }
  }
};
</script>
