<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="考勤机" prop="machineName">
          <el-input
            v-model="queryParams.machineName"
            placeholder="请输入考勤机名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所在位置" prop="attAddress">
          <el-input
            v-model="queryParams.attAddress"
            placeholder="请输入所在位置"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
           v-hasPermi="['att:machine:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
           
          >导出</el-button>
        </el-col>
 
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="devList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="序号" align="center" width="80" type="index">
          <template slot-scope="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="考勤机名称" align="center" prop="machineName" />
        <el-table-column label="型号" align="center" prop="model" :show-overflow-tooltip="true" /> 
        <el-table-column label="设备编码" align="center" prop="machineCode" :show-overflow-tooltip="true" />
        <el-table-column label="IP地址" align="center" prop="ip" :show-overflow-tooltip="true" />
        <el-table-column label="端口号" align="center" prop="port" :show-overflow-tooltip="true" />
        <el-table-column label="考勤地址" align="center" prop="attAddress" :show-overflow-tooltip="true" />
        <el-table-column label="设备状态" align="center" prop="status" :show-overflow-tooltip="true">
          <template slot-scope="scope">
          <dict-tag :options="statusList" :value="scope.row.status"/>
        </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
               v-hasPermi="['att:machine:edit']"
              @click="handleUpdate(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              v-hasPermi="['att:machine:remove']"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="考勤机名称" prop="machineName">
                        <el-input v-model="form.machineName" placeholder="请输入考勤机名称" maxlength="20"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="型号" prop="model">
                        <el-input v-model="form.model" placeholder="请输入型号" maxlength="20"/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="设备编码" prop="machineCode">
                        <el-input v-model="form.machineCode" placeholder="请输入设备编码" maxlength="20"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="IP地址" prop="ip">
                        <el-input v-model="form.ip" placeholder="请输入IP地址" maxlength="20"/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="端口号" prop="port">
                        <el-input v-model="form.port" placeholder="请输入端口号" maxlength="20"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="考勤地址" prop="attAddress">
                        <el-input v-model="form.attAddress" placeholder="请输入考勤地址" maxlength="20"/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="设备状态" prop="status">
                        <el-select v-model="form.status" placeholder="设备状态" clearable>
                            <el-option
                            v-for="item in statusList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                            />
                        </el-select>
                        <!-- <el-input v-model="form.dictName" placeholder="请选择设备状态" maxlength="20"/> -->
                    </el-form-item>
                </el-col>
                
            </el-row>

            
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listMachine,getMachine,delMachine,addMachine,updateMachine } from "@/api/setting/devices";

  export default {
    name: "Dict",
    dicts: ['sys_normal_disable'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        devList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        statusList:[{id:'0',name:'关机'},{id:'1',name:'开机'}],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          machineName: undefined,
          attAddress: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        //   dictName: [
        //     { required: true, message: "名称不能为空", trigger: "blur" }
        //   ],
        //   dictType: [
        //     { required: true, message: "类型不能为空", trigger: "blur" }
        //   ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        listMachine(this.queryParams).then(response => {
            this.devList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          attAddress: undefined,
          ip: undefined,
          machineCode: undefined,
          machineName: undefined,
          model: undefined,
          port: undefined, 
          status: "0",
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加考勤机";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const Id = row.id;
        getMachine(Id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改考勤机";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateMachine(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMachine(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const Ids = row.id;
        this.$modal.confirm('是否确认删除编号为"' + Ids + '"的数据项？').then(function() {
          return delMachine(Ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/dict/type/export', {
          ...this.queryParams
        }, `type_${new Date().getTime()}.xlsx`)
      },
 
    }
  };
  </script>