<template>
  <div class="container">
    <TableSearch :query="query" :options="searchOpt" :search="handleSearch" />
    <div class="table-container">
      <TableCustom :columns="columns" :tableData="tableData" :total="paging.total" :refresh="getData" :currentPage="paging.page" :changePage="changePage">
        <template #toolbarBtn>
          <!-- <el-button type="primary" @click="handleAdd">新增</el-button> -->
          <el-button @click="handleEdit">手铐关联</el-button>
          <!-- <el-button>地图位置</el-button> -->
          <!-- <el-button>导出</el-button> -->
        </template>

        <template #state="{ rows }">
          {{ stateEnum[rows.state] }}
        </template>
        <template #deviceSwitch="{ rows }">
          {{ deviceSwitchEnum[rows.deviceSwitch] }}
        </template>
        <template #mode="{ rows }">
          {{ modeEnum[rows.mode] }}
        </template>
        <template #monitorMode="{ rows }">
          {{ monitorModeEnum[rows.monitorMode] }}
        </template>
        <template #battery="{ rows }"> {{ rows.battery }}% </template>

        <template #operator="{ rows }">
          <el-button link type="primary" size="small" @click="toPage('deviceInfo', rows)"> 详细信息 </el-button>
          <el-button link type="primary" size="small" @click="toPage('mapLocation', rows)"> 地图位置 </el-button>
          <el-button link type="primary" size="small" @click="toPage('setting', rows)"> 专项配置 </el-button>
          <el-button link type="primary" size="small" @click="handelRow(rows)"> 设备控制 </el-button>
          <el-button link type="primary" size="small" @click="handleEdit(rows)"> 修改 </el-button>
          <!-- <el-button link type="danger" size="small" @click="handelDel(rows)"> 删除 </el-button> -->
        </template>
      </TableCustom>
    </div>
    <el-dialog :title="isEdit ? '编辑' : '新增'" v-model="visible" width="700px" destroy-on-close :close-on-click-modal="false" @close="closeDialog">
      <TableEdit :form-data="rowData" :options="TableEditOptions" :edit="isEdit" :update="updateData" @close="closeDialog" />
    </el-dialog>

    <el-dialog title="设备控制" v-model="visible1" width="700px" destroy-on-close :close-on-click-modal="false" @close="closeDialog">
      <!-- <TableEdit :form-data="rowData" :options="controlOptions" :update="updateData" /> -->
      <DeviceControl v-model="controlForm" @change="handelControl" />
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="basetable">
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { deviceList, setMode, deviceGetLocation, deviceControl } from "@/api/index";
import { TDevice } from "@/api/index.d";
import TableCustom from "@/components/table-custom.vue";
import TableSearch from "@/components/table-search.vue";
import TableEdit from "@/components/table-edit.vue";
import DeviceControl from "./deviceControl.vue";
import { TableItem } from "@/types/table";
import { FormOption, FormOptionList } from "@/types/form-option";
import { useRouter } from "vue-router";

enum stateEnum {
  "离线" = 0,
  "在线",
  "充电中",
}
enum deviceSwitchEnum {
  "禁用" = 0,
  "启用",
}
enum modeEnum {
  "室内" = 0,
  "室外",
}
enum monitorModeEnum {
  "室内" = 0,
  "室外",
}

const router = useRouter();
// 新增/编辑弹窗相关
let TableEditOptions = ref<FormOption>({
  labelWidth: "100px",
  span: 24,
  list: [],
});
const addOp = [{ type: "input", label: "唯一编码", prop: "name", required: true }];
const editOp = [
  { type: "select", label: "选择手铐", prop: "name", required: true },
  { type: "input", label: "唯一编码", prop: "name1" },
  { type: "select", label: "辖区绑定", prop: "name2" },
  { type: "input", label: "紧急电话", prop: "name3" },
];

// 查询相关
const query = reactive({
  name: "",
});
const searchOpt = ref<FormOptionList[]>([
  { type: "input", label: "手铐SN：", prop: "name" },
  { type: "input", label: "警察账号：", prop: "name1" },
]);
const handleSearch = () => {
  changePage(1);
};

// 表格相关
let columns = ref([
  { type: "selection" },
  { type: "index", label: "序号", width: 55, align: "center" },
  { prop: "deviceId", label: "手铐IMEI" },
  { prop: "name", label: "绑定警察名称" },
  { prop: "username", label: "绑定警察账户" },
  { prop: "state", label: "设备状态" },
  { prop: "mode", label: "当前模式" },
  { prop: "deviceSwitch", label: "开关" },
  { prop: "monitorMode", label: "监测模式" },
  { prop: "battery", label: "电量" },
  { prop: "deviceVersion", label: "版本号" },
  { prop: "createTime", label: "创建时间" },
  { prop: "orgName", label: "关联辖区编号" },
  { prop: "operator", label: "操作", width: 400 },
]);
const paging = reactive({
  page: 1,
  size: 10,
  total: 0,
});
const controlForm = reactive({
  pattern: 1,
  location: 1,
});

const tableData = ref<TDevice.IListRes[]>([]);
const getData = async () => {
  const res = await deviceList(paging);
  tableData.value = res.records;
  paging.total = res.total;
};
getData();

const changePage = (val: number) => {
  paging.page = val;
  getData();
};

const visible1 = ref(false);
const visible = ref(false);
const isEdit = ref(false);
const rowData = ref<TDevice.IListRes>();

const handleAdd = () => {
  TableEditOptions.value.list = addOp;
  visible.value = true;
};

// 编辑
const handleEdit = (row?: TDevice.IListRes) => {
  rowData.value = { ...row };
  TableEditOptions.value.list = editOp;
  isEdit.value = true;
  visible.value = true;
};

const handelRow = (row?: TDevice.IListRes) => {
  rowData.value = { ...row };
  visible1.value = true;
};

const handelControl = (type: number) => {
  switch (type) {
    case 4:
      deviceGetLocation({
        deviceId: rowData.value.deviceId,
      }).then(() => {
        ElMessage.success("操作成功");
      });
      break;
    default:
      let cmdEnum = {
        1: "poweroff",
        2: "restart",
        3: "factory",
      };
      deviceControl({
        deviceId: rowData.value.deviceId,
        cmd: cmdEnum[type],
      }).then(() => {
        ElMessage.success("操作成功");
      });
      break;
  }

  visible1.value = false;
};
// 删除
const handelDel = (row: TableItem) => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
  })
    .then(async () => {
      // ElMessage.success("删除成功");
    })
    .catch(() => {});
};

const updateData = () => {
  closeDialog();
  getData();
};

const closeDialog = () => {
  visible.value = false;
  isEdit.value = false;
};

// 删除相关
const handleDelete = (row: TableItem) => {
  ElMessage.success("删除成功");
};
//
const toPage = (path: string, row: TDevice.IListRes) => {
  let p;
  if (path == "deviceInfo") {
    p = { ...row };
  } else if (path == "mapLocation") {
    p = { id: row.id };
  } else if (path == "setting") {
    p = { id: row.id };
  }
  router.push({
    path,
    query: p,
  });
};
</script>

<style scoped>
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
