<template>
  <BasicModal v-bind="$attrs" :title="title" @register="registerInnerModal" :width="1200">
    <template #footer>
      <div v-if="isUpdate !== 3">
        <a-button type="default" @click="handleCancle">返回</a-button>
      </div>
    </template>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          danger
          @click="multipleRemove(infoRemove)"
          :disabled="!selected"
          v-auth="'elevator:thirdPartyUser:remove'"
          >删除</a-button
        >
        <a-button type="primary" @click="handleAdd" v-auth="'elevator:thirdPartyUser:add'"
          >添加用户</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'isAdmin'">
          <div>
            {{ record.isAdmin === 1 ? "是" : "否" }}
          </div>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '查看',

                type: 'primary',
                ghost: true,
                auth: 'elevator:thirdPartyUser:query',
                onClick: handCheck.bind(null, record),
              },
              {
                label: '修改',

                type: 'primary',
                ghost: true,
                auth: 'elevator:thirdPartyUser:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',

                type: 'primary',
                danger: true,
                ghost: true,
                auth: 'elevator:thirdPartyUser:remove',
                popConfirm: {
                  placement: 'left',
                  title: '是否删除物业单位基础信息;[' + record.id + ']?',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <addUserModal @register="registerModal" @reload="reload"></addUserModal>
  </BasicModal>
</template>

<script setup lang="ts">
import { BasicModal, useModalInner } from "@/components/Modal";
import { computed, reactive, ref, unref } from "vue";
import { infoInfo, infoAdd, infoUpdate, infoList } from "@/api/basicInfo/tenement";
import { energyModalSchemas } from "./info.data";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import { formSchemas, columns } from "./info.data";
import addUserModal from "./AddUserModal.vue";
import { useModal } from "@/components/Modal";
import { thirdPartyUserList } from "@/api/system/user";
defineOptions({ name: "InfoModal" });
const info = ref<any>({});
const emit = defineEmits(["register", "reload"]);
const postList = ref<Post[]>([]);
const isUpdate = ref<number>(1);
const title = computed<string>(() => {
  return "用户管理";
});

const [registerInnerModal, { modalLoading, closeModal }] = useModalInner(
  async (data: { record?: Recordable; update: number }) => {
    modalLoading(true);
    const { record, update } = data;
    isUpdate.value = update;
    info.value = record;
    await reload();
    modalLoading(false);
  }
);
const [registerModal, { openModal }] = useModal();
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "用户列表",
  api: thirdPartyUserList,
  showIndexColumn: false,
  rowKey: "id",
  useSearchForm: false,
  formConfig: {
    schemas: formSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 6,
    },
  },
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    params.unitId = info.value.id;
    return params;
  },
  columns: columns,
  actionColumn: {
    width: 250,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});
function handleCancle() {
  closeModal();
}
function handleEdit(record: Recordable) {
  openModal(true, { info: info.value, record, update: 1 });
}

function handleAdd(record: Recordable) {
  openModal(true, { info: info.value, record, update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { info: info.value, record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await infoRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
