<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-suffix=":"
        >
          <el-form-item :label="$t('opc.placeholder.name_code')">
            <el-input
              v-model="query.blurry"
              size="small"
              clearable
              :placeholder="$t('device.placeholder.enter_name_code')"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('device.placeholder.device_type')">
            <el-select
              v-model="query.device_types"
              clearable
              filterable
              size="small"
              :placeholder="$t('device.placeholder.device_type')"
              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-form-item>
          <el-form-item label="Server">
            <el-select
              v-model="query.opc_id"
              clearable
              filterable
              size="small"
              placeholder="Server"
              class="filter-item"
              style="width: 190px"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dataOpcservers"
                :key="item.opc_id"
                :label="item.opc_name"
                :value="item.opc_id"
              />
            </el-select>
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>

      <crudOperation>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          size="mini"
          :loading="downLoadcsvLoading"
          @click="doExportCSV()"
        >
          {{ $t('common.Export') }} CSV
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="success"
          icon="el-icon-download"
          size="mini"
          :loading="downLoadcsvLoadingSmart"
          @click="doExportSmartCSV()"
        >
          {{ $t('common.Export') }} SMARTCSV
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-download"
          size="mini"
          :loading="downLoadcsvLoadingFX5U"
          @click="doExportFX5UCSV()"
        >
          {{ $t('common.Export') }} FX5UCSV
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <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="id" />
        <el-table-column prop="device_code" :label="$t('device.device_table_title.device_encoding')" />
        <el-table-column prop="device_type" :label="$t('device.device_table_title.device_type')" />
        <el-table-column :label="$t('protocol.table_title.driver_code')" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>{{ scope.row.driver_code }}</div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('protocol.table_title.extra_code')" min-width="130" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>{{ scope.row.extra_code }}</div>
          </template>
        </el-table-column>

        <el-table-column :label="$t('protocol.table_title.extra_name')">
          <template slot-scope="scope">
            <div>{{ scope.row.extra_name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="opc_code" label="OpcServer" />
        <el-table-column prop="plc_code" label="OpcPlc" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import { get } from '@/views/system/dict/dictDetail'
import { selectOpcList } from '@/api/acs/device/opc'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'
import pagination from '@crud/Pagination'

export default {
  name: 'Protocol',
  components: { crudOperation, rrOperation, pagination },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '设备协议', url: 'api/device/protocol', idField: 'id', sort: 'id,desc',
      optShow: {
        download: false
      }
    })
  },
  data() {
    return {
      downLoadcsvLoading: false,
      downLoadcsvLoadingSmart: false,
      downLoadcsvLoadingFX5U: false,
      device_types: [],
      dataOpcservers: [],
      permission: {
        add: ['admin', 'param:add'],
        edit: ['admin', 'param:edit'],
        del: ['admin', 'param:del']
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // 获取设备类型字典
      get('device_type').then(data => {
        this.device_types = data.content
      })
      selectOpcList().then(data => {
        this.dataOpcservers = data
      })
    })
  },
  methods: {
    doExportCSV() {
      this.downLoadcsvLoading = true
      download(this.crud.url + '/downloadCSV', this.crud.getQueryParams()).then(result => {
        downloadFile(result, crud.title + '数据', 'csv')
        this.downLoadcsvLoading = false
      }).catch(() => {
        this.downLoadcsvLoading = false
      })
    },
    doExportSmartCSV() {
      this.downLoadcsvLoadingSmart = true
      download(this.crud.url + '/downloadSmartCSV', this.crud.getQueryParams()).then(result => {
        downloadFile(result, crud.title + '数据', 'csv')
        this.downLoadcsvLoadingSmart = false
      }).catch(() => {
        this.downLoadcsvLoadingSmart = false
      })
    },
    doExportFX5UCSV() {
      this.downLoadcsvLoadingFX5U = true
      download(this.crud.url + '/downloadFX5UCSV', this.crud.getQueryParams()).then(result => {
        downloadFile(result, crud.title + '数据', 'csv')
        this.downLoadcsvLoadingFX5U = false
      }).catch(() => {
        this.downLoadcsvLoadingSmart = false
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeOpc(val) {
      this.dataOpcservers.forEach(item => {
        if (item.id === val) {
          this.code = item.opc_code
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
