<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label='接口名称'>
          <el-input v-model="search.jkName" @keyup.enter.native="searchData()" size="small" placeholder="请输入查询内容" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        接口信息
      </div>
      <div>
      <el-button icon="el-icon-circle-plus-outline" type="goon" size="mini" @click="dialogTableVisible = true">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" @click="modify">修改</el-button>
      <el-button icon="el-icon-circle-close" type="danger" size="mini" @click="deleteData">停用</el-button>
      <el-button icon="el-icon-check" type="goon" size="mini" @click="recoverData">启用</el-button>
      </div>
    </div>
    <div class="common-table-div">
       <el-table v-loading="dataLoading" ref="dataTable" border :height="height_top" highlight-current-row @selection-change="handleSelectionChange" :data="table.list" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :row-class-name="tableRowClassName" :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" fixed="left" align="center">
        </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="jkNo" label="接口编号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="jkName" label="接口名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="jkNum" label="接口频率" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="jkApi" label="接口路径" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="jkUsed" label="接口作用" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="jkIp" label="接口IP" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="jkCom" label="接口COM" align="center"> </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px"  prop="dataUser" label="数据库用户名" align="center"> </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="dataPassword" label="数据库密码" align="center"> </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="165px" prop="dataInstance" label="数据库实例" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="dataSourceName" label="接口来源" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="connectPath" label="链接地址" align="center"></el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="memo" label="备注" align="center"></el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="validFlag" label="是否有效" align="center">{{table.list.validFlag==1?'有效':'无效'}}</el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createTime" label="创建时间" align="center"></el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createNo" label="创建人编号" align="center"></el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateNo" label="更新人编号" align="center"></el-table-column>
       <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateMan" label="更新人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="165px" prop="updateTime" label="更新时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage" background>
        </el-pagination>
    </div>
    <el-dialog @close="closeDialog('baseData')" :visible.sync="dialogTableVisible">
        <div slot="title" class="titleBox">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
        </div>
       <el-form ref="baseData" label-width="90px" :rules="ruleValidate" :model="baseData">
         <el-row >
          <el-col :span="24" >
            <el-form-item prop="jkNo" label="接口编号">
              <el-input readonly  v-model="baseData.jkNo" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="jkName" label="接口名称">
              <el-input v-model="baseData.jkName" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
           <el-col :span="24">
            <el-form-item prop="dataSourceCode" label="匹配系统">
              <el-select v-model="baseData.dataSourceCode" filterable style="width:100%" collapse-tags size="small">
                <el-option v-for="item in dataSourceNameType" :key="item.index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="jkNum"  label="接口频率">
              <el-input v-model="baseData.jkNum" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item  prop="jkApi" label="接口路径">
             <el-input v-model="baseData.jkApi" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="jkUsed" label="接口作用">
             <el-input v-model="baseData.jkUsed" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="jkIp" label="接口IP">
              <el-input v-model="baseData.jkIp" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="jkCom" label="接口COM">
              <el-input v-model="baseData.jkCom" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="dataUser" label="数据库用户名">
              <el-input v-model="baseData.dataUser" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="dataPassword" label="数据库密码">
              <el-input v-model="baseData.dataPassword" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="dataInstance" label="数据库实例">
              <el-input v-model="baseData.dataInstance" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>

         <el-row>
          <el-col :span="24">
            <el-form-item prop="connectPath" label="链接地址">
              <el-input v-model="baseData.connectPath" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="vlidFlag" label="是否有效">
              <el-input v-model="baseData.validFlag" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>

       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="tableType === 0 ? addData('baseData'):modifyData('baseData')">确定</el-button>
        <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="cancel">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  data () {
    return {
      table: {
        list: []
      },
      dataLoading: false,
      icoreFilterFlag: true,
      tableType:0,
      tableTypeDsc:'新增接口信息',
      companyNameType:[],
      sectionNameType:[],
      sectionClass:[],
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      dialogTableVisible:false,
      baseData:{
              jkNo: '',
              createMan: '',
              dataSourceCode: '',
              dataSourceName: '',
              jkApi: '',
              connectPath: '',
              createNo: '',
              dataInstance: '',
              dataPassword: '',
              dataUser: '',
              jkCom: '',
              jkIp: '',
              jkName: '',
              jkNum: '',
              jkUsed: '',
              memo: '',
              postDesc: '',
              validFlag: ''

      },
      ruleValidate: {
/*        postName: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        companyNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
        deptNo: [{ required: true, message: '该项不能为空', trigger: 'change' }] */
      },
      dataSourceNameType:[],
      sectionNameType:[],
      deptNameType:[],
      multipleSelection: [],
      search: {
        jkName: ''
      },
      height_top:0,
      readonly: true,
    }
  },
  created() {
    this.height_top = window.innerHeight - 190;
  },
 mounted () {
  //获取接口来源
  this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4807}, {individualType: 'json'}).then(response => {
     this.dataSourceNameType = response.data;
   })
  this.getDataList();
  this.addScreen()
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 1) {
          sums[index] = data.length;
        }
      });
      return sums;
    },
    getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        // console.log(targetRow, 'targetRow');
        return targetRow ? targetRow[nameKey] : '';
      },
  //根据选中的接口，将接口来源写入数组
    selectDepartment(val){
      this.sectionNameType = []
      this.dataSourceNameType.find(item =>{
        if(val === item.baseCode){
          this.sectionNameType = item.sysOrgList
        }
      })
    },
    //根据选中的接口名称，查询所有数据展示，写入数组
    selectClass(val){
      this.sectionClass = []
      this.sectionNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionClass = item.sysOrgList
        }
      })
    },
    refresh () {
      this.getDataList();
    },
    //关闭弹窗
    closeDialog (formName){
      this.$refs[formName].resetFields();
      this.baseData = {}
    },
    //取消
    cancel () {
      this.dialogTableVisible = false
      this.baseData = {}
    },
    addData (formName) {
        let _this = this;
        _this.tableData = [];
         _this.$refs[formName].validate((valid) => {
          if (true) {
            _this.baseData.createMan = userName;
            _this.baseData.createNo = userId;
             _this.baseData.dataSourceName = this.getCode(this.baseData.dataSourceCode,this.dataSourceNameType,'baseCode','baseName')

             console.log(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsbaseinterfaces/add', _this.baseData, {individualType: 'json'}).then(res => {
                console.log(_this.baseData.postName)
                if (res.succeed) {
                  _this.dialogTableVisible = false;
                 this.$message.success('添加成功')
                 this.getDataList();
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
      },
    //修改弹窗
   modify () {
     if (this.multipleSelection.length !== 1) {
       this.$message.warning('请勾选您要修改的一条数据！')
       return
     }
     this.tableType = 1
     this.tableTypeDsc = '修改接口信息'
     this.baseData = Object.assign({},this.multipleSelection[0])
     this.selectDepartment(this.baseData.dataSourceName)
     this.selectClass(this.baseData.dataSourceName)
     this.dialogTableVisible = true
   },
   modifyData (formName) {
       let _this = this;
        _this.tableData = [];
         _this.$refs[formName].validate((valid) => {
          if (true) {
            _this.baseData.updateMan = userName;
            _this.baseData.updateNo = userId;
             _this.baseData.dataSourceName = this.getCode(this.baseData.dataSourceCode,this.dataSourceNameType,'baseCode','baseName')
             _this.axios.put('pass/baseManagement/v1/limsbaseinterfaces/'+_this.baseData.jkNo, _this.baseData, {individualType: 'json'}).then(res => {
                console.log(_this.baseData)
                if (res.succeed) {
                  _this.dialogTableVisible = false;
                 this.$message.success('修改成功')
                 this.getDataList();
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
   },
    recoverData () {
       let _this = this;
      if (_this.multipleSelection.length < 1) {
        this.$message.warning('请勾选您要启用的数据！')
        return
      }
      let tempArr = []
      this.multipleSelection.forEach(item =>{
        tempArr.push(item.id)
      })
        _this.axios.post('pass/baseManagement/v1/limsbaseinterfaces/recover', tempArr, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('启用成功');
         this.getDataList();
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
    },
    deleteData () {
       let _this = this;
      if (_this.multipleSelection.length < 1) {
        this.$message.warning('请勾选您要停用的数据！')
        return
      }
      let tempArr = []
      this.multipleSelection.forEach(item =>{
        tempArr.push(item.id)

      })
      this.axios.post('pass/baseManagement/v1/limsbaseinterfaces/stop', tempArr, {individualType: 'json'}).then(res => {
        console.log(tempArr)
        if (res.succeed) {
         this.$message.success('停用成功');
         this.getDataList();
        } else {
         this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    searchData () {
      this.page.pageNum = 2;
      this.getDataList()
    },
    reset () {
      let _this = this;
      _this.search.jkName = '';
      this.page.pageNum = 1;
      this.page.pageSize = 50;
      _this.searchData();
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    pageSizeChange (size) {
      this.page.pageSize = size;
      console.log(this.page.pageSize);
      this.getDataList();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      console.log( this.page.pageNum)
       this.getDataList();
    },
    addScreen () {
      this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    getDataList () {
      this.dataLoading = true;
      let obj= {
        params:{jkName: this.search.jkName},
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize
      }
      this.axios.get('pass/baseManagement/v1/limsbaseinterfaces/like/', {params:obj}).then(res => {
        if (res.code == 200 || res.code == 0) {
          console.log(res)
          this.table.list = res.data.list;
          this.page.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
        this.dataLoading = false;
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(res.message)
      });
    },
    tableRowClassName ({row, column, rowIndex, columnIndex}) {
      if (row.validFlag !== '1') {
        return 'error-row';
      }
      return '';
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>
