<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="锁柜名称" prop="cabinetName">
          <el-input
            v-model="queryParams.cabinetName"
            placeholder="请输入锁柜名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="是否在线" prop="isOnline">
          <el-select
            v-model="queryParams.isOnline"
            placeholder="请选择是否在线"
            clearable
          >
            <el-option
              v-for="dict in dict.type.isOnline_status"
              :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.canbinet_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="锁柜编码"
        width="150px"
        align="center"
        prop="cabinetCode"
      />
      <el-table-column label="锁柜名称" align="center" prop="cabinetName"/>
      <el-table-column label="硬件ID" align="center" prop="hardwareId"></el-table-column>
      <el-table-column label="硬件序列号" align="center" prop="serialNumber"></el-table-column>
      <el-table-column label="岗位" align="center" prop="workstationName">
      </el-table-column>
      <el-table-column label="图片" align="left" prop="cabinetPicture">
        <template slot-scope="scope">
          <div class="img-box" v-if="scope.row.cabinetPicture">
            <el-image
              style="width: 50px; height: 50px"
              :preview-teleported="true"
              class="images"
              :hide-on-click-modal="true"
              :src="scope.row.cabinetPicture"
              :zoom-rate="1.2"
              :preview-src-list="[scope.row.cabinetPicture]"
              :initial-index="1"

            >
            </el-image>
            <i class="el-icon-zoom-in" id="eyeicon"></i>
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="left" prop="cabinetIcon">
        <template slot-scope="scope">
          <div class="img-box" v-if="scope.row.cabinetIcon">
            <el-image
              style="width: 50px; height: 50px"
              :preview-teleported="true"
              class="images"
              :hide-on-click-modal="true"
              :src="scope.row.cabinetIcon"
              :zoom-rate="1.2"
              :preview-src-list="[scope.row.cabinetIcon]"
              :initial-index="1"

            >
            </el-image>
            <i class="el-icon-zoom-in" id="eyeicon"></i>
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="是否在线" align="center" prop="isOnline">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.isOnline_status" :value="scope.row.isOnline"/>

        </template>
      </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" prop="detail">
        <template slot-scope="scope">
          <el-button type="text" @click="lookDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="测试" align="center" prop="detail" v-hasPermi="['iscs:cabinet:test']">
        <template slot-scope="scope">
          <el-button type="text" @click="openDialogFunction(scope.row)">查看</el-button>
        </template>
      </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="锁柜编号" prop="cabinetCode">
              <el-input
                v-model="form.cabinetCode"
                placeholder="请输入锁柜编号"
                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="锁柜名称" prop="cabinetName" style="width: 380px">
          <el-input
            v-model="form.cabinetName"
            placeholder="请输入锁柜名称"
          />
        </el-form-item>
        <el-form-item label="岗位序号" prop="workstationId" style="width: 380px">
          <treeselect v-model="form.workstationId" :options="marsOptions"  :normalizer="Marsnormalizer" placeholder="选择岗位" />
        </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="isOnline">
          <el-radio-group v-model="form.isOnline">
            <el-radio
              v-for="dict in dict.type.isOnline_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.canbinet_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row>
          <el-col :span="10">
            <el-form-item label="图标" prop="cabinetIcon">
              <ImageUploadSingle
                :limit="1"
                :value="form.cabinetIcon"
                :fileSize="5"
                @onUploaded="handleImgUplaoded"
                @onRemoved="handleImgRemoved"
              ></ImageUploadSingle>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="图片" prop="cabinetPicture">
              <ImageUploadSingle
                :limit="1"
                :value="form.cabinetPicture"
                :fileSize="5"
                @onUploaded="handleImgUplaoded1"
                @onRemoved="handleImgRemoved1"
              ></ImageUploadSingle>
            </el-form-item>
          </el-col>
        </el-row>
        <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>
<!--    测试弹框-->
    <el-dialog title="测试" :visible.sync="openDialog" width="650px" append-to-body>
      <el-form ref="form" :model="DialogData"  label-width="100px">
        <el-form-item label="类型">
          <el-input v-model="DialogData.type" placeholder="请输入type"></el-input>
        </el-form-item>
        <el-form-item label="序列号">
          <el-input v-model="DialogData.lotoSerialNumber"  placeholder="请输入lotoSerialNumber"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="DialogData.content"  placeholder="请输入content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitTestForm"
        >确 定
        </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 {listMarsDept} from "@/api/system/marsdept";
