<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.Id==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.Name" ref="Name"
            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-pb-lg" label="菜单地址" />
          <q-select class="col-6 q-pr-lg q-pb-lg" v-model="objOption.MenuLevel" :options="MenuLevelOpts" emit-value
            map-options label="菜单等级" @input="getFatherData()" />
          <q-select class="col-6 q-pb-lg" v-model="objOption.ParentId" option-value="Id" option-label="Name"
            :options="FatherLevelOpts" emit-value map-options label="父级Id" />
          <q-input filled stack-label maxlength="50" :dense="false" v-model="objOption.ClassName" ref="ClassName"
            class="col-6 q-pb-lg q-pr-lg" label="样式" />
          <q-input filled stack-label maxlength="4" :dense="false" @keyup.native="checkInteger(objOption,'SortNum')"
            v-model="objOption.SortNum" ref="SortNum" class="col-6 q-pb-lg" label="排序" />
          <div class="col-6 q-pb-lg q-pr-lg">
            <q-uploader :style="{ backgroundImage: 'url(' + objOption.Icon + ')' }"
              style="width:auto;height:200px;background-repeat:no-repeat;background-size:cover;" flat hide-upload-btn
              max-files="1" label="菜单图标" accept=".jpg, image/*" :factory="uploadFile" auto-upload>
            </q-uploader>
          </div>
        </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="saveWebkitMenu" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    SetWebMenu,
    GetWebMenu,
    GetWebMenuList
  } from '../../api/system/webkit'
  import {
    UploadSelfFile
  } from "../../api/common/common";
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        //菜单等级
        MenuLevelOpts: [{
            label: '全部',
            value: -1
          },
          {
            label: '一级',
            value: 1
          },
          {
            label: '二级',
            value: 2
          },
          {
            label: '三级',
            value: 3
          }
        ],
        persistent: true,
        objOption: {
          Id: 0,
          Name: '', //菜单名称
          MenuUrl: '',
          ParentId: 0,
          MenuLevel: -1,
          ClassName: '',
          Icon: '',
          SortNum: ''
        },
        optionTitle: "",
        //菜单列表
        MenuList: [],
        FatherLevelOpts: [],
        saveLoading: false,
      }
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //下拉切换父级数据
      getFatherData() {
        this.objOption.ParentId = 0;
        this.FatherLevelOpts = [];
        var MenuLevel = Number(this.objOption.MenuLevel - 1)
        if (MenuLevel && MenuLevel > 0) {
          let msg = {
            MenuLevel: MenuLevel
          }
          GetWebMenuList(msg).then(res => {
            if (res.Code == 1) {
              this.FatherLevelOpts = res.Data;
            }
          })
        }
      },
      uploadFile(files) {
        UploadSelfFile("course", files[0], res => {
          if (res.Code == 1) {
            this.objOption.Icon = res.FileUrl;
          }
        });
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.Id > 0) {
          GetWebMenu({
            Id: this.saveObj.Id
          }).then(res => {
            this.objOption.MenuLevel = res.Data.MenuLevel;
            this.getFatherData();
            this.objOption.Id = res.Data.Id;
            this.objOption.Name = res.Data.Name;
            this.objOption.MenuUrl = res.Data.MenuUrl;
            this.objOption.ParentId = res.Data.ParentId;
            this.objOption.ClassName = res.Data.ClassName;
            this.objOption.Icon = res.Data.Icon;
            this.objOption.SortNum = res.Data.SortNum;
          })
          this.optionTitle = "修改网站菜单信息"
        } else {
          this.optionTitle = "新增网站菜单信息"
          this.objOption.Id = 0;
          this.objOption.Name = '';
          this.objOption.MenuUrl = '';
          this.objOption.ParentId = 0;
          this.objOption.MenuLevel = -1;
          this.objOption.ClassName = '';
          this.objOption.Icon = '';
          this.objOption.SortNum = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveWebkitMenu() {
        this.$refs.Name.validate();
        if (!this.$refs.Name.hasError) {
          this.saveLoading = true
          SetWebMenu(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>
