<template>
  <li
    v-if="model.show"
    @click="changeUserMenuClickCount"
  >
    <a
      v-if="(model.menuNodeType != &quot;Branch&quot;) && (model.menuNodeType != &quot;HTML&quot;)"
      target="_blank"
      @click="clickMenuNode"
    >
      {{ Language.getMenuNameTrl($i18n.locale, model) }}
    </a>
    <span v-if="model.subMenus == undefined || model.subMenus.length == 0">
      <i
        v-if="model.favorite == true"
        class="fa fa-star menu-branch-leaf-icon"
        @click="changeInFavoriteList"
      />
      <i
        v-if="model.favorite == false"
        class="fa fa-star-o menu-branch-leaf-icon"
        @click="changeInFavoriteList"
      />
    </span>
    <a
      v-if="model.menuNodeType == 'HTML'"
      target="_blank"
      @click="clickMenuNode"
    >{{ Language.getMenuNameTrl($i18n.locale, model) }}</a>
    <a
      v-if="model.menuNodeType == &quot;Branch&quot;"
      class="menu-branch"
      :class="{'active': model.expand}"
      @click="selectMenuNode(model)"
    >
      <i
        class="child_middle_menu"
        :class="model.icon"
      />
      {{ Language.getMenuNameTrl($i18n.locale, model) }}
      <span class="fa fa-chevron-down" />
    </a>
    <ul
      v-if="model.menuNodeType == &quot;Branch&quot;"
      class="nav menu-nav child_menu"
      :class="{'active': model.expand}"
    >
      <MenuNode
        v-for="item in model.subMenus"
        :key="item.uuid"
        :model="item"
        @select-node="selectNode($event, model.subMenus)"
      />
    </ul>
  </li>
</template>

<script>
import Common from '../common/Common.js';
import RouteUtil from '../common/RouteUtil.js';


import CustomerWindowResource from '../api/dic/CustomerWindowResource.js';
import HtmlWindowResource from '../api/dic/HtmlWindowResource.js';
import MenuResource from '../api/dic/MenuResource.js';
import UserMenuResource from '../api/system/UserMenuResource.js';


import UserMenuClickCountResource from '../api/system/UserMenuClickCountResource.js';



import Language from '../common/Language.js';
import CurdWindowResourceV2 from '../api/dic/CurdWindowResourceV2.js';
import { Notify, Uuid } from 'pc-component-v3';

export default {
  name: 'MenuNode',

  components: {

  },

  props: {
    model: {
      type: Object,
      default: function(){
        return null;
      },
    },
  },
  emits: ['selectNode'],


  data: function () {
    this.Language = Language;

    return {
      'expand': false,	// 选择的节点
    };
  },

  mounted: function () {
    var _self = this;
  },

  methods: {
        

    /**
         * 点击菜单节点
         */
    clickMenuNode: function () {
      var _self = this;
      var nodeType = this.model.menuNodeType;
      if (nodeType == undefined || nodeType == 'NONE' || nodeType == 'Branch') {
        return '#';
      } else if (nodeType == 'Report' || nodeType == 'Process') {
        let url = '/desktop/process-report/' + _self.model.processReportNo;
        _self.$router.push({
          path: url,
        });
      } else if (nodeType == 'WorkFlow') {
        return '#';
      } else if (nodeType == 'Window') {
        let url = '/desktop/window/' + _self.model.curdWindowNo + '?uuid=' + Uuid.createUUID();
        
        CurdWindowResourceV2.uniqueByNoAccessControl(
          _self.model.curdWindowNo,
        ).then(successData => {
          if(successData.errorCode === 0){
            _self.$router.push(
              '/desktop/window/' +
                _self.model.curdWindowNo +
                  '?uuid=' + Uuid.createUUID(),
            );
          }else{
            Notify.error(_self.$t('lang.Notify.errorOpeningCURDWindow'), successData.errorMessage, false);
          }
        },errorData => {
          Common.processException(errorData);
        });
      } else if (nodeType == 'SheetWindow') {
        let url = '/desktop/sheetWindow/' + _self.model.sheetCurdWindowNo + '?uuid=' + Uuid.createUUID();
        this.$router.push({
          path: url,
        });
      } else if (nodeType == 'Info') {
        let url = '/desktop/info/' + _self.model.infoWindowNo;
        this.$router.push({
          path: url,
        });
      } else if (nodeType == 'Customer') {
        CustomerWindowResource.uniqueByNo(_self.model.customerWindowNo).then(successData => {
          if (successData != null) {
            // var url = "/desktop/" + data.routeUrl;
            _self.$router.push({
              path: successData.routeUrl,
            });
          }
        }, errorData => {
          Common.processException(errorData);
        });
      } else if (nodeType == 'HTML') {
        HtmlWindowResource.uniqueByNo(_self.model.htmlWindowNo).then(successData => {
          if (successData != null) {
            var htmlFileName = successData.htmlFileName;
            if(htmlFileName == null){
              Notify.error(_self.$t('lang.Notify.dataDictionaryError'), _self.$t('lang.Notify.describe1'), false);
              return;
            }
            if (htmlFileName.indexOf('{URL}') >= 0) {
              htmlFileName = htmlFileName.replace('{URL}', Common.getHostPageBaseURL());
            }
            window.open(htmlFileName);
          }
        }, errorData => {
          Common.processException(errorData);
        });
      }else if(nodeType == 'LOWCODE'){
        let url = '/desktop/lowcode-example/' + _self.model.lowcodeWindowNo;
        this.$router.push({
          path: url,
        });
      }
    },

    changeUserMenuClickCount: function () {
      // 统计点击数
      var _self = this;
      if (_self.model.subMenus == undefined || _self.model.subMenus.length == 0) {
        UserMenuClickCountResource.changeUserMenuClickCount(_self.model.no).then(successData => {

        }, errorData => {
          Common.processException(errorData);
        });
      }
    },

    selectMenuNode: function (node) {
      this.changeUserMenuClickCount();
      node.expand = !node.expand;
      this.$emit('selectNode', node);
    },

    selectNode: function (node, subMenus) {
      if (node.expand) {
        subMenus.forEach(function (item) {
          if (item != node) {
            item.expand = false;
          }
        });
      }
    },
    // 收藏、取消收藏菜单
    changeInFavoriteList: function () {
      var _self = this;
      UserMenuResource.addToFavoriteList(_self.model.no).then(successData => {
        _self.model.favorite = !_self.model.favorite;
      }, errorData => {
        Common.processException(errorData);

      });
    },
  },
};
</script>

<style>
.menu-branch.active {
    text-shadow: rgba(0, 0, 0, 0.25) 0 -1px 0;
    background: linear-gradient(#334556, #2c4257), #2a3f54;
    box-shadow: rgba(0, 0, 0, 0.25) 0 1px 0,
        inset rgba(255, 255, 255, 0.16) 0 1px 0;
}
</style>


<style scoped>
.menu-branch-leaf-icon {
    float: top;
    position: absolute;
    left: 150px;
    top: 15px;
    color: #FFFFFF;
}

.menu-branch-leaf-text {
    text-decoration: none;
    color: white;
    padding: 15px 0px 15px 0px;
}
</style>