<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
        重置
      </el-button>
    </div>
    <el-table ref="multipleTable" :data="varList" :header-cell-style="{'font-weight': 'bold','color': '#000'}" tooltip-effect="dark" border fit highlight-current-row>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="CORP_NAME" label="企业名称" />
      <el-table-column prop="NAME" label="设备名称" />
      <el-table-column prop="DEVICEID" label="设备ID" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.DEVICECORP_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.DEVICECORP_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <template>
          <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        </template>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="pd" label-width="150px" style="width: 500px;">
        <el-form-item label="企业" prop="CORPINFO_ID">
          <el-select v-model="pd.CORPINFO_ID" clearable placeholder="请选择企业" style="width: 100%">
            <el-option v-for="item in corpList" :key="item.CORPINFO_ID" :label="item.CORP_NAME" :value="item.CORPINFO_ID" />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="NAME">
          <el-input v-model="pd.NAME" placeholder="这里输入名称..." />
        </el-form-item>
        <el-form-item label="设备ID" prop="DEVICEID">
          <el-input v-model="pd.DEVICEID" placeholder="这里输入名称..." />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      varList: [],
      roleGroupList: [
      ],
      KEYWORDS: '',
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {

        CORPINFO_ID: [
          { required: true, message: '企业不能为空', trigger: 'blur' }
        ],
        NAME: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        DEVICEID: [
          { required: true, message: '设备ID不能为空', trigger: 'blur' }
        ]
      },
      pd: {
      },
      fromExcel: false, // 从excel导入权限
      toExcel: false,	// 导出到excel权限
      add: false,		// 新增按钮
      del: false,		// 删除按钮
      edit: false,	// 修改按钮
      corpList: []
    }
  },
  created() {
    this.getList()
    this.getCorpList()
    this.hasButton()
  },
  methods: {
    getList() {
      this.listLoading = true
      requestFN(
        '/devicecorp/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
      })
        .catch((e) => {
          this.listLoading = false
        })
    },

    handleAdd() {
      this.dialogFormEdit = true
      this.pd = {}
      // this.getroleList()
      this.dialogType = 'add'
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/devicecorp/' + this.dialogType,
            {
              DEVICECORP_ID: this.pd.DEVICECORP_ID,
              CORPINFO_ID: this.pd.CORPINFO_ID,
              NAME: this.pd.NAME,
              DEVICEID: this.pd.DEVICEID,
              tm: new Date().getTime()
            }
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.varList = []
            this.listQuery.page = 1
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/devicecorp/delete',
          {
            DEVICECORP_ID: id
          }
        ).then((data) => {
          if (data.result == 'success') {
            this.listLoading = false
            this.getList()
          }
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 重置
    goKeyReset() {
      this.KEYWORDS = ''
      this.CORP_NAME = ''
      this.getQuery()
    },
    getCorpList() {
      requestFN(
        '/corpinfo/listAll',
        {
        }
      ).then((data) => {
        if (data.result == 'success') {
          this.corpList = data.varList
        }
      }).catch((e) => {

      })
    },
    handleEdit(id) {
      requestFN(
        '/devicecorp/goEdit',
        {
          DEVICECORP_ID: id
        }
      ).then((data) => {
        if (data.result == 'success') {
          this.pd = Object.assign({}, data.pd)
        }
      }).catch((e) => {

      })
      this.dialogType = 'edit'
      this.dialogFormEdit = true
    },
    // 获取数据字典数据
    hasButton() {
      var keys = 'devicecorp:add,devicecorp:del,devicecorp:edit,fhSms,email,fromExcel,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys, tm: new Date().getTime()
        }
      ).then((data) => {
        if (data.result == 'success') {
          this.add = data.devicecorpfhadminadd	// 增
          this.del = data.devicecorpfhadmindel	// 删
          this.edit = data.devicecorpfhadminedit	// 改
          this.fromExcel = data.fromExcel	// 从excel导入权限
          this.toExcel = data.toExcel		// 导出到excel权限
        } else if (data.result == 'exception') {
          // showException('按钮权限', data.exception)// 显示异常
          console.info('按钮权限:', data.exception)
        }
      })
        .catch((e) => {
          this.listLoading = false
        })
    }
  }
}
</script>

// 禁用el-input  type= number 时。右侧出现的可以点击控制数字大小的上下箭头。为了只输入正整数服务
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
</style>
