a
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="打印机编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入打印机编号"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="打印机名称" prop="name" label-width="140px">
        <el-input
          v-model="queryParams.name"
          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 size="mini" @click="printOrder"
          >新增设备</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="打印机名称" align="center" prop="name" />
      <el-table-column label="打印机编码" align="center" prop="sn" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:order:detail']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            style="color: red"
            icon="el-icon-remove"
            @click="handleremove(scope.row)"
            v-hasPermi="['manage:order:detail']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="打印机名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入打印机名称" />
        </el-form-item>
        <el-form-item label="打印机sn" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入打印机sn" />
        </el-form-item>
        <el-form-item label="操作类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio :label="0" :value="0">新增</el-radio>
            <el-radio :label="1" :value="1">绑定</el-radio>
          </el-radio-group>
        </el-form-item>
      </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 { equipmentList, addPrinter, removePrinter } from "@/api/manage/equipment";

export default {
  name: "Config",
  //   dicts: ["sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        type: 0,
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "打印机名称不能为空", trigger: "blur" }],
        sn: [{ required: true, message: "打印机sn不能为空", trigger: "blur" }],
        type: [{ required: true, message: "请选择操作类型", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      equipmentList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.configList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    printOrder() {
      this.title = "新增设备";
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleremove(row) {
      this.$confirm("是否确认删除该设备?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          removePrinter(row.id).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.getList();
            } else {
              this.$modal.msgError(response.msg);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      this.title = "编辑设备";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.configId != undefined) {
            editPrinter(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgError(response.msg);
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPrinter(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgError(response.msg);
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal
        .confirm('是否确认删除参数编号为"' + configIds + '"的数据项？')
        .then(function () {
          return delConfig(configIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/config/export",
        {
          ...this.queryParams,
        },
        `config_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    },
  },
};
</script>
<style scoped lang="scss"></style>
