<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('device.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_type"
              clearable
              filterable
              size="small"
              :placeholder="$t('device.placeholder.device_type')"
              class="filter-item"
              style="width: 200px"
              @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="$t('device.device_table_title.region')">
            <el-select
              v-model="query.region"
              clearable
              filterable
              size="small"
              :placeholder="$t('device.device_table_title.region')"
              class="filter-item"
              style="width: 200px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in regions" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否配置">
            <el-select
              v-model="query.is_config"
              clearable
              filterable
              size="small"
              placeholder="是否配置"
              class="filter-item"
              style="width: 200px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in is_configs" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          icon="el-icon-upload2"
          size="mini"
          @click="uploadShow = true"
        >
          {{ $t('auto.common.import') }}
        </el-button>
        <!-- <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-refresh"
          @click="reload"
        >
          {{ $t('auto.common.synchronous') }}
        </el-button> -->
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu"
        :title="crud.status.title"
        width="1050px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <el-row>
            <el-col :span="12">
              <div class="grid-content bg-purple" />
              <el-form-item v-if="false" :label="$t('device.table_title.device_identification')" prop="device_id">
                <el-input v-model="form.device_id" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('device.device_table_title.device_encoding')" prop="device_code">
                <el-input v-model="form.device_code" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('device.device_table_title.device_name')" prop="device_name">
                <el-input v-model="form.device_name" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('device.device_dialog.in_device_name')" prop="in_device_name">
                <el-input v-model="form.in_device_name" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('device.device_dialog.en_device_name')" prop="en_device_name">
                <el-input v-model="form.en_device_name" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('device.device_dialog.zh_device_name')" prop="zh_device_name">
                <el-input v-model="form.zh_device_name" style="width: 370px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="grid-content bg-purple-light" />
              <el-form-item :label="$t('device.device_table_title.device_type')" prop="device_type">
                <el-select v-model="form.device_type" style="width: 370px" :placeholder="$t('device.placeholder.please_select')">
                  <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="$t('device.device_table_title.region')" prop="region">
                <el-select v-model="form.region" style="width: 370px" :placeholder="$t('device.placeholder.please_select')">
                  <el-option
                    v-for="item in regions"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('device.device_table_title.sequence_number')" 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="$t('device.device_table_title.descriptive_information')" prop="description">
                <el-input v-model="form.remark" style="width: 370px;" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">{{ $t('auto.common.Cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('auto.common.Confirm') }}</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="$t('device.device_table_title.device_identification')" />
        <el-table-column sortable prop="device_code" :label="$t('device.device_table_title.device_encoding')" :min-width="flexWidth('device_code',crud.data,$t('device.device_table_title.device_encoding'))" />
        <el-table-column sortable :label="$t('device.device_table_title.device_name')" :prop="$langPre.computedProp('device_name')" :min-width="flexWidth($langPre.computedProp('device_name'),crud.data,$t('device.device_table_title.device_name'))" />
        <el-table-column prop="device_type_name" :label="$t('device.device_table_title.device_type')" :min-width="flexWidth('device_type_name',crud.data,$t('device.device_table_title.device_type'))">
          <template slot-scope="scope">
            <div>{{ scope.row.device_type_name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="region_name" :label="$t('device.device_table_title.region')" :min-width="flexWidth('region_name',crud.data,$t('device.device_table_title.region'))">
          <template slot-scope="scope">
            <div>{{ scope.row.region_name }}</div>
          </template>
        </el-table-column>
        <el-table-column sortable prop="seq_num" :label="$t('device.device_table_title.sequence_number')" :min-width="flexWidth('seq_num',crud.data,$t('device.device_table_title.sequence_number'))" />
        <el-table-column prop="is_config" :label="$t('device.device_table_title.configured')" :min-width="flexWidth('is_config',crud.data,$t('device.device_table_title.configured'))">
          <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="$t('device.device_table_title.descriptive_information')" :min-width="flexWidth('remark',crud.data,$t('device.device_table_title.descriptive_information'))" />
        <el-table-column v-permission="['admin','device:edit','device:del']" :label="$t('auto.common.Operate')" 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" icon="el-icon-edit-outline">
              <router-link :to="'/devices/device/config/' + scope.row.device_code ">
                {{ $t('auto.common.driver_configuration') }}
              </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 '@/views/system/dict/dictDetail'
import UploadDialog from '@/views/acs/device/UploadDialog'
import i18n from '@/i18n'

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,
  zh_device_name: null,
  en_device_name: null,
  in_device_name: null
}
export default {
  name: 'Device',
  components: { pagination, crudOperation, rrOperation, udOperation, UploadDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: i18n.t('device.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: [],
      uploadShow: false,
      regions: [],
      is_configs: [],
      rules: {
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名字不能为空', trigger: 'blur' }
        ],
        device_type: [
          { required: true, message: '设备类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // 获取设备类型字典
      get('device_type').then(data => {
        this.device_types = data.content
      })
      // 获取区域类型
      get('region_type').then(data => {
        this.regions = data.content
      })
      // 是否
      get('TrueOrFalse').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)
      })
    }
  }
}
</script>

<style scoped>

</style>
