<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.MenuId==0?'新增菜单信息':'修改菜单信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-lg q-px-md text-grey-6">菜单信息</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.MenuName" ref="MenuName"
            class="col-6 q-pr-lg q-pb-lg" label="菜单名称" :rules="[val => !!val || '请填写菜单名称']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.MenuUrl" ref="MenuUrl"
            class="col-6 q-pr-lg q-pb-lg" label="菜单地址" />
          <div class="col-6 q-pr-lg q-pb-lg q-pt-lg">
            <q-toggle size="md" label="固定用户中心" color="primary" :false-value="0" :true-value="1"
              v-model="objOption.IsUserCenter" />
          </div>
          <q-select class="col-6 q-pr-lg" v-model="objOption.MenuType" :options="MenuTypeOpts" emit-value map-options
            label="菜单类型" @input="queryMenuList()" />
          <q-select class="col-6 q-pr-lg q-pb-lg" v-if="objOption.IsUserCenter==0" v-model="objOption.MenuLevel"
            :options="MenuLevelOpts" emit-value map-options label="菜单等级" @input="queryMenuList()" />
          <q-select v-if="objOption.IsUserCenter==0" filled clearable use-input option-value="MenuId"
            option-label="MenuName" v-model="objOption.ParentId" :options="MenuList" label="上级菜单" :dense="false"
            emit-value map-options class="col-6 q-pr-lg q-pb-lg" @filter="filterFn">
            <template v-slot:no-option>
              <q-item>
                <q-item-section class="text-grey">
                  未找到相关数据
                </q-item-section>
              </q-item>
            </template>
          </q-select>
          <q-input stack-label maxlength="60" :dense="false" v-model="objOption.MenuIcon" class="col-6 q-pr-lg q-pb-lg"
            label="图标" />
          <q-input filled stack-label maxlength="10" :dense="false" v-model="objOption.SortNum"
            class="col-6 q-pr-lg q-pb-lg" label="排序" />
          <div class="col-6 q-pr-lg q-pb-lg q-pt-lg">
            <q-toggle size="md" label="状态" color="primary" :false-value="1" :true-value="0"
              v-model="objOption.Status" />
            <div class="text-grey-6 text-caption">注意：关闭后，菜单将无法正常使用.</div>
          </div>
          <q-select class="col-6 q-pr-lg" v-model="objOption.TeamType" :options="TeamTypeOpts" emit-value map-options
            label="项目类型" />
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveSystemMenu" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    getMenuList,
    saveMenu,
    getMenuTypeList,
    getMenuLevelList,
    getMenuInfo
  } from '../../api/system/index'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        //菜单等级
        MenuLevelOpts: [],
        //菜单类型
        MenuTypeOpts: [],
        TeamTypeOpts: [{
            label: '教育菜单',
            value: 1
          },
          {
            label: '资产菜单',
            value: 2
          }
        ],
        persistent: true,
        objOption: {
          MenuId: 0, //菜单编号
          ParentId: 0, //父级节点编号
          MenuName: "", //菜单名称
          MenuUrl: "", //菜单地址
          MenuIcon: "", //菜单图标
          MenuLevel: -1, //菜单等级
          MenuType: -1, //菜单类型
          TeamType: 1, //项目类型
          SortNum: 0, //排序
          Status: 0, //状态(0-正常,1-禁用)
          IsUserCenter: 0, //是否个人中心页面
        },
        optionTitle: "",
        //菜单列表
        MenuList: [],
        AllMenuList: [],
        saveLoading: false,
      }
    },
    mounted() {
      this.initData();
      this.initObj()
    },
    methods: {
      //筛选员工
      filterFn(val, update) {
        update(() => {
          if (val === '') {
            this.MenuList = JSON.parse(JSON.stringify(this.AllMenuList))
          } else {
            const needle = val.toLowerCase();
            this.MenuList = this.AllMenuList.filter(v => v.MenuName.toLowerCase().indexOf(needle) > -1);
          }
        })
      },
      //获取菜单列表
      queryMenuList() {
        this.MenuList = [];
        this.AllMenuList = [];
        var qMsg = {
          MenuLevel: Number(this.objOption.MenuLevel - 1),
          MenuType: this.objOption.MenuType
        }
        getMenuList(qMsg).then(res => {
          var tempArray = [];
          if (res.Data && res.Data.length > 0) {
            res.Data.forEach(item => {
              var obj = {
                MenuId: item.MenuId.toString(),
                MenuName: item.MenuName
              };
              tempArray.push(obj);
            });
          }
          this.MenuList = tempArray;
          this.AllMenuList = tempArray;
          this.$forceUpdate();
        })
      },
      //初始化下拉框
      initData() {
        this.MenuTypeOpts = getMenuTypeList();
        this.MenuLevelOpts = getMenuLevelList();
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.MenuId > 0) {
          if (this.saveObj.MenuType > 0 || this.saveObj.MenuLevel > 0) {
            this.objOption.MenuType = this.saveObj.MenuType;
            this.objOption.MenuLevel = this.saveObj.MenuLevel;
            this.queryMenuList();
          }
          getMenuInfo({
            MenuId: this.saveObj.MenuId
          }).then(res => {
            this.objOption.MenuId = res.Data.MenuId;
            this.objOption.ParentId = res.Data.ParentId.toString();
            this.objOption.MenuName = res.Data.MenuName;
            this.objOption.MenuUrl = res.Data.MenuUrl;
            this.objOption.MenuIcon = res.Data.MenuIcon;
            this.objOption.MenuLevel = res.Data.MenuLevel;
            this.objOption.MenuType = res.Data.MenuType;
            this.objOption.TeamType = res.Data.TeamType;
            this.objOption.SortNum = res.Data.SortNum;
            this.objOption.Status = res.Data.Status;
            this.objOption.IsUserCenter = res.Data.IsUserCenter;
          })
          this.optionTitle = "修改菜单信息"
        } else {
          this.optionTitle = "新增菜单"
          this.objOption.MenuId = 0;
          this.objOption.ParentId = 0;
          this.objOption.MenuName = "";
          this.objOption.MenuUrl = "";
          this.objOption.MenuIcon = "";
          this.objOption.MenuLevel = -1;
          this.objOption.MenuType = -1;
          this.objOption.TeamType = 1;
          this.objOption.SortNum = 0;
          this.objOption.Status = 0;
          this.objOption.IsUserCenter = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveSystemMenu() {
        this.saveLoading = true
        saveMenu(this.objOption).then(res => {
          this.saveLoading = false
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success")
          this.closeSaveForm()
        }).catch(() => {
          this.saveLoading = false
        })
      }
    },
  }

</script>
