<template>
  <BasicModal
    v-bind="$attrs"
    :title="title"
    @register="registerInnerModal"
    @ok="handleSubmit"
    @cancel="resetForm"
    :width="800"
  >
    <template #footer>
      <div v-if="isUpdate !== 3">
        <a-button type="default" @click="handleCancle">取消</a-button>
        <a-button type="primary" @click="handleSubmit">{{
          activeKey === 1 ? "取消关注" : "关注"
        }}</a-button>
      </div>
    </template>

    <Tabs v-model:activeKey="activeKey" centered @change="handleChange">
      <TabPane :key="1" tab="已关注"></TabPane>
      <TabPane :key="0" tab="未关注" force-render></TabPane>
    </Tabs>
    <BasicTable @register="registerTable"> </BasicTable>
  </BasicModal>
</template>

<script setup lang="ts">
import { BasicModal, useModalInner } from "@/components/Modal";
import { BasicForm, useForm } from "@/components/Form";
import { computed, ref, unref } from "vue";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  workerInfoInfo,
  workerInfoAdd,
  workerInfoUpdate,
  elevatorUserList,
  batchConcernPeo,
} from "@/api/workPlace/workerInfoManage";
import { modalSchemas } from "./user.data";
import { Tabs, TabPane, message } from "ant-design-vue";
import { infoList } from "@/api/basicInfo/elevator";
import { formEvaSchemas, evaColumns } from "./user.data";
defineOptions({ name: "WorkerInfoModal" });
const activeKey = ref(1);
const emit = defineEmits(["register", "reload"]);
const info = ref<any>({});
const isUpdate = ref<number>(1);
const title = computed<string>(() => {
  return "批量关注电梯";
});
const [registerTable, { reload, getSelectRows }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "",
  api: elevatorUserList,
  showIndexColumn: false,
  rowKey: "id",
  useSearchForm: true,
  showTableSetting: false,
  formConfig: {
    schemas: formEvaSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 8,
    },
  },
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    params.userId = info.value.userId;
    params.isConcern = activeKey.value;
    return params;
  },
  columns: evaColumns,
  // 需要添加上部门参数
});
const [registerInnerModal, { modalLoading, closeModal }] = useModalInner(
  async (data: { record?: Recordable; update: number }) => {
    modalLoading(true);
    const { record, update } = data;
    info.value = record;
    await reload();
    modalLoading(false);
  }
);

const [registerForm, { setFieldsValue, resetForm, validate, setProps }] = useForm({
  labelWidth: 160,
  showActionButtonGroup: false,
  baseColProps: { span: 12 },
  schemas: modalSchemas,
});
function handleCancle() {
  closeModal();
  resetForm();
}
function handleChange(e) {
  console.log(e);
  reload();
}
const isConcern = ref<any>(null);
async function handleSubmit() {
  try {
    modalLoading(true);

    if (getSelectRows().length <= 0) {
      message.error("请选择电梯");
      return;
    }
    const elevatorList = getSelectRows().map((item) => {
      return item.elevatorId;
    });

    const userId = info.value.userId;

    isConcern.value = activeKey.value === 1 ? 0 : 1;
    const data = { elevatorList, userId, isConcern: isConcern.value };
    // console.log(data, "data");
    await batchConcernPeo(data);
    emit("reload");
    closeModal();
    await resetForm();
  } catch (e) {
    console.warn(e);
  } finally {
    modalLoading(false);
  }
}
</script>

<style scoped>
.vben-basic-modal .ant-modal .ant-modal-body > .scrollbar {
  padding: 0px !important;
}
</style>