import {
  deleteIsLockCabinetByCabinetIds,
  getIsLockCabinetPage,
  insertIsLockCabinet,
  selectIsLockCabinetById, sendDataToLoto,
  updateIsLockCabinet
} from '@/api/mes/lockCabinet'

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Template from "@/views/print/printtemplate/list.vue";

export default {
  name: "lock",
  components: {Template, Treeselect},
  dicts: ["card_type", 'isOnline_status', 'canbinet_status'],
  data() {
    return {
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位数据
      marsOptions: [],
      //锁柜列表
      LockCabinetList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDialog: false,
      DialogData:[],
      TestRow:null,
      // 查询参数
      createTime: "",
      queryParams: {
        current: 1,
        size: 10,
        cardCode: null,
        cardType: null,
        userName: null,
      },
      hardwareList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cabinetCode: [
          {required: true, message: "锁柜编码不能为空", trigger: "blur"},
        ],
        cabinetName: [
          {required: true, message: "锁柜名称不能为空", trigger: "blur"},
        ],
        hardwareId:[
          {required: true, message: "硬件ID不能为空", trigger: "blur"},
        ],
        workstationId:[
          {required: true, message: "岗位序号不能为空", trigger: "blur"},
        ]
      },
      // 新增状态
      EditId: 0, //修改判断
    };
  },
  created() {
    this.getList();
    this.getOtherList()
  },
  methods: {
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("CABINET_CODE").then((response) => {
          this.form.cabinetCode = response;
          //  console.log(response, this.form.cardCode, "cardCode");
        });
      } else {
        this.form.cabinetCode = null;
      }
    },
    lookDetail(row) {
      this.$router.push({
        path:'/mes/hw/lockCabinet/LookDetail',
        query:{
          cabinetId:row.cabinetId,
        }
      })
    },
    /** 查询设备维修单列表 */

    getList() {
      this.loading = true;

      // 工卡数据
      getIsLockCabinetPage(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,
          }
        })
      })
      // 岗位Id
      listMarsDept(data).then(res => {
        console.log(res);
        this.marsOptions = this.handleTree(
          res.data.records,
          "workstationId",
          "parentId"
        );
      })

    },
    /** 转换mars岗位数据结构 */
    Marsnormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.workstationId,
        label: node.workstationName,
        children: node.children,
      };
    },
    handleSelectHardware(value) {
      this.form.hardwareId = value
    },
    handleSelectWorkstation(value) {
      this.form.workstationId = value
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDialog = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cabinetCode: null,
        cabinetIcon: null,
        cabinetName: null,
        cabinetPicture: null,
        hardwareId: null,
        isOnline: null,
        status: null,
        remark: null,
        workstationId: 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.cabinetId);
      this.codes = selection.map((item) => item.cabinetCode);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.EditId = null;
      this.title = "添加锁柜信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.EditId = row.cabinetId || this.ids;
      selectIsLockCabinetById(this.EditId).then((response) => {
        console.log(response, "response");
        this.form = response.data;

        this.open = true;
        this.title = "编辑锁柜信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.EditId != null) {
            updateIsLockCabinet(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertIsLockCabinet(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.EditId = null;
              this.getList();
            });
          }
        }
      });
    },
    // 测试
    openDialogFunction(row) {
      this.openDialog = true;
      this.TestRow=row
    },
    // 测试提交数据
    submitTestForm(){
      const params={
        type:this.DialogData.type,
        lotoSerialNumber:this.DialogData.lotoSerialNumber,
        content:this.DialogData.content,
      }
      sendDataToLoto(params).then(response => {
        this.$modal.msgSuccess('测试成功');
        this.openDialog=false;
        this.DialogData.type=''
        this.DialogData.lotoSerialNumber=''
        this.DialogData.content=''
      })
    },
    //图片上传成功
    handleImgUplaoded(imgUrl) {
      console.log(imgUrl);
      this.form.cabinetIcon = imgUrl[0].url;
    },
    //图片移除
    handleImgRemoved(imgUrl) {
      console.log(imgUrl);
      this.form.cabinetIcon = null;
    },
    handleImgUplaoded1(imgUrl) {
      console.log(imgUrl);
      this.form.cabinetPicture = imgUrl[0].url;
    },
    //图片移除
    handleImgRemoved1(imgUrl) {
      console.log(imgUrl);
      this.form.cabinetPicture = null;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.cabinetId || this.ids;
      const repairCodes = row.cabinetCode || this.codes;
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function () {
          return deleteIsLockCabinetByCabinetIds(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>
