<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.channelId" clearable placeholder="信道id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.channelName" clearable placeholder="通道名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.baudRate" clearable placeholder="波特率" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.driveId" clearable filterable placeholder="请选择关联的驱动" class="filter-item" @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in drive"
            :key="item.driveId"
            :label="item.name"
            :value="item.driveId"
          />
        </el-select>
        <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="channelName">
            <el-input v-model="form.channelName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="串口参数" prop="serialPort">
            <el-select v-model="form.serialPort" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.channel_serial_port"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="波特率" prop="baudRate">
            <el-select v-model="form.baudRate" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.channel_baud_rate"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数据位" prop="dataBits">
            <el-select v-model="form.dataBits" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.channel_data_bits"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="停止位" prop="stopBits">
            <el-select v-model="form.stopBits" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.channel_stop_bits"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="校验位" prop="verifyBits">
            <el-select v-model="form.verifyBits" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.channel_verify_bits"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="流控制" prop="streamControl">
            <el-select v-model="form.streamControl" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.channel_stream_control"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="超时时间(ms)" prop="timeout">
            <el-input v-model="form.timeout" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="关联驱动">
            <el-select v-model="form.driveId" clearable filterable placeholder="请选择">
              <el-option
                v-for="item in drive"
                :key="item.driveId"
                :label="item.name"
                :value="item.driveId"
              />
            </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="channelId" label="信道id" />
        <el-table-column prop="channelName" label="通道名称" />
        <el-table-column prop="serialPort" label="串口参数">
          <template slot-scope="scope">
            {{ dict.label.channel_serial_port[scope.row.serialPort] }}
          </template>
        </el-table-column>
        <el-table-column prop="baudRate" label="波特率">
          <template slot-scope="scope">
            {{ dict.label.channel_baud_rate[scope.row.baudRate] }}
          </template>
        </el-table-column>
        <el-table-column prop="dataBits" label="数据位">
          <template slot-scope="scope">
            {{ dict.label.channel_data_bits[scope.row.dataBits] }}
          </template>
        </el-table-column>
        <el-table-column prop="stopBits" label="停止位">
          <template slot-scope="scope">
            {{ dict.label.channel_stop_bits[scope.row.stopBits] }}
          </template>
        </el-table-column>
        <el-table-column prop="verifyBits" label="校验位">
          <template slot-scope="scope">
            {{ dict.label.channel_verify_bits[scope.row.verifyBits] }}
          </template>
        </el-table-column>
        <el-table-column prop="streamControl" label="流控制">
          <template slot-scope="scope">
            {{ dict.label.channel_stream_control[scope.row.streamControl] }}
          </template>
        </el-table-column>
        <el-table-column prop="timeout" label="超时时间" />
        <el-table-column prop="driveId" label="关联驱动">
          <!--          <template slot-scope="scope">-->
          <!--            {{ scope.row.dmModbusDrive.name }}-->
          <!--          </template>-->
        </el-table-column>
        <el-table-column prop="updateBy" label="更新人" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column v-if="checkPer(['admin','dmModbusChannel:edit','dmModbusChannel: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 crudDmModbusChannel from '@/api/dmModbusChannel'
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 { get } from '@/api/dmModbusDrive'

const defaultForm = { channelId: null, channelName: null, serialPort: null, baudRate: null, dataBits: null, stopBits: null, verifyBits: null, streamControl: null, timeout: null, driveId: null, createBy: null, updateBy: null, createTime: null, updateTime: null,
  dmModbusDevice: { deviceId: null, deviceName: null }}
export default {
  name: 'DmModbusChannel',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['channel_serial_port', 'channel_baud_rate', 'channel_data_bits', 'channel_stop_bits', 'channel_verify_bits', 'channel_stream_control'],
  cruds() {
    return CRUD({ title: 'channel', url: 'api/dmModbusChannel', idField: 'channelId', sort: 'channelId,desc', crudMethod: { ...crudDmModbusChannel }})
  },
  data() {
    return {
      drive: [],
      permission: {
        add: ['admin', 'dmModbusChannel:add'],
        edit: ['admin', 'dmModbusChannel:edit'],
        del: ['admin', 'dmModbusChannel:del']
      },
      rules: {
        channelId: [
          { required: true, message: '信道id不能为空', trigger: 'blur' }
        ],
        channelName: [
          { required: true, message: '通道名称不能为空', trigger: 'blur' }
        ],
        serialPort: [
          { required: true, message: '串口参数不能为空', trigger: 'blur' }
        ],
        baudRate: [
          { required: true, message: '波特率不能为空', trigger: 'blur' }
        ],
        dataBits: [
          { required: true, message: '数据位不能为空', trigger: 'blur' }
        ],
        stopBits: [
          { required: true, message: '停止位不能为空', trigger: 'blur' }
        ],
        verifyBits: [
          { required: true, message: '校验位不能为空', trigger: 'blur' }
        ],
        streamControl: [
          { required: true, message: '流控制不能为空', trigger: 'blur' }
        ],
        timeout: [
          { required: true, message: '超时时间不能为空', trigger: 'blur' }
        ],
        dmModbusDevice: [
          { required: true, message: '关联驱动id不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'channelId', display_name: '信道id' },
        { key: 'channelName', display_name: '通道名称' },
        { key: 'baudRate', display_name: '波特率' },
        { key: 'driveId', display_name: '关联驱动id' }
      ]
    }
  },
  created() {
    this.getDrive()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getDrive() {
      const param = 'page=0&size=9999'
      // eslint-disable-next-line no-unused-vars
      const _that = this
      get(param).then(res => {
        _that.drive = res.content
      })
    }
  }
}
</script>

<style scoped>

</style>
