<template>
  <div class="view-table-content">
    <div style="width: 100%">
      <div class="view-dataType-title">
        <div class="view-dataType-title-btn">
          <el-button type="success" @click="openDialog()" :disabled="currentNode && currentNode.type == 0">新增</el-button>
          <el-button type="warning" @click="remove(tableCheckItems)" :disabled="tableCheckItems.length == 0">删除</el-button>
        </div>
        <div class="view-dataType-title-search">
          <el-input placeholder="请输入名称" v-model="keyWordData" class="input1">
            <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
          </el-input>
        </div>
      </div>
      <div class="view-dataType-table">
        <LTable ref="table" @selection-change="selection" :defaultFetch="false" :fetch="fetch" :columns="columns" :dataSource="tableData" :options="options" :pagination="tableRequset"></LTable>
      </div>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px" :before-close="handleClose">
        <el-form ref="form" :inline="true" :model="form" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="IP地址" prop="ipAddress">
                <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="网段" prop="networkSegment">
                <el-input v-model="form.networkSegment" placeholder="请输入网段" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="子网掩码" prop="subnetMask">
                <el-input v-model="form.subnetMask" placeholder="请输入子网掩码" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="DNS" prop="dns"> <el-input v-model="form.dns" placeholder="请输入DNS" /> </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="网关" prop="gateway"> <el-input v-model="form.gateway" placeholder="请输入网关" /> </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12"
              ><el-form-item label="http" prop="http"> <el-input v-model="form.http" placeholder="请输入http" /> </el-form-item>
            </el-col>
            <el-col :span="12"
              ><el-form-item label="端口号" prop="port"> <el-input v-model="form.port" placeholder="请输入端口号" /> </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12"
              ><el-form-item label="用户名" prop="userName"> <el-input v-model="form.userName" placeholder="请输入用户名" /> </el-form-item>
            </el-col>
            <el-col :span="12"
              ><el-form-item label="密码" prop="password"> <el-input v-model="form.password" placeholder="请输入密码" /> </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCommunication, addCommunication, updateCommunication, removeCommunication } from '@/api/als/communication'
import { deepClone, debounce } from '@/utils/index'
export default {
  name: 'Communication',
  components: {},
  data() {
    // 这里存放数据
    return {
      dialogTitle: '新增',
      dialogVisible: false,
      keyWordData: '',
      communicationModelIdList: [],
      currentNodeKey: null,
      currentNode: null,
      menuTreeData: [],
      treeObj: {
        title: '所属机种',
        activityheight: '275px',
        searchIcon: false,
        configure: {
          children: 'children',
          label: 'label'
        }
      },
      typeTree: {
        children: 'children',
        label: 'label'
      },
      searchValue: '',
      columns: [
        {
          prop: 'name',
          label: '名称'
        },
        {
          prop: 'ipAddress',
          label: 'IP地址'
        },
        {
          prop: 'networkSegment',
          label: '网段'
        },
        {
          prop: 'subnetMask',
          label: '子网掩码'
        },
        {
          prop: 'dns',
          label: 'DNS'
        },
        {
          prop: 'gateway',
          label: '网关'
        },
        {
          prop: 'http',
          label: 'http'
        },
        {
          prop: 'port',
          label: '端口号'
        },
        {
          prop: 'userName',
          label: '用户名'
        },
        {
          prop: 'password',
          label: '密码'
        },
        {
          button: true,
          label: '操作',
          width: '240px',
          group: [
            {
              name: '编辑',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.handUpdate(row)
              }
            },
            {
              name: '删除',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.remove([row])
              }
            }
          ]
        }
      ],
      options: {
        stripe: true, // 斑马纹
        mutiSelect: true, // 多选框
        index: false, // 显示序号， 多选则 mutiSelect
        loading: false, // 表格动画
        initTable: false, // 是否一挂载就加载数据
        border: true,
        height: 'calc(100vh - 300px)'
      },
      tableCheckItems: [],
      tableData: [],
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 20,
        searchValue: ''
      },
      form: {
        id: '',
        name: '',
        ipAddress: '',
        networkSegment: '',
        subnetMask: '',
        gateway: '',
        http: '',
        port: '',
        userName: '',
        password: '',
        tenantId: '',
        version: '',
        delFlag: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      },
      debounceFn: debounce(this.fetch, 500)
    }
  },
  watch: {
    keyWord() {
      this.tableRequset.pageIndex = 1
      this.debounceFn()
    }
  },
  mounted() {
    this.getCommunicationAPI()
  },
  methods: {
    async removeCommunicationAPI(params) {
      try {
        const { success } = await removeCommunication(params)
        if (success != false) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          await this.getCommunicationAPI()
          this.handleClose()
        }
      } catch (error) {}
    },

    getTreeLeafData(list) {
      const newArr = []
      function getLeaf(data, arr) {
        data.forEach((e) => {
          if (e.type === 2) {
            arr.push(e)
          }
          if (e.children.length) {
            getLeaf(e.children, arr)
          }
        })
      }
      getLeaf(list, newArr)
      return newArr
    },

    async getCommunicationAPI(params) {
      if (this.$refs.table) this.$refs.table.clearSelection()
      const { keyWord } = this
      const { pageSize, pageIndex } = this.tableRequset
      const {
        data: { list, total }
      } = await getCommunication({ pageSize, pageNum: pageIndex, ...params })
      this.tableData = list
      this.tableRequset.total = total
    },

    fetch() {
      this.getCommunicationAPI()
    },

    searchClick() {
      this.getCommunicationAPI({ name: this.keyWordData })
    },

    async addCommunicationAPI() {
      try {
        const { success } = await addCommunication({ ...this.form })
        console.log(success)
        if (success != false) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getCommunicationAPI()
        }
      } catch (error) {}
    },

    async updateCommunicationAPI() {
      try {
        const { success } = await updateCommunication({ ...this.form })
        if (success != false) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getCommunicationAPI()
        }
      } catch (error) {}
    },

    treeNodeClick(data) {
      this.$refs.table.clearSelection()
      this.currentNodeKey = data.id
      this.currentNode = data
      this.aircaftModelIdList = this.getTreeLeafData(data.children.length ? data.children : [data]).map((e) => e.id)
      this.getAircraftAPI({ aircaftModelIdList: this.aircaftModelIdList })
    },

    openDialog() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
    },

    handleClose() {
      this.dialogVisible = false
      this.form = {
        id: '',
        parentId: '',
        parentName: '',
        name: '',
        ancestors: '',
        orderNum: '',
        tenantId: '',
        version: '',
        delFlag: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      }
    },

    handUpdate(row) {
      this.dialogTitle = '编辑'
      this.form = deepClone(row)
      this.dialogVisible = true
    },

    submit() {
      switch (this.dialogTitle) {
        case '编辑':
          this.updateCommunicationAPI()
          break
        case '新增':
          this.addCommunicationAPI()
          break
      }
    },

    selection(val) {
      this.tableCheckItems = val
    },

    remove(row) {
      this.$confirm('是否删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.removeCommunicationAPI(row.map((e) => e.id))
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../index.scss';
</style>
