import breadcrumbComponent from '@/components/breadcrumb.vue'
import tableComponent from '@/components/table.vue'
import list from '@/mixins/list'
import routerUserMenu from '@/mixins/router-user-menu'

export default {
  name: 'DatabaseList',
  components: {breadcrumbComponent, tableComponent},
  mixins: [list, routerUserMenu],
  data () {
    return {
      searchForm: {
        methodName: '',
        status: '',
        projectName: '',
        pageSize: 10,
        page: 1
      },
      statusOption:[{id:"1",name:"启用"},{id:"0",name:"禁用"}],
      reqOptions: [],
      typeOptions: [],
      // 列表数据
      tableData: {
        data: [],
        total: 0
      },
      testDataVisible: false,
      interfaceVisible: false,
      testReturnData: "",
      showInterfaceData: [],
      labelInfo:[],
      // 列表设置
      tableOption: {
        // 是否显示序号
        index: true,
        // 是否显示勾选框
        select: false,
        // 默认排序
        defaultSort: {prop: 'updateTime', order: 'desc'},
        // 列
        tableLabel: [
          {prop: 'interfaceName', title: '接口名称', width: '100'},
          {prop: 'projectName', title: '接口分类', width: '100'},
          {prop: 'interfaceUrl', title: '接口地址', width: '100'},
          {prop: 'methodName', title: '请求方式', width: '100'},
          {prop: 'numberOfInputNodes', title: '入参节点数', width: '100'},
          {prop: 'numberOfOutNodes', title: '出参节点数', width: '100'},
          {prop: 'statusName', title: '状态', width: '100'},
          {prop: 'updateTime', title: '修改时间', width: '120', sort: true}
        ],
        // 操作
        tableOperate: {
          width: '300',
          buttons: [
            {label: '接口测试', methods: 'testInterface', color: 'link'},
            {label: '禁用', methods: 'stop', color: 'warn', show: { statusId: "1" }},
            {label: '启用', methods: 'start', color: 'safe', show: { statusId: "0" }},
            {label: '修改', methods: 'edit', color: 'safe', show: { statusId: "0" }},
            {label: '查看', methods: 'show', color: 'safe'},
            {label: '删除', methods: 'del', color: 'warn'}
          ]
        },
      },
    }
  },
  created () {
  },
  mounted () {
    this.listResourceType()
    this.getInterfaceTypeOptions()
    // this.getList()
  },
  methods: {

    /* 编辑功能 */
    edit (obj) {
      this.go('interfaceEdit', {id: obj.interfaceId})
    },

    /* 启用功能 */
    start (obj) {
      let params = {id: obj.interfaceId,statusId:"1"}
      this.$api.interfaceDb.updateInterfaceDataSourcesStatus(params)
        .then(res => {
          if (res.code == 1) {
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    /* 禁用功能 */
    stop (obj) {
      let params = {id: obj.interfaceId,statusId:"0"}
      this.$api.interfaceDb.updateInterfaceDataSourcesStatus(params)
        .then(res => {
          if (res.code == 1) {
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    /* 测试 */
    testInterface (obj) {
      let params = {interfaceId: obj.interfaceId}
      this.$api.interfaceDb.testInterfaceDataSources(params)
        .then(res => {
          if (res.code == 1) {
            let data = res.data
            data = data ? JSON.stringify(data, 4) : ''
            this.testReturnData = data
            this.testDataVisible = true
            this.$message.success(res.msg)
          }
        })
    },
    /* 查看 */
    show (obj) {
      let params = {id: obj.interfaceId}
      this.$api.interfaceDb.getInterfaceDataSourcesById(params).then(res => {
        if (res.code == 1) {
          this.$message.success(res.msg)
          let data = res.data
          let newArr = []
          let labelArr = [{'interfaceName':'接口名称'},{'projectName': '接口分类'}, {'interfaceUrl': '接口地址'},{'methodName': '请求方式'},{'numberOfInputNodes': '入参节点数'},{'numberOfOutNodes': '出参节点数'},{'statusName': '状态'},{'updateTime': '修改时间'}]
          for (let i=0,len = labelArr.length;i<len;i++) {
            for (let key in labelArr[i]) {
              newArr.push({'value': data[key],'name': labelArr[i][key]})
            }
          }
          this.labelInfo = newArr
          this.interfaceVisible = true
        }
      })
    },
    // 执行删除
    delAction (obj) {
      let params = {id: obj.interfaceId}
      this.$api.interfaceDb.deleteInterfaceDataSources(params)
        .then(res => {
          if (res.code == 1) {
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    // 删除
    del(obj) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delAction(obj)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    /* 请求方式 */
    listResourceType () {
      this.$api.interfaceDb.listRequestMode()
        .then(res => {
          this.reqOptions = res.data
        })
    },
    /* 接口类型 */
    getInterfaceTypeOptions () {
      this.$api.interfaceDb.listInterfaceProject()
        .then(res => {
          this.typeOptions = res.data
        })
    },

    /* 获取接口列表信息 */
    getList () {
      let params = this.searchForm
      this.$api.interfaceDb.listInterfaceDataSources(params)
        .then(res => {
          this.tableData.data = res.data
          this.tableData.total = res.totalNumber
        })
    },
  }
}
