<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.connId"
          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.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" />
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="600px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="180px">
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="设备描述" prop="description">
          <el-input v-model="form.description" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="所属连接" prop="connId">
          <el-select v-model="form.connId" style="width: 200px" placeholder="请选择">
            <el-option
              v-for="item in optionsObj.NetConn"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="站号" prop="station">
          <el-input v-model="form.station" style="width: 120px;" />范围：1-255
        </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 pelcoDeviceTableOptions"
        :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:pelcodevice:edit', 'datacommunicationmanage:pelcodevice: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 crudDevice from '@/api/dataComm/pelco/device'
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/pelco/options'

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

const defaultForm = {
  id: '0',
  name: '',
  description: '',
  connId: null,
  enable: true,
  station: 1
}

export default {
  name: 'PelcoDevice',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: 'Pelco设备',
      url: 'dataCommunicationManage/pelco/device/getList',
      listOrder:[],
      crudMethod: { ...crudDevice }
    })
  },
  data() {
    return {
      pelcoDeviceTableOptions,
      rules: {
        name: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
        connId: [{ required: true, message: '请选择所属连接', trigger: 'blur' }],
        station: [{ required: true, message: '请输入站号', trigger: 'blur' }],
      },
      permission: {
        add: ['admin', 'datacommunicationmanage:pelcodevice:add'],
        edit: ['admin', 'datacommunicationmanage:pelcodevice:edit'],
        del: ['admin', 'datacommunicationmanage:pelcodevice:del']
      },
      optionsObj: {
        NetConn: [],
      },
      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>
