<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          size="small"
          clearable
          placeholder="输入名称或者编码"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.device_type"
          clearable
          filterable
          size="small"
          placeholder="设备类型"
          class="filter-item"
          style="width: 190px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in device_types" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <el-select
          v-model="query.region"
          clearable
          filterable
          size="small"
          placeholder="区域名称"
          class="filter-item"
          style="width: 190px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in regions" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <el-select
          v-model="query.is_config"
          clearable
          filterable
          size="small"
          placeholder="是否配置"
          class="filter-item"
          style="width: 190px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in is_configs" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="warning"
          icon="el-icon-download"
          @click="crud.doExport"
        >导出模型
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          icon="el-icon-upload2"
          size="mini"
          @click="uploadShow = true"
        >
          导入
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-refresh"
          @click="reload"
        >
          同步
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <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 v-if="false" label="设备标识" prop="device_id">
            <el-input v-model="form.device_id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备编码" prop="device_code">
            <el-input v-model="form.device_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备名字" prop="device_name">
            <el-input v-model="form.device_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备类型" prop="device_type">
            <el-select v-model="form.device_type" style="width: 370px" placeholder="请选择">
              <el-option
                v-for="item in device_types"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属区域" prop="region">
            <el-select
              v-model="form.region"
              style="width: 370px;"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in dict.region"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否配置" prop="is_config">
            <el-select v-model="form.is_config" style="width: 370px" placeholder="请选择">
              <el-option
                v-for="item in is_configs"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="顺序号" prop="seq_num">
            <el-input-number v-model="form.seq_num" value="1" :min="1" label="描述文字" />
          </el-form-item>
          <!--          <el-form-item label="生产厂家">-->
          <!--            <el-input v-model="form.manufacturer" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="厂家电话">-->
          <!--            <el-input v-model="form.manufacturer_phone" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <el-form-item label="描述信息" prop="description">
            <el-input v-model="form.remark" style="width: 380px;" rows="5" type="textarea" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.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 v-if="false" prop="device_id" label="设备标识" />
        <el-table-column sortable prop="device_code" label="设备编码" />
        <el-table-column sortable label="设备名字">
          <template slot-scope="scope">
            <div>{{ scope.row.device_name }}</div>
          </template>
        </el-table-column>
        <el-table-column label="设备类型">
          <template slot-scope="scope">
            <div>{{ scope.row.device_type_name }}</div>
          </template>
        </el-table-column>
        <el-table-column label="所属区域">
          <template slot-scope="scope">
            {{ dict.label.region[scope.row.region] }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="seq_num" label="顺序号" />
        <el-table-column prop="is_config" label="是否配置" width="75px">
          <template slot-scope="scope">
            <span v-if="scope.row.is_config=='true' ">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <!--        <el-table-column prop="manufacturer" label="生产厂家" />-->
        <!--        <el-table-column prop="manufacturer_phone" label="厂家电话" />-->
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-permission="['admin','device:edit','device:del']" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <udOperation
              style="display: inline"
              :data="scope.row"
              :permission="permission"
            />
            <el-button slot="right" size="mini" style="margin-left: -1px;margin-right: 2px" type="text">
              <router-link :to="'/devices/device/config/' + scope.row.device_code ">
                驱动配置
              </router-link>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <UploadDialog :dialog-show.sync="uploadShow" @tableChanged3="tableChanged3" />
  </div>
</template>

<script>
import crudDevice from '@/api/acs/device/device'
import CRUD, { crud, form, header, presenter } 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 { get } from '@/api/system/dictDetail'
import UploadDialog from '@/views/acs/device/UploadDialog'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'

const defaultForm = {
  manufacturer: null,
  manufacturer_phone: null,
  opc_server_id: null,
  opc_plc_id: null,
  is_active: null,
  is_delete: null,
  create_by: null,
  create_time: null,
  update_by: null,
  update_time: null,
  device_id: null,
  device_code: null,
  device_name: null,
  device_type: null,
  region: null,
  is_config: null,
  remark: null
}
export default {
  name: 'Device',
  dicts: ['region'],
  components: { pagination, crudOperation, rrOperation, udOperation, UploadDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备',
      url: 'api/device',
      idField: 'device_id',
      sort: 'device_id,desc',
      crudMethod: { ...crudDevice }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'device:add'],
        edit: ['admin', 'device:edit'],
        del: ['admin', 'device:del']
      },
      device_types: [],
      regions: [],
      is_configs: [],
      uploadShow: false,
      device_code: '',
      rules: {
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名字不能为空', trigger: 'blur' }
        ],
        device_type: [
          { required: true, message: '设备类型不能为空', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '所属区域不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // 获取设备类型字典
      get('device_type').then(data => {
        this.device_types = data.content
      })
      // 获取区域类型
      get('region').then(data => {
        this.regions = data.content
      })
      // 获取是否配置
      get('is_config').then(data => {
        this.is_configs = data.content
      })
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    tableChanged3() {
      this.crud.toQuery()
    },
    reload() {
      crudDevice.reload().then(res => {
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    downloadMethod() {
      this.beforeInit()
      this.downloadLoading = true
      download(this.url + 'api/device/download', this.params).then(result => {
        downloadFile(result, this.title + '数据', 'xlsx')
        this.downloadLoading = false
      }).catch(() => {
        this.downloadLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
