<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， 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="mini" label-width="80px">
          <el-form-item :label="$t('SysParam.table.code')" prop="code">
            <el-input v-model="form.code" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('SysParam.table.name')" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('SysParam.table.values')" prop="value">
            <el-input v-model="form.value" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('SysParam.table.description')" 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">{{ $t('common.Cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('common.Confirm') }}</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="false" prop="id" label="id" />
        <el-table-column prop="code" :label="$t('SysParam.table.code')" min-width="130" show-overflow-tooltip />
        <el-table-column :prop="$langPre.computedProp('name')" :label="$t('SysParam.table.name')" min-width="120" show-overflow-tooltip />
        <el-table-column prop="value" :label="$t('SysParam.table.values')" min-width="270" show-overflow-tooltip />
        <el-table-column prop="remark" :label="$t('SysParam.table.description')" />
        <el-table-column v-permission="['admin','param:edit','param:del']" :label="$t('common.Operate')" 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 crudParam from '@/views/system/param/param'
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'

const defaultForm = {
  id: null,
  code: null,
  name: null,
  value: null,
  remark: null,
  is_active: true
}
export default {
  name: 'Param',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'menu.SystemParam', url: 'api/param', idField: 'id', sort: 'id,desc', crudMethod: { ...crudParam },
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'param:add'],
        edit: ['admin', 'param:edit'],
        del: ['admin', 'param:del']
      },

      rules: {
        id: [
          { required: true, message: this.$t('SysParam.rules.NotNull'), trigger: 'blur' }
        ],
        code: [
          { required: true, message: this.$t('SysParam.rules.NotNull'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('SysParam.rules.NotNull'), trigger: 'blur' }
        ],
        value: [
          { required: true, message: this.$t('SysParam.rules.NotNull'), trigger: 'blur' }
        ]
      }
    }
  },
  // computed: {
  //   computedProp() {
  //     // 在计算属性中拼接属性名
  //     console.log(localStorage.getItem('lang'))
  //     return localStorage.getItem('lang') + '_name'
  //   }
  // },
  created() {
    // this.webSocket()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    webSocket() {
      const that = this
      if (typeof (WebSocket) === 'undefined') {
        this.$notify({
          title: '提示',
          message: '当前浏览器无法接收实时报警信息，请使用谷歌浏览器！',
          type: 'warning',
          duration: 0
        })
      } else {
        const id = '123'
        // 获取token保存到vuex中的用户信息，此处仅适用于本项目，注意删除或修改
        // 实例化socket，这里我把用户名传给了后台，使后台能判断要把消息发给哪个用户，其实也可以后台直接获取用户IP来判断并推送
        const socketUrl = process.env.VUE_APP_WS_API + id
        this.socket = new WebSocket(socketUrl)
        // 监听socket打开
        this.socket.onopen = function() {
          console.log('浏览器WebSocket已打开')
          that.socket.send('测试客户端发送消息')
        }

        // 监听socket消息接收

        this.socket.onmessage = function(msg) {
          console.log(msg)

          // 转换为json对象
          /*  const data = JSON.parse(msg.data);*/
        }

        // 监听socket错误
        this.socket.onerror = function() {
          that.$notify({
            title: '错误',
            message: '服务器错误，无法接收实时报警信息',
            type: 'error',
            duration: 0
          })
        }
        // 监听socket关闭
        this.socket.onclose = function() {
          console.log('WebSocket已关闭')
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
