<template>
  <div class="fullheight">
    <ht-sidebar-dialog
        width="80%"
        title=""
        class="sp-manager__dialog"
        :visible="dialogVisible"
        :before-close="beforeCloseDialog"
    >
      <el-form v-form data-vv-scope="PortalUrlPermissionForm">
        <!-- <ht-form-item label="">
          <ht-input v-model="PortalUrlPermission.id" validate="required" />
        </ht-form-item> -->
        <ht-form-item label="外部系统名称" label-width="140px">
          <ht-select
              placeholder="请选择外部系统"
              v-model="PortalUrlPermission.sysId"
              class="m-r"
              :options="options"
              :validate="{required:true}"
          />
        </ht-form-item>
        <!--        <ht-form-item label="系统编码">-->
        <!--          <ht-input v-model="PortalUrlPermission.sysId" validate="required" />-->
        <!--        </ht-form-item>-->
        <ht-form-item>
          <el-tree
              :data="apiData"
              show-checkbox
              node-key="id"
              ref="tree"
              highlight-current
              :props="defaultProps"
              v-model="PortalUrlPermission.uriList"
          >
          </el-tree>

        </ht-form-item>
      </el-form>
      <div slot="footer" style="text-align: right">
        <el-button @click="beforeCloseDialog">{{$t("eip.common.cancel")}}</el-button>
        <el-button type="primary" @click="submitData">{{
            $t('eip.common.save')
          }}</el-button>
      </div>
    </ht-sidebar-dialog>
  </div>
</template>
<script>
export default {
  name:'PortalUrlPermissionManagerEdit',
  data() {
    return {
      dialogVisible: false,
      PortalUrlPermission: {},
      saveMethod: "POST",
      apiData: [],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      options:[]
    };
  },
  mounted() {
    this.$validator = this.$root.$validator;
  },
  methods: {
    submitData(){
      if(this.saveMethod=="PUT" && this.PortalUrlPermission.sysId){
        this.$http
            .post('${portal}/portalUrlPermission/v1/update', this.PortalUrlPermission)
            .then((res) => {
              this.afterSaveData()
            })
      }else{
        let url=this.$refs.tree.getCheckedNodes()
        if( !this.PortalUrlPermission.sysId){
          this.$message.error(`请选择授权外部系统的编码`);
          return
        }else if(!url.length){
          this.$message.error(`请勾选外部接口。`);
          return
        }
        let urlList=[]
        url.map(item=>{
          if(item.children && !item.children[0].children){
            let obj={
              methodURI:item.children[0].id.replace("realPath:",""),
              function:item.function,
              moduleName:item.moduleName,
              description:item.description
            }
            urlList.push(obj)
          }
        })
        this.PortalUrlPermission.uriList=urlList
        this.$http
            .post('${portal}/portalUrlPermission/v1/insert', this.PortalUrlPermission)
            .then((res) => {
              if(res){
                this.$message({
                  message: res.data.message,
                  type: 'success'
                });
              }
              this.afterSaveData()
            })
      }
    },
    showDialog(id) {
      if (id) {
        this.saveMethod = "PUT";
        this.$http.get("${portal}/portalUrlPermission/v1/" + id).then(
            resp => {
              this.PortalUrlPermission = resp.data;
              this.dialogVisible = true;
            }
        );
      } else {
        this.saveMethod = "POST";
        this.dialogVisible = true;
      }
    },
    beforeCloseDialog() {
      this.PortalUrlPermission = {};
      this.dialogVisible = false;
    },
    afterSaveData() {
      setTimeout(() => {
        this.$emit('loadData', {})
        this.beforeCloseDialog()
      }, 500);
    },
    getCheckedNodes() {
    },
    getCheckedKeys() {
    },
    // 获取外部接口数据
    initData() {
      this.$http
          .get('${portal}/portalBizSystem/v1/getOpenApiList')
          .then((res) => {
            if(res.data.value){
              for (let item in res.data.value) {
                // 初始化一级栏目：模块名称
                let module_name={
                  id:item,
                  label: item,
                  children:[]
                }
                for(let obj in res.data.value[item]){
                  let class_function={
                    id:obj,
                    label:obj,
                    children:[]
                  }
                  for(let o of res.data.value[item][obj]){
                    let method_description={
                      id:o.methodURI,
                      label:o.description,
                      function:o.function,
                      moduleName:o.moduleName,
                      description:o.description,
                      children:[{
                        id:"realPath:"+o.classURI+o.methodURI,
                        label:o.methodURI
                      }]
                    }
                    class_function.children.push(method_description)
                  }
                  module_name.children.push(class_function)
                }
                this.apiData.push(module_name)
              }
            }
          })
    },
    // 获取外部系统
    getBizSystemList(){
      let this_=this
      this.$http
          .get('${portal}/portalBizSystem/v1/getList')
          .then((res) => {
            this_.options = res.data.map((item) => {
              let m = {key: item.sysCode, value: item.sysName}
              return {...m}
            })
          })

    }
  },
  created() {
    this.initData()
    this.getBizSystemList()
  },

};
</script>

<style lang="scss" scoped>
.sp-manager__dialog /deep/ > .el-dialog > .el-dialog__body {
  height: calc(100% - 170px);
}
</style>
