<template>
  <div class="body">
    <!-- 头部区域 -->

    <div class="head">
      <div class="head-img">
        <img src="../../../public/images/img-1.png" />
      </div>
      <div class="head-info">设备管理</div>
    </div>

    <!-- 添加按钮区域 -->

    <div class="switch">
      <div class="switch-button">
        <el-row>
          <el-button type="primary" size="small" @click="handleCreate"
            ><img src="../../../public/images/img-3.png" />添加
          </el-button>
        </el-row>
      </div>
    </div>

    <!-- 表格区域 -->
    <div class="form">
      <el-table :data="list">
        <el-table-column label="序列号" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.serialNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column label="名称" width="300px" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="厂家" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.manufacturer }}</span>
          </template>
        </el-table-column>

        <el-table-column label="型号" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.model }}</span>
          </template>
        </el-table-column>

        <el-table-column label="IP地址" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.ipAddress }}</span>
          </template>
        </el-table-column>

        <el-table-column label="开关" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#3BAAFD"
              inactive-color="#CCCCCC"
              :active-value="1"
              :inactive-value="0"
              disabled
              @click.native="changeSwitch(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180px" align="center">
          <template slot-scope="{ row, $index }">
            <el-button type="text" size="small" @click="handleUpdate(row)"
              >编辑</el-button
            >

            <el-button
              @click="handleDelete(row, $index)"
              type="text"
              size="small"
              class="button"
              ><img src="../../../public/images/img-2.png" />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部分页区 -->

      <div class="block">
        <template>
          <pagination
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            @pagination="getList"
          />
        </template>
      </div>
    </div>

    <!-- 添加编辑按钮弹窗区域 -->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogVisible"
      width="30%"
      center
    >
      <el-form ref="dataForm" :model="temp" :rules="rules">
        <el-form-item
          style="white-space: pre"
          label="序列号："
          class="switch-button-item"
          prop="serialNumber"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.serialNumber"
              autocomplete="off"
              placeholder="请输入序列号"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="名    称："
          class="switch-button-item"
          prop="name"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.name"
              autocomplete="off"
              placeholder="请输入名称"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="厂      家："
          class="switch-button-item"
          prop="manufacturer"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.manufacturer"
              autocomplete="off"
              placeholder="请输入厂家"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="型      号："
          class="switch-button-item"
          prop="model"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.model"
              autocomplete="off"
              placeholder="请输入型号"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="IP  地 址："
          class="switch-button-item"
          prop="ipAddress"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.ipAddress"
              autocomplete="off"
              placeholder="请输入IP地址"
            ></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? createData() : updateData()"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  fetchList,
  createDevice,
  updateDevice,
  deletDevice,
} from "@/api/devicemanagement";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      list: [],
      total: 0, //数据的总数//
      listQuery: {
        page: 1, //当前在第几页//
        limit: 8, //一页几条//
      },
      dialogVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑门禁设备",
        create: "添加门禁设备",
      },
      temp: {
        id: "",
        serialNumber: "",
        name: "",
        manufacturer: "",
        model: "",
        ipAddress: "",
        status: "1",
      },
      rules: {
        serialNumber: [
          {
            required: true,
            message: "请输入序列号",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      fetchList(this.listQuery).then((response) => {
        this.list = response.data;
        this.total = response.total;
      });
    },
    handleDelete(row, index) {
      if (confirm("是否删除？")) {
        deletDevice({ id: row.id }).then(() => {
          this.$notify({
            title: "删除成功",
            type: "success",
            duration: 2000,
          });
          this.list.splice(index, 1);
        });
      }
      this.getList();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        serialNumber: "",
        name: "",
        manufacturer: "",
        model: "",
        ipAddress: "",
        status: "1",
      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "update";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createDevice(this.temp).then(() => {
            this.list.push({
              serialNumber: this.temp.serialNumber,
              name: this.temp.name,
              manufacturer: this.temp.manufacturer,
              model: this.temp.model,
              ipAddress: this.temp.ipAddress,
              status: this.temp.status,
            });
            this.dialogVisible = false;
            this.$notify({
              title: "添加成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
      this.getList();
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp);
          updateDevice(tempData).then(() => {
            const index = this.list.findIndex((v) => v.id === this.temp.id);
            this.list.splice(index, 1, this.temp);
            this.dialogVisible = false;
            this.$notify({
              title: "编辑成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
    },
    changeSwitch(row) {
      this.temp = Object.assign({}, row);
      if (this.temp.status == 0) {
        if (confirm("确定开启设备吗？")) {
          updateDevice({ id: this.temp.id, status: this.temp.status }).then(
            () => {
              const index = this.list.findIndex((v) => v.id === this.temp.id);
              this.temp.status = 1;
              this.list.splice(index, 1, this.temp);
            }
          );
          this.$notify({
            title: "开启成功",
            type: "success",
            duration: 1500,
          });
        }
      } else {
        if (confirm("确定关闭设备吗？")) {
          updateDevice({ id: this.temp.id, status: this.temp.status }).then(
            () => {
              const index = this.list.findIndex((v) => v.id === this.temp.id);
              this.temp.status = 0;
              this.list.splice(index, 1, this.temp);
            }
          );
          this.$notify({
            title: "关闭成功",
            type: "success",
            duration: 1500,
          });
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.body {
  width: 1551px;
  margin: 29px 29px 0 31px;
  .head {
    display: flex;
    width: 1380px;
    height: 55px;
    margin-left: 40px;
    border-bottom: 1px solid #cccccc;

    .head-img {
      width: 30px;
      height: 30px;

      img {
        width: 30px;
      }
    }

    .head-info {
      width: 95px;
      height: 22px;
      margin-top: 5px;
      margin-left: 16px;
      font-weight: bold;
    }
  }

  .switch {
    display: flex;
    width: 1380px;
    line-height: 58px;
    margin-left: 40px;
    font-size: 9px;

    .switch-button {
      width: 50px;

      img {
        width: 10.5px;
        margin-right: 8px;
      }
    }
  }

  .form {
    margin-left: 40px;
    width: 1380px;

    .el-switch.is-disabled {
      opacity: 1;
      ::v-deep .el-switch__core {
        cursor: pointer;
      }
    }

    .button {
      color: red;
    }

    img {
      width: 8.5px;
      margin-right: 8px;
    }

    .block {
      float: right;
    }
  }

  .switch-button-item {
    margin-left: 120px;
    margin-top: 32px;
  }
}
</style>
