<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">设备id</label>
        <el-input v-model="query.deviceId" clearable placeholder="设备id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">设备名称</label>
        <el-input v-model="query.deviceName" clearable placeholder="设备名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">从站站号</label>
        <el-input v-model="query.salveStationNumber" clearable placeholder="从站站号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker
          v-model="query.updateTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="设备名称" prop="deviceName">
            <el-input v-model="form.deviceName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备状态" prop="deviceStatus">
            <el-select v-model="form.deviceStatus" clearable filterable placeholder="请选择">
              <el-option
                v-for="item in dict.env_device_statue"
                :key="item.id"
                :label="item.label"
                :value="item.value"
                :disabled="(item.value === '2')"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="从站站号" prop="salveStationNumber">
            <el-input v-model="form.salveStationNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="采集通讯站号" prop="stationNumber">
            <el-input v-model="form.stationNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="采集通讯请求帧间隔(毫秒)" prop="requestFrameInterval">
            <el-input v-model="form.requestFrameInterval" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="采集通讯写值刷新间隔(毫秒)" prop="writeValueRefreshInterval">
            <el-input v-model="form.writeValueRefreshInterval" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="采集通讯2字节整数顺序" prop="twoByteIntOrder">
            <el-select v-model="form.twoByteIntOrder" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.two_byte_int_order"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="采集通讯4字节整数顺序" prop="fourByteIntOrder">
            <el-select v-model="form.fourByteIntOrder" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.four_or_float_byte_int_order"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单精度浮点数顺序" prop="floatOrder">
            <el-select v-model="form.floatOrder" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.four_or_float_byte_int_order"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="批量传输模拟量组包间隔" prop="analogQuantityGroupPacketInterval">
            <el-input v-model="form.analogQuantityGroupPacketInterval" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="批量传输模拟量组包最大长度" prop="analogQuantityGroupPacketMaxlength">
            <el-input v-model="form.analogQuantityGroupPacketMaxlength" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="批量传输数字量组包间隔" prop="digitalQuantityGroupPacketInterval">
            <el-input v-model="form.digitalQuantityGroupPacketInterval" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="批量传输数字量组包最大长度" prop="digitalQuantityGroupPacketMaxlength">
            <el-input v-model="form.digitalQuantityGroupPacketMaxlength" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="关联的通道channel">
            <el-select v-model="form.channelId" filterable placeholder="请选择">
              <el-option
                v-for="item in channel"
                :key="item.channelId"
                :label="item.channelName"
                :value="item.channelId"
              />
            </el-select>
          </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" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="deviceId" label="设备id" />
        <el-table-column prop="deviceName" label="设备名称" />
        <el-table-column prop="salveStationNumber" label="从站站号" />
        <el-table-column prop="stationNumber" label="采集通讯站号" />
        <el-table-column prop="requestFrameInterval" label="采集通讯请求帧间隔(毫秒)" />
        <el-table-column prop="writeValueRefreshInterval" label="采集通讯写值刷新间隔(毫秒)" />
        <el-table-column prop="twoByteIntOrder" label="采集通讯2字节整数顺序">
          <template slot-scope="scope">
            {{ dict.label.two_byte_int_order[scope.row.twoByteIntOrder] }}
          </template>
        </el-table-column>
        <el-table-column prop="fourByteIntOrder" label="采集通讯4字节整数顺序">
          <template slot-scope="scope">
            {{ dict.label.four_or_float_byte_int_order[scope.row.fourByteIntOrder] }}
          </template>
        </el-table-column>
        <el-table-column prop="floatOrder" label="单精度浮点数顺序">
          <template slot-scope="scope">
            {{ dict.label.four_or_float_byte_int_order[scope.row.floatOrder] }}
          </template>
        </el-table-column>
        <el-table-column prop="analogQuantityGroupPacketInterval" label="批量传输模拟量组包间隔" />
        <el-table-column prop="analogQuantityGroupPacketMaxlength" label="批量传输模拟量组包最大长度" />
        <el-table-column prop="digitalQuantityGroupPacketInterval" label="批量传输数字量组包间隔" />
        <el-table-column prop="digitalQuantityGroupPacketMaxlength" label="批量传输数字量组包最大长度" />
        <el-table-column prop="channelId" label="关联的通道channel_id">
          <!--          <template slot-scope="scope">-->
          <!--            {{ scope.row.dmModbusChannel.channelName }}-->
          <!--          </template>-->
        </el-table-column>
        <el-table-column prop="deviceStatus" label="设备状态">
          <template slot-scope="scope">
            {{ dict.label.env_device_statue[scope.row.deviceStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="updateBy" label="更新人" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column v-if="checkPer(['admin','dmModbusDevice:edit','dmModbusDevice:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmModbusDevice from '@/api/dmModbusDevice'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import dateRangePicker from '@/components/DateRangePicker'
import crudDmModbusChannel from '@/api/dmModbusChannel'

const defaultForm = { deviceId: null, deviceName: null, salveStationNumber: null, stationNumber: null, requestFrameInterval: null, writeValueRefreshInterval: null, twoByteIntOrder: null, fourByteIntOrder: null, floatOrder: null, analogQuantityGroupPacketInterval: null, analogQuantityGroupPacketMaxlength: null, digitalQuantityGroupPacketInterval: null, digitalQuantityGroupPacketMaxlength: null, channelId: null, createBy: null, updateBy: null, createTime: null, updateTime: null, deviceStatus: null }
export default {
  name: 'DmModbusDevice',
  components: { pagination, crudOperation, rrOperation, udOperation, dateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['two_byte_int_order', 'four_or_float_byte_int_order', 'env_device_statue'],
  cruds() {
    return CRUD({ title: '通道下的设备管理', url: 'api/dmModbusDevice', idField: 'deviceId', sort: 'deviceId,desc', crudMethod: { ...crudDmModbusDevice }})
  },
  data() {
    return {
      channel: [],
      permission: {
        add: ['admin', 'dmModbusDevice:add'],
        edit: ['admin', 'dmModbusDevice:edit'],
        del: ['admin', 'dmModbusDevice:del']
      },
      rules: {
        deviceStatus: [
          { required: true, message: '设备状态不能为空', trigger: 'blur' }
        ],
        deviceId: [
          { required: true, message: '设备id不能为空', trigger: 'blur' }
        ],
        deviceName: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        salveStationNumber: [
          { required: true, message: '从站站号不能为空', trigger: 'blur' }
        ],
        stationNumber: [
          { required: true, message: '采集通讯站号不能为空', trigger: 'blur' }
        ],
        requestFrameInterval: [
          { required: true, message: '采集通讯请求帧间隔(毫秒)不能为空', trigger: 'blur' }
        ],
        writeValueRefreshInterval: [
          { required: true, message: '采集通讯写值刷新间隔(毫秒)不能为空', trigger: 'blur' }
        ],
        twoByteIntOrder: [
          { required: true, message: '采集通讯2字节整数顺序不能为空', trigger: 'blur' }
        ],
        fourByteIntOrder: [
          { required: true, message: '采集通讯4字节整数顺序不能为空', trigger: 'blur' }
        ],
        floatOrder: [
          { required: true, message: '单精度浮点数顺序不能为空', trigger: 'blur' }
        ],
        analogQuantityGroupPacketInterval: [
          { required: true, message: '批量传输模拟量组包间隔不能为空', trigger: 'blur' }
        ],
        analogQuantityGroupPacketMaxlength: [
          { required: true, message: '批量传输模拟量组包最大长度不能为空', trigger: 'blur' }
        ],
        digitalQuantityGroupPacketInterval: [
          { required: true, message: '批量传输数字量组包间隔不能为空', trigger: 'blur' }
        ],
        digitalQuantityGroupPacketMaxlength: [
          { required: true, message: '批量传输数字量组包最大长度不能为空', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '关联的通道channel_id不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'deviceId', display_name: '设备id' },
        { key: 'deviceName', display_name: '设备名称' },
        { key: 'salveStationNumber', display_name: '从站站号' }
      ]
    }
  },
  created() {
    this.getChannelData()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getChannelData() {
      const param = 'page=0&size=9999'
      const _that = this
      crudDmModbusChannel.get(param).then(res => {
        _that.channel = res.content
      })
    }
  }
}
</script>

<style scoped>

</style>
