<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="50" :dense="false" v-model="objOption.NavTitle" ref="NavTitle"
            class="col-12 q-pb-lg" label="导航主标题" :rules="[val => !!val || '请填写导航主标题']" />
          <q-input v-model="objOption.SubTitle" class="col-12 q-pb-lg" filled type="textarea" placeholder="导航子标题"/>
          <q-input stack-label maxlength="200" :dense="false" v-model="objOption.LinkUrl" class="col-6 q-pr-lg q-pb-lg"
            label="链接地址" />
          <q-select class="col-6 q-pb-lg" option-value="Id" option-label="Name" v-model="objOption.NavType"
            :options="NavTypeList" emit-value map-options label="导航类型" />
          <div class="col-6 q-pb-lg q-pr-lg">
            <q-uploader :style="{ backgroundImage: 'url(' + objOption.BgImg + ')' }" 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 {
    GetNavTypeList,
    SetWebNav,
    GetWebNav
  } from '../../api/system/webkit'
  import {
    UploadSelfFile
  } from "../../api/common/common";
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        //菜单等级
        MenuLevelOpts: [],
        //菜单类型
        NavTypeList: [],
        persistent: true,
        objOption: {
          Id: 0,
          NavTitle: '', //导航主标题
          SubTitle: '', //导航子标题
          BgImg: '', // 导航背景图
          LinkUrl: '', //链接地址
          NavType: 0 //导航类型  
        },
        optionTitle: "",
        //菜单列表
        MenuList: [],
        saveLoading: false,
      }
    },
    mounted() {
      this.initNavType();
      this.initObj()
    },
    methods: {
      //初始化下拉框
      initNavType() {
        GetNavTypeList().then(res => {
          if (res.Code == 1) {
            this.NavTypeList = res.Data;
          } else {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: res.Message
            })
          }
        })
      },
      uploadFile(files) {
        UploadSelfFile("course", files[0], res => {
          if (res.Code == 1) {
            this.objOption.BgImg = res.FileUrl;
          }
        });
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.Id > 0) {
          GetWebNav({
            Id: this.saveObj.Id
          }).then(res => {
            this.objOption.Id = res.Data.Id;
            this.objOption.NavTitle = res.Data.NavTitle;
            this.objOption.SubTitle = res.Data.SubTitle;
            this.objOption.BgImg = res.Data.BgImg;
            this.objOption.LinkUrl = res.Data.LinkUrl;
            this.objOption.NavType = res.Data.NavType;
          })
          this.optionTitle = "修改网站导航信息"
        } else {
          this.optionTitle = "新增网站导航信息"
          this.objOption.Id = 0;
          this.objOption.NavTitle = '';
          this.objOption.SubTitle = '';
          this.objOption.BgImg = '';
          this.objOption.LinkUrl = '';
          this.objOption.NavType = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveWebkitMenu() {
        this.$refs.NavTitle.validate();
        if (!this.$refs.NavTitle.hasError) {
          this.saveLoading = true
          SetWebNav(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>
