<template>
  <div class="app-container">
    <!-- 列表 -->
    <!-- 搜索 -->
    <page-search
      ref="searchRef"
      :search-config="searchConfig"
      @query-click="handleQueryClick"
      @reset-click="handleResetClick"
    />
    <!-- 列表 -->
    <page-content
      ref="contentRef"
      :content-config="contentConfig"
      @add-click="handleAddClick"
      @edit-click="handleEditClick"
      @export-click="handleExportClick"
      @search-click="handleSearchClick"
      @toolbar-click="handleToolbarClick"
      @operat-click="handleOperatClick"
      @filter-change="handleFilterChange"
    >
      <template #status="scope">
        <el-tag :type="scope.row[scope.prop] == 1 ? 'success' : 'info'">
          {{ scope.row[scope.prop] == 1 ? "启用" : "禁用" }}
        </el-tag>
      </template>
      <template #options="scope">
        {{ returnOptionsLabel(scope.prop, scope.row[scope.prop]) }}
      </template>
      <template #gender="scope">
        <DictLabel v-model="scope.row[scope.prop]" code="gender" />
      </template>
      <template #mobile="scope">
        <el-text>{{ scope.row[scope.prop] }}</el-text>
        <copy-button
          v-if="scope.row[scope.prop]"
          :text="scope.row[scope.prop]"
          style="margin-left: 2px"
        />
      </template>
    </page-content>

    <!-- 新增 -->
    <page-modal ref="addModalRef" :modal-config="addModalConfig" @submit-click="handleSubmitClick">
      <template #formFooter>
        <!-- <h3 style="color: rgb(63, 158, 255)">收银机权限设置</h3>
        <div>
          <el-checkbox-group v-model="addPagePathIdList">
            <el-checkbox v-for="(item, index) in pagePathIdLists" :key="index" :value="item.value"
              :label="item.label" />
          </el-checkbox-group>
        </div>
        <h3 style="color: rgb(63, 158, 255)">员工权限设置</h3>

        <selectPermission v-model="addSelPermissionList" :list="permissionList"></selectPermission> -->
      </template>
    </page-modal>

    <!-- 编辑 -->
    <page-modal
      ref="editModalRef"
      :modal-config="editModalConfig"
      @submit-click="handleSubmitClick"
    >
      <template #formFooter>
        <h3 style="color: rgb(63, 158, 255)">收银机权限设置</h3>
        <div>
          <el-checkbox-group v-model="editPagePathIdList">
            <el-checkbox
              v-for="(item, index) in pagePathIdLists"
              :key="index"
              :value="item.value"
              :label="item.label"
            />
          </el-checkbox-group>
        </div>
        <h3 style="color: rgb(63, 158, 255)">员工权限设置</h3>

        <selectPermission v-model="selPermissionList" :list="permissionList"></selectPermission>
      </template>
      -->
    </page-modal>

    <!-- 修改密码 -->
    <changePwd ref="changePwdRef" />
  </div>
</template>

<script setup lang="ts">
import changePwd from "./components/change-pwd.vue";
import type { IObject, IOperatData } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";
import addModalConfig from "./config/add";
import contentConfig from "./config/content";
import editModalConfig from "./config/edit";
import searchConfig from "./config/search";
import { returnOptionsLabel } from "./config/config";
import RoleApi, { type SysRole } from "@/api/account/role";
import ShopStaffApi from "@/api/account/shopStaff";
import permissionApi, { type ShopPermission } from "@/api/account/permission";
import selectPermission from "./components/select-permission.vue";
import shopPagePermissionApi from "@/api/account/shopPagePermission";

const {
  searchRef,
  contentRef,
  addModalRef,
  editModalRef,
  handleQueryClick,
  handleResetClick,
  // handleAddClick,
  // handleEditClick,
  handleSubmitClick,
  handleExportClick,
  handleSearchClick,
  handleFilterChange,
} = usePage();

