<template>
  <div class="app-container">
    <Filter
      v-model="filterData"
      :config="filterConfig"
      @handleAction="handleFilterData"
      @updateSchool="handleUpdateSchool"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >添加</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <FileUpload
          uploadUrl="/sc/family/ht/importFamilyInfo"
          :isShowTip="false"
          buttonText="导入"
          @handleSuccessUplaod="handleSuccessUplaod"
        />
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain @click="handleImportTemplate"
          >下载导入模板</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          @click="handleDelete"
          :disabled="multiple"
          >批量删除</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格 -->
    <Table
      :xbTable="xbTable"
      @tablePageChange="handleTablePageChange"
      @selectionChange="handleSelectionChange"
    >
      <template v-slot:relation="scope">
        <span v-for="i in selectConfig.relationOption" :key="i.value">
          {{ scope.row.sysFamilyDictValue === i.value ? i.label : "" }}
        </span>
      </template>
      <template v-slot:operation="scope">
        <el-button
          link
          type="primary"
          icon="Edit"
          @click="handleUpdate(scope.row)"
          >编辑</el-button
        >
        <el-button
          link
          type="danger"
          icon="Delete"
          @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </Table>
    <PatriachDia
      v-model="open"
      :detailInfo="detailInfo"
      :selectOption="selectConfig"
      @success="handleSuccessDia"
      @cancel="handleCancelDia"
    />
  </div>
</template>

<script setup name="patriarchManage">
import Filter from "@/views/smartCampus/components/Filter/index.vue";
import Table from "@/views/smartCampus/components/Table/index.vue";
import PatriachDia from "./patriachDia.vue";
import {
  getSelectDownData,
  useFetchGradeClassTree,
} from "@/utils/dropDownData";
import {
  getPatriarchListApi,
  deletePatriarchApi,
} from "@/api/smartCampus/index";
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();

const open = ref(false);
const detailInfo = ref({}); //详情
const ids = ref([]); //批量删除ids
const multiple = ref(true); //批量删除按钮
const filterData = ref({}); //过滤
const selectConfig = ref({
  gradeDataTree: [],
  relationOption: [],
});

const filterConfig = computed(() => {
  return [
    {
      comName: "admin",
      label: "学校名称",
      prop: "tenantId",
      placeholder: "请选择",
      clearable: true,
    },
    {
      comName: "el-tree-select",
      label: "学生班级",
      placeholder: "请选择",
      prop: "search",
      clearable: true,
      options: selectConfig.value.gradeDataTree,
      selectConfig: {
        label: "name",
        value: "name",
        children: "children",
      },
    },
    {
      comName: "el-input",
      label: "关联学生姓名",
      prop: "stuName",
      placeholder: "请输入",
      clearable: true,
    },
    {
      comName: "el-input",
      label: "家长姓名",
      prop: "name",
      placeholder: "请输入",
      clearable: true,
    },
    {
      comName: "el-input",
      label: "联系电话",
      prop: "phone",
      placeholder: "请输入",
      clearable: true,
    },
  ];
});
const xbTable = ref({
  loading: false,
  columns: [
    { type: "selection" },
    { prop: "name", label: "家长姓名" },
    { prop: "stuName", label: "关联学生姓名" },
    { prop: "sysFamilyDictValue", label: "称谓", slot: "relation" },
    { prop: "phone", label: "联系电话" },
    { label: "操作", slot: "operation" },
  ],
  tableData: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
  },
});

// 过滤按钮
async function handleFilterData(type) {
  switch (type.value) {
    case "search":
      getList();
      break;
    case "reset":
      xbTable.value.pagination.pageNum = 1;
      selectConfig.value.gradeDataTree = await getSelectDownData(
        "gradeClassTree"
      );
      getList();
      break;
  }
}

// 导入
function handleSuccessUplaod(val) {
  proxy.$modal.msgSuccess("导入成功");
  getList();
}
// 下载导入模版
function handleImportTemplate() {
  proxy.download(
    "/sc/family/ht/getFamilyImportTemplate",
    {},
    `family_template_${new Date().getTime()}.xlsx`
  );
}
/** 查询列表 */
function getList() {
  xbTable.value.loading = true;
  getPatriarchListApi({
    ...xbTable.value.pagination,
    ...filterData.value,
  }).then((res) => {
    xbTable.value.tableData = res.rows;
    xbTable.value.loading = false;
    xbTable.value.pagination.total = res.total;
  });
}
// 表格勾选
function handleSelectionChange(val) {
  ids.value = val.map((item) => item.id);
  multiple.value = !val.length;
}
// 表格分页
function handleTablePageChange(val) {
  xbTable.value.pagination.pageSize = val.limit;
  xbTable.value.pagination.pageNum = val.page;
  getList();
}
/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  detailInfo.value = {};
}
// 编辑或新增成功
function handleSuccessDia(val) {
  open.value = val;
  getList();
}
/** 取消弹窗 */
function handleCancelDia(val) {
  open.value = val;
  detailInfo.value = {};
}
/** 删除按钮操作 */
function handleDelete(row) {
  const deleteId = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除？")
    .then(() => {
      xbTable.value.loading = true;
      return deletePatriarchApi(deleteId);
    })
    .then(() => {
      xbTable.value.loading = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .finally(() => {
      xbTable.value.loading = false;
    });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  detailInfo.value = row;
}

// 获取下拉数据
async function getSelectOption() {
  selectConfig.value.relationOption = await getSelectDownData("relation");
  selectConfig.value.gradeDataTree = await getSelectDownData("gradeClassTree");
}
// 学校
async function handleUpdateSchool() {
  filterData.value.search = "";
  selectConfig.value.gradeDataTree = await useFetchGradeClassTree({
    tenantId: filterData.value.tenantId,
  });
}
// 超管
function handleAdminRole() {
  if (useUserStore().showAllTenant) {
    xbTable.value.columns.splice(1, 0, {
      prop: "tenantName",
      label: "学校名称",
    });
  }
}
handleAdminRole();
getSelectOption();
getList();
</script>
