<template>
  <q-dialog v-model="persistent" 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.FunctionName" ref="FunctionName"
            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.FunctionCode" ref="FunctionCode"
            class="col-6 q-pr-lg q-pb-lg" label="功能编码" />
          <q-input filled stack-label maxlength="200" :dense="false" v-model="objOption.Remarks" ref="Remarks"
            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>
        </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 {
    saveMenuFunction,
    queryMenuFunction
  } from '../../api/system/index'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      paramObj:{
          type:Number,
          default:0
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          Id: 0, //主键编号
          MenuId: 0, //菜单编号
          FunctionName: "", //权限名称
          FunctionCode: "", //权限编号
          Remarks: "", //权限备注
          Status: 0, //状态(0-正常,1-禁用)},
        },
        optionTitle: "",
        saveLoading: false,
      }
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.Id > 0) {
          queryMenuFunction({
            Id: this.saveObj.Id
          }).then(res => {
            this.objOption.Id = res.Data.Id;
            this.objOption.MenuId = res.Data.MenuId;
            this.objOption.FunctionName = res.Data.FunctionName;
            this.objOption.FunctionCode = res.Data.FunctionCode;
            this.objOption.Remarks = res.Data.Remarks;
            this.objOption.Status = res.Data.Status;
          })
          this.optionTitle = "修改菜单功能权限信息"
        } else {
          this.optionTitle = "新增菜单功能权限"
          this.objOption.Id = 0;
          this.objOption.MenuId = this.paramObj;
          this.objOption.FunctionName = "";
          this.objOption.FunctionCode = "";
          this.objOption.Remarks = "";
          this.objOption.Status = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveSystemMenu() {
        this.saveLoading = true
        saveMenuFunction(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>
