import {
  UserType,
  addSingleUser,
  addMultipleUser,
  UserTypeDel,
  delUser,
  updateUser,
  QueryUser,
  getUserList,
} from '@/api/system/user-operate';
import { ref } from 'vue';
import { QuerySelect } from '../constant';
import { defineStore } from 'pinia';

export const useUser = defineStore('user-list', () => {
  //查询的类型
  const queryType = ref<string>('');
  //查询输入的内容
  const queryTypeContent = ref<string>('');
  //查询的工号
  const queryStaffNo = ref<string>('');
  //查询的姓名
  const queryNickname = ref<string>('');
  //查询的手机号
  const queryMobile = ref<string>('');
  //查询的角色
  const queryRoleName = ref<string>('');
  //查询的组织
  const queryDeptName = ref<string>('');
  //user数据
  const userList = ref<UserTypeDel[]>([]);

  //编辑时的单个数据
  //   const userData = ref<UserType>();

  //当前的页数
  const page = ref<number>(1);
  //总页数
  //   const totalPage = ref<number>(1);
  //每页的数量
  const pageSize = ref<number>(10);
  //总数量
  const totalCount = ref<number>(0);

  //条件查询
  const conditionSearch = () => {
    const params: QueryUser = {
      page: page.value,
      pageRow: pageSize.value,
    };
    if (queryType.value === QuerySelect.staffNo) {
      queryStaffNo.value = queryTypeContent.value;
    } else if (queryType.value === QuerySelect.nickname) {
      queryNickname.value = queryTypeContent.value;
    } else if (queryType.value === QuerySelect.mobile) {
      queryMobile.value = queryTypeContent.value;
    }
    if (queryStaffNo.value) {
      params.staffNo = queryStaffNo.value;
    }
    if (queryNickname.value) {
      params.nickname = queryNickname.value;
    }
    if (queryMobile.value) {
      params.mobile = queryMobile.value;
    }
    if (queryRoleName.value) {
      params.roleName = queryRoleName.value;
    }
    if (queryDeptName.value) {
      params.deptName = queryDeptName.value;
    }
    getUserList(params).then((res) => {
      userList.value = res.list;
      totalCount.value = res.totalCount;
    });
  };

  //获取列表
  const getList = () => {
    getUserList(null).then((res) => {
      userList.value = res.list;
      console.log('userList.value', userList.value);

      totalCount.value = res.totalCount;
    });
  };

  //添加单个数据
  const singleUserAdd = (data: UserType) => {
    addSingleUser(data).then(() => {
      () => {
        conditionSearch();
      };
    });
  };

  //添加多个数据
  const multipleUserAdd = (data: File) => {
    addMultipleUser(data).then(() => {
      () => {
        conditionSearch();
      };
    });
  };

  //删除用户
  const userDel = (data: UserTypeDel) => {
    delUser(data).then(() => {
      conditionSearch();
    });
  };

  //修改用户
  const userUpdate = (data: UserType) => {
    updateUser(data).then(() => {
      conditionSearch();
    });
  };

  const resetArgs = () => {
    queryType.value = '';
    queryTypeContent.value = '';
    queryStaffNo.value = '';
    queryNickname.value = '';
    queryMobile.value = '';
    queryRoleName.value = '';
    queryDeptName.value = '';
    userList.value = [];
    page.value = 1;
    pageSize.value = 10;
    totalCount.value = 0;
  };

  return {
    queryType,
    queryTypeContent,
    queryStaffNo,
    queryNickname,
    queryMobile,
    queryRoleName,
    queryDeptName,
    userList,
    page,
    pageSize,
    totalCount,
    conditionSearch,
    getList,
    singleUserAdd,
    multipleUserAdd,
    userDel,
    userUpdate,
    resetArgs,
  };
});

//获取单个数据

//修改用户

export default useUser;
