<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.tag"
          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.deviceId"
          clearable
          size="small"
          placeholder="所属连接"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.NetConn"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.dataType"
          clearable
          size="small"
          placeholder="数据类型"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.DataType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-input
          v-model="query.oid"
          clearable
          size="small"
          placeholder="请输入OID"
          style="width: 170px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.tagEnable"
          clearable
          size="small"
          placeholder="状态"
          class="filter-item"
          style="width: 170px"
          @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" />
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="640px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="100px">
        <el-row>
          <el-form-item label="变量名" prop="tag">
            <el-input v-model="form.tag" style="width: 400px" />
          </el-form-item>
          <el-form-item label="变量描述" prop="description">
            <el-input v-model="form.description" style="width: 400px" />
          </el-form-item>
          <el-form-item label="所属连接" prop="deviceId">
            <el-select v-model="form.deviceId" style="width: 150px" placeholder="请选择">
              <el-option
                v-for="item in optionsObj.NetConn"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="数据类型" prop="dataType">
            <el-select v-model="form.dataType" style="width: 150px" placeholder="请选择">
              <el-option
                v-for="item in optionsObj.DataType"
                :key="item.value"
                :label="item.label"
                :value="+item.value"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="OID" prop="oid">
            <el-input v-model="form.oid" style="width: 400px" />
          </el-form-item>
        </el-row>
        <el-row>
          <template v-if="form.dataType == 10 || form.dataType == 11">
            <el-form-item label="字符串类型" prop="octetStringType">
              <el-select v-model="form.octetStringType" style="width: 150px" placeholder="请选择">
                <el-option
                  v-for="item in optionsObj.SnmpOctetStringType"
                  :key="item.value"
                  :label="item.label"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
          </template>
        </el-row>
        <el-row>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="form.unit" style="width: 150px" />
          </el-form-item>
          <el-form-item label="小数位数" prop="decimals">
            <el-input-number
              v-model="form.decimals"
              controls-position="right"
              :min="1"
              :max="16"
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="数据处理脚本" prop="valueScript">
            <el-input
              v-model="form.valueScript"
              type="textarea"
              autosize
              clearable
              style="width: 400px"
            />
          </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"
              style="width: 150px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="报警启用" prop="alarmEnable">
            <el-switch
              v-model="form.alarmEnable"
              active-color="#409EFF"
              inactive-color="#E8E8E8"
              :active-value="true"
              :inactive-value="false"
              style="width: 150px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <template v-if="form.dataType === 1">
            <el-form-item label="报警类型" prop="boolAlarmType">
              <el-radio-group v-model="form.boolAlarmType">
                <el-radio
                  v-for="item in optionsObj.BoolAlarmType"
                  :label="+item.value"
                  :key="item.value"
                  >{{ item.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </template>
          <template v-if="form.dataType === 10">
            <el-form-item label="报警字符串" prop="alarmString">
              <el-input v-model="form.alarmString" style="width: 150px" />
            </el-form-item>
          </template>
          <template v-if="!(form.dataType === 1 || form.dataType === 10)">
            <el-form-item label="高报" prop="hAlarm">
              <el-input v-model="form.hAlarm" style="width: 150px" />
            </el-form-item>
            <el-form-item label="高高报" prop="hhAlarm">
              <el-input v-model="form.hhAlarm" style="width: 150px" />
            </el-form-item>
            <el-form-item label="低报" prop="lAlarm">
              <el-input v-model="form.lAlarm" style="width: 150px" />
            </el-form-item>
            <el-form-item label="低低报" prop="llAlarm">
              <el-input v-model="form.llAlarm" style="width: 150px" />
            </el-form-item>
            <el-form-item label="报警死区" prop="alarmDeadBand">
              <el-input v-model="form.alarmDeadBand" style="width: 150px" />
            </el-form-item>
          </template>
        </el-row>
        <el-row>
          <el-form-item label="模拟启用" prop="isSimulation">
            <el-switch
              v-model="form.isSimulation"
              active-color="#409EFF"
              inactive-color="#E8E8E8"
              :active-value="true"
              :inactive-value="false"
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="模拟值" prop="simulationValue">
            <el-input
              v-model="form.simulationValue"
              type="textarea"
              autosize
              clearable
              style="width: 400px"
            />
          </el-form-item>
        </el-row>
      </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 snmpTagTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        v-if="
          checkPer([
            'admin',
            'datacommunicationmanage:snmptag:edit',
            'datacommunicationmanage:snmptag: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 crudTag from '@/api/dataComm/snmp/tag'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { getOptions } from '@/api/dataComm/snmp/options'

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

const defaultForm = {
  description: '',
  deviceId: '',
  enable: true,
  oid: '',
  octetStringType: 0,
  id: '',
  tag: '',
  token: null,
  dataType: 8,
  typeStr: '',
  unit: null,
  decimals: 3,
  valueScript: '',
  alarmEnable: false,
  hhAlarm: 0,
  hAlarm: 0,
  lAlarm: 0,
  llAlarm: 0,
  alarmDeadBand: 0,
  boolAlarmType: 1,
  alarmString: '',
  isSimulation: false,
  simulationValue: '',
  enable: true,
}

export default {
  name: 'SnmpTag',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: 'SNMP变量',
      url: 'dataCommunicationManage/snmp/tag/getList',
      listOrder: [],
      crudMethod: { ...crudTag },
      optShow: {
        add: true,
        edit: true,
        del: true,
        import: true,
        export: true,
      },
    })
  },
  // dicts: ['ModbusTagStatus'],
  data() {
    return {
      snmpTagTableOptions,
      rules: {
        tag: [{ required: true, message: '请输入变量名', trigger: 'blur' }],
        deviceId: [{ required: true, message: '请选择所属设备', trigger: 'blur' }],
        dataType: [{ required: true, message: '请选择数据类型', trigger: 'blur' }],
        oid: [{ required: true, message: '请输入OID', trigger: 'blur' }],
        octetStringType: [{ required: true, message: '请选择字符出类型', trigger: 'blur' }],
      },
      permission: {
        add: ['admin', 'datacommunicationmanage:snmptag:add'],
        edit: ['admin', 'datacommunicationmanage:snmptag:edit'],
        del: ['admin', 'datacommunicationmanage:snmptag:del'],
      },
      optionsObj: {
        DataType: [],
        SnmpOctetStringType: [],
        NetConn: [],
        BoolAlarmType: [],
      },
      statusOpt: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    }
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {
    this.getOptions()
  },
  methods: {
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
  },
}
</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;
}
</style>
