import routerUserMenu from '@/mixins/router-user-menu'
import breadcrumbComponent from '@/components/breadcrumb.vue'
import simpleSelectTree from '@/components/simple-select-tree.vue'
import {toTree} from '@/utils/util'
/**
 * 页面组件设置
 *
 *  version = "1.0.0";
 *  author = "徐飞";
 *  email = "xu.fei@data-u.com";
 * */
export default {
  name: 'detail',
  components:{ breadcrumbComponent, simpleSelectTree },
  mixins: [routerUserMenu],
  data () {
    return {
      // 组件id（修改和查看）
      id: false,
      methodOptions: [],
      typeOptions: [],
      // 新建对象
      interfaceInfo: {
        interfaceName: '',
        interfaceId: '',
        interfaceUrl: '',
        interfaceReturn: '',
        methodId: '',
        paramInput: [{paramKey:"" ,paramValue:""}],
        paramOut: [{ nodeName:"", nodeSite:"",nodeDesc: '' }],
      },
      // 页面打开类型，0：新建，1：查看，2：修改
      dialogState: 0,
      resourceTypeOptions: [],
      charsetOptions: [],
      interfaceTypeOptions: [],
    }
  },
  computed: {
    // 组件设置是否只读
    readonly: {
      // getter
      get: function() {
        return (this.dialogState === 1)
      },
      // setter
      set: function (newValue) {
      }
    },
    // /* 转换数据格式 */
    // treeData:{
    //   get: function() {
    //     let tree=this.typeOptions
    //     // 线性数组转成树后渲染
    //     let defaultProps={
    //       id: 'menuCode',
    //       parent_id: 'pcode',
    //       label: 'menuName'
    //     }
    //     let newTree=toTree(tree,'0',defaultProps)
    //     let root={
    //       id: '0',
    //       children: newTree,
    //       name: '自助报表',
    //       data:{
    //         id: '0',
    //         menuCode: '0',
    //         menuName: '自助报表'
    //       }
    //     }
    //     return [root]
    //   }
    // },
  },
  mounted () {
    // 初始化
    this.init()
  },
  methods: {
    //  如果有id，则获取id对应的部门信息赋值给formInfo
    init () {
      // 获取url参数
      let params = this.$route.params
      if (params.hasOwnProperty('id')) {
        // 如果有id，则加载组件对象
        this.id = params.id
        this.getDetail()
      }
      // 根据页面参数设置当前页面类型
      this.dialogState = params.id ? 2 : 0

      // 获取请求方式下拉列表
      this.getPostOptions()
      // 获取分类下拉
      this.getInterfaceTypeOptions()
    },

    getPostOptions () {
      this.$api.interfaceDb.listRequestMode().then(res=>{
        this.methodOptions = res.data
      })
    },

    getInterfaceTypeOptions () {
      this.$api.interfaceDb.listInterfaceProject()
        .then(res => {
          this.typeOptions = res.data
        })
    },

    // 获取回显详情信息
    getDetail () {
      let params = { id: this.id }
      let _this = this
      this.$api.interfaceDb.getInterfaceDataSourcesById(params)
        .then(res => {
          if (res.code == 1) {
            if (res.data) {
              _this.interfaceInfo = res.data

              _this.$nextTick(()=> {
                _this.$refs.interfaceInfo.clearValidate()
              })
            }
          }
        })
    },

    // 测试连接
    getTestConnection () {
      let _this = this
      // 判断组件设置是否通过验证
      this.$refs.interfaceInfo.validate((valid) => {
        if (valid) {
          // 提交后台并在成功后回到上一页
          let params = _this.interfaceInfo
          console.info(params)
          params.interfaceId = this.id
          _this.$api.interfaceDb.testInterfaceDataSources(params)
            .then(res => {
              if (res.code == 1) {
                let data = res.data || {}
                _this.interfaceInfo.interfaceReturn = JSON.stringify(data, 4)
                _this.$message.success(res.msg)
              } else {
                _this.$message.error(res.msg)
              }
            })
        } else {
          return false
        }
      })

    },

    // 提交保存
    add () {
      let _this = this
      // 判断组件设置是否通过验证
      this.$refs.interfaceInfo.validate((valid) => {
        if (valid) {
          // 提交后台并在成功后回到上一页
          let params = _this.interfaceInfo
          _this.$api.interfaceDb.addInterfaceDataSources(params)
            .then(res => {
              if (res.code == 1) {
                _this.$message.success(res.msg)
                _this.goBack()
              }
            })
        } else {
          return false
        }
      })
    },

    // 更新
    update () {
      let _this = this
      // 判断组件设置是否通过验证
      this.$refs.interfaceInfo.validate((valid) => {
        if (valid) {
          // 提交后台并在成功后回到上一页
          let params = _this.interfaceInfo
          params.interfaceId = _this.id
          params.id = _this.id
          _this.$api.interfaceDb.updateInterfaceDataSourcesById(params)
            .then(res => {
              if (res.code == 1) {
                _this.$message.success(res.msg)
                _this.goBack()
              }
            })
        } else {
          return false
        }
      })
    },
    // 添加入参
    addParameter () {
      this.interfaceInfo.paramInput.push({
        paramKey:"" ,
        paramValue:""
      })
    },
    // 添加出参
    addResult () {
      this.interfaceInfo.paramOut.push({ nodeName:"", nodeSite:"",nodeDesc: '' })
    },
    // 删除入参
    paramDelete (index, row) {
      this.interfaceInfo.paramInput.splice(index, 1)
    },
    // 删除出参
    resultDelete (index, row) {
      this.interfaceInfo.paramOut.splice(index, 1)
    },
    goBack () {
      this.$router.go('Interface')
    }
  }
}