//店铺权限列表
let permissionList = ref<ShopPermission[]>([]);
//选中的权限列表
let selPermissionList = ref<string[][]>([]);
let addSelPermissionList = ref<string[]>([]);

// 选中的收音机页面列表
interface PagePath {
  label: string;
  value: string;
  [property: string]: any;
}

const pagePathIdLists = ref<PagePath[]>([]);
const addPagePathIdList = ref<string[]>([]);
const editPagePathIdList = ref<string[]>([]);

const oldAddSubmitFunc = addModalConfig.formAction;

const oldeditSubmitFunc = editModalConfig.formAction;
// 数据初始化
async function init() {
  // 覆写添加确定方法
  if (
    JSON.parse(localStorage.getItem("userInfo") || "{}").isHeadShop == 0 &&
    localStorage.getItem("loginType") == "0"
  ) {
    searchConfig.formItems.splice(0, 1);
  }
  addModalConfig.formAction = function (data) {
    return ShopStaffApi.add({
      ...data,
      pagePathIdList: addPagePathIdList.value,
      shopPermissionIds: addSelPermissionList.value.reduce((pre: string[], cur: string) => {
        return pre.concat(cur);
      }, [] as string[]),
    });
  };
  // 覆写编辑确定方法
  editModalConfig.formAction = function (data) {
    return ShopStaffApi.edit({
      ...data,
      pagePathIdList: editPagePathIdList.value,
      shopPermissionIds: selPermissionList.value.reduce((pre: string[], cur: string) => {
        return pre.concat(cur);
      }, [] as string[]),
    });
  };
  const res = await RoleApi.getList({ page: 1, size: 100 });
  const permission = await permissionApi.getshopPermission();
  permissionList.value = Array.isArray(permission) ? permission : [];
  const roleArr = res.records.map((item: SysRole) => {
    return {
      ...item,
      label: item.name,
      value: item.id,
    };
  });
  addModalConfig.formItems[1]!.options = roleArr;
  editModalConfig.formItems[1]!.options = roleArr;
  const pagesRes = await shopPagePermissionApi.getPageAll({});
  pagePathIdLists.value = pagesRes.map((item: any) => {
    return {
      ...item,
      label: item.name,
      value: item.id,
    };
  });
  addPagePathIdList.value = pagePathIdLists.value.map((v) => v.id);
}
init();

// 新增
async function handleAddClick() {
  selPermissionList.value = [];
  addModalRef.value?.setModalVisible();
}
// 编辑
async function handleEditClick(row: IObject) {
  selPermissionList.value = [];
  editModalRef.value?.handleDisabled(false);
  editModalRef.value?.setModalVisible();
  // 设置权限选中
  const perselArr = await permissionApi.getPermission(row.id);
  for (let id of perselArr) {
    for (let index in permissionList.value) {
      const item = permissionList.value[index];
      if (item.children) {
        for (let child of item.children) {
          if (child.id != null && child.id == id) {
            if (selPermissionList.value[index]) {
              selPermissionList.value[index].push(id);
            } else {
              selPermissionList.value[index] = [id];
            }
          }
        }
      }
    }
  }
  const pageList = await shopPagePermissionApi.detail({ staffId: row.id });
  editPagePathIdList.value = pageList.map((item) => item.id);
  // 根据id获取数据进行填充
  await ShopStaffApi.get(row.id).then((res) => {
    console.log(res);
    editModalRef.value?.setFormData({ ...res });
  });
}
// 其他工具栏
function handleToolbarClick(name: string) {
  console.log(name);
  if (name === "custom1") {
    ElMessage.success("点击了自定义1按钮");
  }
}

const changePwdRef = ref();
function changePwdRefShow(row: IObject) {
  changePwdRef.value?.open(row);
}
// 其他操作列
async function handleOperatClick(data: IOperatData) {
  console.log(data);
  if (data.name === "change_pwd") {
    changePwdRefShow(data.row);
  }
}

// 切换示例
</script>
