<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetDataTransfer"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="cardNo" label="卡号" />
        <el-table-column prop="name" label="绑定人" />
        <el-table-column prop="stset" label="电量" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnEditDataInfo(row)"
            >
              修改
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            type="primary"
            @click="data.batchaddcardDialog.visible = true"
          >
            批量制卡
          </el-button>
          <el-button type="warning" @click="batchUnbinding">
            批量解绑
          </el-button>
          <el-button type="danger" @click="batchDelete"> 批量删除 </el-button>
        </template>
      </layout-table>
    </layout-card>

    <batchaddcard
      v-model:visible="data.batchaddcardDialog.visible"
      @get-data="fnGetDataTransfer"
    />
    <editcard
      v-if="data.editDialog.visible"
      v-model:visible="data.editDialog.visible"
      v-model:form="data.editDialog.form"
      @get-data="fnGetDataTransfer"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import batchaddcard from "./components/batchaddcard.vue";
import editcard from "./components/editcard.vue";
import {
  getDeployCardList,
  setRemoveDeployCardData,
  setUnbindingDeployCardData,
} from "@/request/positioning.js";
import { reactive } from "vue";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";

const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getDeployCardList);

const data = reactive({
  batchaddcardDialog: {
    visible: false,
  },
  editDialog: {
    visible: false,
    form: {}
  },
});
const fnGetDataTransfer = () => {
  fnGetData();
};
const fnEditDataInfo = (row) => {
  data.editDialog.visible = true;
  data.editDialog.form = row;

};
const batchUnbinding = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要解绑的项");
      return;
    }
    const DATA_IDS = selectionData.map((item) => item.cardNo).join(",");
    await ElMessageBox.confirm("确定要解绑吗？", { type: "warning" });
    await setUnbindingDeployCardData({ DATA_IDS });
    ElMessage.success("解绑成功");
    fnResetPagination();
  },
  { atBegin: true }
);
// 批量删除
const batchDelete = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    const DATA_IDS = selectionData.map((item) => item.cardNo).join(",");
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setRemoveDeployCardData({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnResetPaginationTransfer = () => {
  fnResetPagination();
};
</script>

<style scoped></style>
