import { reactive, ref } from 'vue' import { create, list, remove, update } from '@/api/address_book_collection' import { ElMessage, ElMessageBox } from 'element-plus' import { T } from '@/utils/i18n' export function useRepositories (is_my) { const listRes = reactive({ list: [], total: 0, loading: false, }) const listQuery = reactive({ page: 1, page_size: 10, is_my, name: null, user_id: null, }) const getList = async () => { listRes.loading = true const res = await list(listQuery).catch(_ => false) listRes.loading = false if (res) { listRes.list = res.data.list listRes.total = res.data.total } } const handlerQuery = () => { if (listQuery.page === 1) { getList() } else { listQuery.page = 1 } } const del = async (row) => { const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), { confirmButtonText: T('Confirm'), cancelButtonText: T('Cancel'), type: 'warning', }).catch(_ => false) if (!cf) { return false } const res = await remove({ id: row.id }).catch(_ => false) if (res) { ElMessage.success(T('OperationSuccess')) getList() } } const formVisible = ref(false) const formData = reactive({ id: 0, name: '', }) const toEdit = (row) => { formVisible.value = true //将row中的数据赋值给formData Object.keys(formData).forEach(key => { formData[key] = row[key] }) } const toAdd = () => { formVisible.value = true //重置formData Object.keys(formData).forEach(key => { formData[key] = undefined }) } const submit = async () => { const api = formData.id ? update : create const res = await api(formData).catch(_ => false) if (res) { ElMessage.success(T('OperationSuccess')) formVisible.value = false getList() } } return { listRes, listQuery, getList, handlerQuery, del, formVisible, formData, toEdit, toAdd, submit, } }