<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-row>
        <el-form-item label="RFID编码" prop="rfidCode">
          <el-input
            v-model="queryParams.rfidCode"
            placeholder="请输入RFID编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="RFID内容" prop="rfid">
          <el-input
            v-model="queryParams.rfid"
            placeholder="请输入锁柜名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="RFID类型" prop="rfidType">
          <el-select
            v-model="queryParams.rfidType"
            placeholder="请选择RFID类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.rfid_token_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="dict in dict.type.rfid_token_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 20px">
          <el-button
            v-no-more-click
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索
          </el-button>
          <el-button
            v-no-more-click
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
          >重置
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-no-more-click
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:hw:work:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          v-no-more-click
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:hw:work:batchremove']"
        >批量删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="LockCabinetList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        label="RIFID编码"
        width="150px"
        align="center"
        prop="rfidCode"
      />
      <el-table-column label="RFID内容" align="center" prop="rfid"/>
      <el-table-column label="RFID类型" align="center" prop="rfidType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rfid_token_type" :value="scope.row.rfidType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="硬件ID" align="center" prop="hardwareId">-->

<!--      </el-table-column>-->

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            style="pointer-events: none;"
            v-if="scope.row.status!==null"
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="grey"
          ></el-switch>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"></el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:hw:work:edit']"
          >编辑
          </el-button>
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:hw:work:remove']"
          >删除
          </el-button>
          <!--          <el-button v-no-more-click size="mini" type="text">预览</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 添加或修改设备维修单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="15">
            <el-form-item label="RFID编码" prop="rfidCode">
              <el-input
                v-model="form.rfidCode"
                placeholder="请输入RFID编码"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="80">
              <el-switch
                v-model="autoGenFlag"
                active-color="#13ce66"
                active-text="自动生成"
                @change="handleAutoGenChange(autoGenFlag)"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="RFID内容" prop="rfid" style="width: 380px">
          <el-input
            v-model="form.rfid"
            placeholder="请输入RFID内容"
          />
        </el-form-item>


        <el-form-item label="RFID类型" prop="rfidType" style="width: 380px">
          <el-select
            v-model="form.rfidType"
            placeholder="请选择RFID类型"
            clearable
            style="width: 100%"
            @change="handleSelectHardware"
          >
            <el-option
              v-for="dict in dict.type.rfid_token_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="硬件ID" prop="hardwareId" style="width: 380px">-->
<!--          <el-select-->
<!--            v-model="form.hardwareId"-->
<!--            placeholder="请选择硬件ID"-->
<!--            clearable-->
<!--            style="width: 100%"-->
<!--            @change="handleSelectHardware"-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="dict in this.hardwareList"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.rfid_token_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" prop="remark" style="width: 380px">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm"
        >确 定
        </el-button
        >
        <el-button v-no-more-click @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {listHardware} from "@/api/mes/hw/hardwareinfo";
import {genCode} from "@/api/system/autocode/rule";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Template from "@/views/print/printtemplate/list.vue";
import {
  deleteIsRfidTokenByRfidIds,
  getIsRfidTokenPage,
  insertIsRfidToken,
  selectIsRfidTokenById,
  updateIsRfidToken
} from "@/api/mes/rfid_token";

export default {
  name: "lock",
  components: {Template, Treeselect},
  dicts: ["card_type", 'rfid_token_status', 'rfid_token_type'],
  data() {
    return {
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位数据
      marsOptions: [],
      //锁柜列表
      LockCabinetList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      createTime: "",
      queryParams: {
        current: 1,
        size: 10,
        rfid: null,
        rfidType: null,
        rfidCode: null,
      },
      hardwareList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rfidCode: [
          {required: true, message: "RFID编码不能为空", trigger: "blur"},
        ],
        rfid: [
          {required: true, message: "RFID内容不能为空", trigger: "blur"},
        ],
        rfidType:[
          {required: true, message: "RFID类型不能为空", trigger: "blur"},
        ],
       /* hardwareId:[
          {required: true, message: "硬件ID不能为空", trigger: "blur"},
        ]*/
      },
      // 新增状态
      EditId: 0, //修改判断
    };
  },
  created() {
    this.getList();
    this.getOtherList()
  },
  methods: {
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("RFID_CODE").then((response) => {
          this.form.rfidCode = response;
          //  console.log(response, this.form.cardCode, "cardCode");
        });
      } else {
        this.form.rfidCode = null;
      }
    },

    /** 查询设备维修单列表 */
    getList() {
      this.loading = true;
      // 工卡数据
      getIsRfidTokenPage(this.queryParams).then((response) => {
        this.LockCabinetList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getOtherList() {
      const data = {
        current: 1,
        size: -1
      }
      // 硬件Id
      listHardware(data).then(res => {
        console.log(res,'硬件');
        this.hardwareList = res.data.records.map((item) => {
          return {
            value: item.id,
            label: item.hardwareName,
          }
        })
      })
    },

    handleSelectHardware(value) {
      this.form.hardwareId = value
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rfid: null,
        rfidCode: null,
        rfidType: null,
        status: null,
        hardwareId: null,
      };

      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.cardCode = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.rfidId);
      this.codes = selection.map((item) => item.rfidCode);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.EditId = null;
      this.title = "添加RFID信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.EditId = row.rfidId || this.ids;
      selectIsRfidTokenById(this.EditId).then((response) => {
        console.log(response, "response");
        this.form = response.data;

        this.open = true;
        this.title = "编辑RFID信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.EditId != null) {
            updateIsRfidToken(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertIsRfidToken(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.EditId = null;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.rfidId || this.ids;
      const repairCodes = row.rfidCode || this.codes;
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function () {
          return deleteIsRfidTokenByRfidIds(repairIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
  },
};
</script>
<style scoped lang="scss">
.imgstatus {
  position: relative;
  top: 1px;
  left: 0px;
}
//图片放大
.img-box {
  width: 50px;
  height: 50px;
  position: relative;

  #eyeicon {
    display: none;
  }
}

.img-box:hover {
  background: #000;

  .images {
    opacity: 0.6;
  }

  #eyeicon {
    display: block;
    position: absolute;
    top: 40%;
    left: 30%;
    z-index: 100;
    color: white;
    pointer-events: none;
  }
}

</style>
