<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="请输入连接名称"
          style="width: 170px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.description"
          clearable
          size="small"
          placeholder="请输入连接描述"
          style="width: 170px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.enable"
          clearable
          size="small"
          placeholder="状态"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in statusOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          v-if="
            checkPer([
              'admin',
              'datacommunicationmanage:micropointtpinetconn:edit',
              'datacommunicationmanage:micropointtpinetconn:del',
            ])
          "
          class="filter-item"
          size="mini"
          type="info"
          icon="el-icon-refresh"
          @click="refreshDriver"
        >
          重启驱动
        </el-button>
      </crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="连接名称" prop="name">
          <el-input v-model="form.name" style="width: 140px" />
        </el-form-item>
        <el-form-item label="连接描述" prop="description">
          <el-input v-model="form.description" style="width: 370px" />
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress">
          <el-input v-model="form.ipAddress" style="width: 190px" />
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port" style="width: 90px" />
        </el-form-item>
        <el-form-item label="计算机号" prop="pcNum">
          <el-input v-model="form.pcNum" style="width: 90px" />
        </el-form-item>
        <el-form-item label="地图路径" prop="mapPath">
          <el-input v-model="form.mapPath" style="width: 90px" />
        </el-form-item>
        <el-form-item label="循环时间" prop="cycleTime">
          <el-input v-model="form.cycleTime" style="width: 140px">
            <template slot="append">ms</template>
          </el-input>
        </el-form-item>
        <el-form-item label="连接超时" prop="connectTimeOut">
          <el-input v-model="form.connectTimeOut" style="width: 140px">
            <template slot="append">ms</template>
          </el-input>
        </el-form-item>
        <el-form-item label="接收超时" prop="receiveTimeOut">
          <el-input v-model="form.receiveTimeOut" style="width: 140px">
            <template slot="append">ms</template>
          </el-input>
        </el-form-item>
        <el-form-item label="发送超时" prop="sendTimeOut">
          <el-input v-model="form.sendTimeOut" style="width: 140px">
            <template slot="append">ms</template>
          </el-input>
        </el-form-item>
        <el-form-item label="启用" prop="enable">
          <el-switch
            v-model="form.enable"
            active-color="#409EFF"
            inactive-color="#E8E8E8"
            :active-value="true"
            :inactive-value="false"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU"
          >确认
        </el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in microPointTPINetConnTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
        <!-- <template slot-scope="scope">
          <span v-if="item.prop == 'enabled'">
            <el-switch
              v-model="scope.row.enabled"
              :disabled="scope.row.id === 1"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.enabled)"
            />
          </span>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        v-if="
          checkPer([
            'admin',
            'datacommunicationmanage:micropointtpinetconn:edit',
            'datacommunicationmanage:micropointtpinetconn:del',
          ])
        "
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudNetConn from '@/api/dataComm/microPointTPI/netConn'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'

// 表头配置
import { microPointTPINetConnTableOptions } from '../../tableConfig'

const defaultForm = {
  id: '0',
  name: '',
  description: '',
  ipAddress: '',
  port: '',
  pcNum: '',
  mapPath: '',
  cycleTime: 1000,
  connectTimeOut: 3000,
  receiveTimeOut: 1000,
  sendTimeOut: 1000,
  isLongConnection: true,
  enable: true,
}

export default {
  name: 'MicroPointTPINetConn',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
  },
  cruds() {
    return CRUD({
      title: '西南微波周界网络连接',
      url: 'dataCommunicationManage/microPointTPI/netConn/getList',
      listOrder: [],
      crudMethod: { ...crudNetConn },
    })
  },
  data() {
    return {
      microPointTPINetConnTableOptions,
      rules: {
        name: [{ required: true, message: '请输入连接名称', trigger: 'blur' }],
        ipAddress: [{ required: true, message: '请输入IP地址', trigger: 'blur' }],
        port: [{ required: true, message: '请输入端口号', trigger: 'blur' }],
        pcNum: [{ required: true, message: '请输入计算机号', trigger: 'blur' }],
        mapPath: [{ required: true, message: '请输入地图路径', trigger: 'blur' }],
        cycleTime: [{ required: true, message: '请输入循环时间', trigger: 'blur' }],
        connectTimeOut: [{ required: true, message: '请输入连接超时', trigger: 'blur' }],
        receiveTimeOut: [{ required: true, message: '请输入接收超时', trigger: 'blur' }],
        sendTimeOut: [{ required: true, message: '请输入发送超时', trigger: 'blur' }],
      },
      permission: {
        add: ['admin', 'datacommunicationmanage:micropointtpinetconn:add'],
        edit: ['admin', 'datacommunicationmanage:micropointtpinetconn:edit'],
        del: ['admin', 'datacommunicationmanage:micropointtpinetconn:del'],
      },
      statusOpt: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    }
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {},
  methods: {
    refreshDriver() {
      this.$confirm('此操作将重启驱动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          crudNetConn.refreshDriver().then((res) => {
            if (res.code === 20000) {
              this.$message({
                type: 'success',
                message: '重启成功!',
              })
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
::v-deep .el-input-group__append,
.el-input-group__prepend {
  padding: 0 8px;
}
</style>
