<template>
  <div class="device">
    <el-card shadow="never">
      <div slot="header">
        <span>样表设备</span>
      </div>
      <TableHeader>
        <div slot="left">
          <!-- <el-button size="small" type="primary" @click="handleAdd"
            >新 增</el-button
          > 
          <el-dropdown class="ml15" @command="handleMore">
            <el-button type="primary" size="small">
              更多操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="1">模板下载</el-dropdown-item>
              <el-dropdown-item command="2">导入</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          -->
        </div>
        <div slot="right" class="flex">
          <el-input
            size="small"
            v-model="searchVal"
            class="ml10 mr10"
            placeholder="请输入设备名称搜索"
            @keyup.native.enter="handleSearch"
          ></el-input>
          <el-button size="small" type="primary" @click="handleSearch"
            >搜 索</el-button
          >
          <el-button size="small" @click="searchReset">重 置</el-button>
        </div>
      </TableHeader>
      <!-- 表格 -->
      <div class="table-content">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          border
          :data="tableData"
          size="small"
          tooltip-effect="dark"
          style="width: 100%"
          max-height="676px"
          :row-key="(row) => row.id"
        >
          <el-table-column
            type="index"
            label="序号"
            width="55"
            align="center"
            :index="(index) => (current - 1) * size + index + 1"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="设备名称"
            align="center"
            prop="deviceName"
          >
          </el-table-column>
          <el-table-column align="center" prop="deviceMac" label="mac地址">
          </el-table-column>
          <el-table-column align="center" label="设备位置">
            <template slot-scope="scope">
              <span
                v-if="
                  scope.row.deviceInBuilding ||
                  scope.row.deviceInFloor ||
                  scope.row.lon ||
                  scope.row.lati
                "
                >{{
                  `${
                    scope.row.deviceInBuilding
                      ? scope.row.deviceInBuilding
                      : "--"
                  }栋/${
                    scope.row.deviceInFloor ? scope.row.deviceInFloor : "--"
                  }楼/${scope.row.lon ? scope.row.lon : "--"},${
                    scope.row.lati ? scope.row.lati : "--"
                  }`
                }}</span
              >
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="resolution" label="分辨率">
          </el-table-column>
          <el-table-column align="center" prop="leadingOfficial" label="负责人">
          </el-table-column>
          <el-table-column
            align="center"
            prop="leadingOfficialTelephone"
            label="联系电话"
          >
          </el-table-column>
          <el-table-column align="center" label="状态">
            <template slot-scope="scope">
              <el-tag
                size="small"
                v-if="scope.row.deviceStatus === 2"
                type="success"
                >在线</el-tag
              >
              <el-tag size="small" v-else type="danger">离线</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="enabled" label="启用/停用">
            <template slot-scope="scope">
              <el-switch
                disabled
                class="tableScopeSwitch"
                :active-value="1"
                :inactive-value="0"
                inactive-text="停用"
                active-text="启用"
                v-model="scope.row.enabled"
                @change="handleChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100">
            <template slot-scope="scope">
              <div class="flex jca">
                <!-- <span
                  v-if="scope.row.active !== 1"
                  class="primary pointer"
                  @click="handleActive(scope.row)"
                  >激活</span
                >
                <span
                  v-if="scope.row.active == 1"
                  style="opacity: 0"
                  class="primary"
                  >激活</span
                > -->
                <span class="primary pointer" @click="handleBound(scope.row)"
                  >绑定表单</span
                >
                <!-- <span class="primary pointer" @click="handleEdit(scope.row)"
                  >编辑</span
                >
                <span class="delete pointer" @click="handleDel(scope.row.id)"
                  >删除</span
                > -->
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination" v-if="total">
        <el-pagination
          background
          layout="total,sizes,prev,pager,next,jumper"
          :pager-count="5"
          :total="total"
          :current-page="current"
          :page-size="size"
          :page-sizes="pageSizes"
          @current-change="changePagination"
          @size-change="changeSize"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增设备 -->
    <AddDevice
      :dialogVisible.sync="dialogVisible"
      :title="title"
      ref="AddDevice"
    ></AddDevice>
    <!-- 关联事项 -->
    <AddMatter ref="AddMatter" :matterDrawer.sync="matterDrawer"></AddMatter>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import AddMatter from "./modal/AddMatter.vue";
import AddDevice from "./modal/AddDevice.vue";
import { getDeviceList } from "@/api/device";
import local from "@/utils/local";
export default {
  components: {
    TableHeader,
    AddDevice,
    AddMatter,
  },
  data() {
    return {
      searchVal: "",
      siteId: local.getLocal("sampleSiteId")
        ? local.getLocal("sampleSiteId")
        : "",
      tableData: [],
      pageSizes: [10, 20, 30, 40, 50, 100, 200],
      total: 10,
      current: 1,
      size: 10,
      dialogVisible: false,
      title: "新增数字样表设备",
      loading: false,
      matterDrawer: false,
    };
  },
  created() {
    this.getDeviceList();
  },
  methods: {
    // 获取设备列表
    async getDeviceList() {
      this.loading = true;
      let res = await getDeviceList({
        siteId: this.siteId,
        page: this.current,
        size: this.size,
        deviceName: `%${this.searchVal}%`,
        productName: "样表机",
      });
      this.loading = false;
      let { data } = res.data.data;
      this.tableData = data;
      this.$refs.multipleTable.bodyWrapper.scrollTop = 0;
    },
    // 新增
    handleAdd() {
      this.title = "新增数字样表设备";
      this.$refs.AddDevice.onAdd();
      this.dialogVisible = true;
    },
    // 搜索
    handleSearch() {
      this.current = 1;
      this.getDeviceList();
    },

    // 重置
    searchReset() {
      this.searchVal = "";
      this.current = 1;
      this.getDeviceList();
    },
    // 翻页
    changePagination(cur) {
      this.current = cur;
      this.getDeviceList();
    },
    // 改变每页显示数量
    changeSize(size) {
      this.size = size;
      this.getDeviceList();
    },
    // 更多操作
    handleMore(item) {
      console.log(item);
    },
    // 激活
    handleActive(row) {
      this.$confirm("此操作将激活这台设备，是否继续?", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          console.log(row);
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑数字样表设备";
      this.$refs.AddDevice.onEdit(row);
      this.dialogVisible = true;
    },
    // 删除
    handleDel(id) {
      this.$confirm("此操作将删除该数字样表设备，是否继续?", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
    // 启停用
    handleChange(row) {
      console.log(row);
    },
    // 绑定表单
    handleBound(row) {
      this.matterDrawer = true;
      this.$nextTick(() => {
        this.$refs.AddMatter.getDevInfo(row);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.device {
  width: 100%;
  min-height: 100%;
  display: flex;
  :deep(.el-card) {
    height: auto;
  }
}
// .table-content {
//   height: 550px;
// }
/deep/.tableScopeSwitch .el-switch__label {
  position: absolute;
  display: none;
  color: #fff;
}
/*打开时文字位置设置*/
/deep/.tableScopeSwitch .el-switch__label--right {
  z-index: 1;
  left: 0px; /*不同场景下可能不同，自行调整*/
}
/*关闭时文字位置设置*/
/deep/.tableScopeSwitch .el-switch__label--left {
  z-index: 1;
  right: 0px; /*不同场景下可能不同，自行调整*/
}
/*显示文字*/
/deep/.tableScopeSwitch .el-switch__label.is-active {
  display: block;
}
/deep/.tableScopeSwitch.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 60px !important; /*开关按钮的宽度大小*/
}
</style>
