diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index a45a796..6f0d0b1 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -2,7 +2,6 @@ import "$lib/global.css"; import { path } from "@tauri-apps/api"; import Database from "@tauri-apps/plugin-sql"; - // import { writeTextFile, BaseDirectory } from "@tauri-apps/plugin-fs"; import ScreenWrap from "$lib/components/ui/ScreenWrap.svelte"; import Dialog from "$lib/components/ui/Dialog.svelte"; @@ -11,7 +10,6 @@ import TextInput from "$lib/components/form/TextInput.svelte"; import NumberInput from "$lib/components/form/NumberInput.svelte"; import ItemList from "$lib/components/form/ItemList.svelte"; - // import DateInput from "$lib/components/form/DateInput.svelte"; import PhoneInput from "$lib/components/form/PhoneInput.svelte"; import Select from "$lib/components/form/Select.svelte"; @@ -25,8 +23,8 @@ import ViewTable from "$lib/components/shorts/ViewTable.svelte"; import TableListShort from "$lib/components/shorts/TableListShort.svelte"; - /** @type {Database} */ - let db; + /** @type {Database | undefined} */ + let db = $state(); async function load_db() { // dev @@ -166,11 +164,11 @@ CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); }; /** @type {any} */ - export let current_item = {}; + let { current_item = $bindable({}) } = $props(); - let is_item_dialog_open = false; - let is_dialog_item_add = false; - let is_login_requied = false; + let is_item_dialog_open = $state(false); + let is_dialog_item_add = $state(false); + const is_login_requied = false; /** * @param {string} access_level @@ -195,7 +193,7 @@ CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); */ async function check_login(login, passowrd) { const access_type = - await db.select(`SELECT AccountAccessLevel.name FROM Accounts + await db?.select(`SELECT AccountAccessLevel.name FROM Accounts JOIN AccountAccessLevel ON AccountAccessLevel.id = Accounts.account_access_level_id WHERE Accounts.login = "${login}" AND Accounts.password = "${passowrd}";`); if (access_type.length === 0) { @@ -209,7 +207,7 @@ WHERE Accounts.login = "${login}" AND Accounts.password = "${passowrd}";`); @param {Array} ar */ async function id_buffer(ar) { - await db.execute(`delete from IdBuffer; + await db?.execute(`delete from IdBuffer; insert into IdBuffer (id) values (${ar.join("), (")});`); } @@ -223,547 +221,535 @@ insert into IdBuffer (id) values (${ar.join("), (")});`); {is_login_requied} bind:is_item_dialog_open bind:is_dialog_item_add - bind:current_item - let:current_page - let:current_table - let:current_view - let:is_view_open - let:cur_dialog_name - let:toggle_add_dialog - let:open_item_edit - let:sql_update_short - let:sql_insert_short - let:sql_delete_short -> - {#if !is_view_open && current_page === "Запросы"} - {#if current_table === "Pizzas"} - {@const cur_cols = ["name", "price"]} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - + bind:current_item> - - { - await id_buffer(current_item.ingredients_ids); - const query = ` -select sum(price) as value -from Ingredients -join IdBuffer on IdBuffer.id = Ingredients.id -`; - const price = await db.select(query); - current_item.price = price[0].value; - }} - > - + {#snippet children({ current_page, current_table, current_view, is_view_open, cur_dialog_name, toggle_add_dialog, open_item_edit, sql_update_short, sql_insert_short, sql_delete_short })} - - - + {#if !is_view_open && current_page === "Запросы"} + {#if current_table === "Pizzas"} + {@const cur_cols = ["name", "price"]} + + {#snippet children({ form })} - { - const las_id = await sql_insert_short( - current_table, - cur_cols, - "returning id", - ); - console.log(las_id.lastInsertId); + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} - const query = `insert into PizzasIngredientsJoin (pizza_id, ingredient_id) values -${current_item.ingredients_ids.map((el) => "(" + las_id.lastInsertId + "," + el + ")").join(",")} -`; - console.log(query); - await db.execute(query); - }} - on_save={async () => { - const query = ` -delete from PizzasIngredientsJoin where pizza_id = ${current_item.id}; -insert into PizzasIngredientsJoin (pizza_id, ingredient_id) values -${current_item.ingredients_ids.map((el) => "(" + current_item.id + "," + el + ")").join(",")} -`; - await db.execute(query); - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - >
-
- { - current_item = { - id: 0, - name: "", - price: 0, - ingredients_ids: [], - }; - }} - columns={cur_cols} - let:is_searching - let:search_query_result - > - - { - const ingredients_ids = await db.select( - `select ingredient_id from PizzasIngredientsJoin where pizza_id = ${item.id}`, - ); - open_item_edit(item); - current_item.ingredients_ids = ingredients_ids.map( - (/** @type {any} */ el) => el.ingredient_id, - ); - }} - > - - -
-
- {:else if current_table === "PizzasIngredientsJoin"} - {@const cur_cols = ["pizza_id", "ingredient_id"]} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- { - current_item = { - id: 0, - pizza_id: null, - ingredient_id: null, - }; - }} - columns={cur_cols} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - -
-
- {:else if current_table === "Ingredients"} - {@const cur_cols = ["name", "price"]} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- { - current_item = { - id: 0, - name: "", - price: 0, - }; - }} - columns={cur_cols} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - -
-
- {:else if current_table === "Clients"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - - - - - - - {@const cur_cols = ["phone", "fname", "sname", "tname"]} - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- { - current_item = { - id: 0, - phone: "+7 (", - fname: "", - sname: "", - tname: "", - }; - }} - columns={["phone", "fname", "sname", "tname"]} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - - -
{item.sname} {item.fname[0]}.{item.tname[0]}
-
- {:else if current_table === "Requests"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - {#if !is_dialog_item_add} - - - - {/if} - - - - {#if !is_dialog_item_add} -

Дата начала {current_item.initial_date}

- {/if} - - - - {#if !is_dialog_item_add} - - - - {/if} - {@const cur_cols = [ - "client_id", - "employee_id", - "request_status_id", - "request_type_id", - "initial_date", - "description", - "duration", - ]} + + + - { - await sql_insert_short(current_table, [ + + { + await id_buffer(current_item.ingredients_ids); + const query = `select sum(price) as value from Ingredients join IdBuffer on IdBuffer.id = Ingredients.id`; + const price = await db?.select(query); + current_item.price = price[0].value; + }} + > + + + + + + + { + const las_id = await sql_insert_short( + current_table, + cur_cols, + "returning id", + ); + // console.log(las_id.lastInsertId); + + const query = `insert into PizzasIngredientsJoin (pizza_id, ingredient_id) values + ${current_item.ingredients_ids.map((el) => "(" + las_id.lastInsertId + "," + el + ")").join(",")}`; + + // console.log(query); + await db?.execute(query); + }} + on_save={async () => { + const query = ` + delete from PizzasIngredientsJoin where pizza_id = ${current_item.id}; + insert into PizzasIngredientsJoin (pizza_id, ingredient_id) values + ${current_item.ingredients_ids.map((el) => "(" + current_item.id + "," + el + ")").join(",")}`; + + await db?.execute(query); + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + {/snippet} +
+ + { + current_item = { + id: 0, + name: "", + price: 0, + ingredients_ids: [], + }; + }}> + {#snippet children({ is_searching, search_query_result })} + + {#snippet children({ item, index})} + { + const ingredients_ids = await db?.select( + `select ingredient_id from PizzasIngredientsJoin where pizza_id = ${item.id}`, + ); + open_item_edit(item); + current_item.ingredients_ids = ingredients_ids.map( + (/** @type {any} */ el) => el.ingredient_id, + ); + }}> + + + + {/snippet} +
+ {/snippet} +
+ + {:else if current_table === "PizzasIngredientsJoin"} + {@const cur_cols = ["pizza_id", "ingredient_id"]} + + {#snippet children({ form })} + + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} + + + + + + + + + + { + await sql_insert_short(current_table, cur_cols); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + {/snippet} +
+ + { + current_item = { + id: 0, + pizza_id: null, + ingredient_id: null, + }; + }}> + {#snippet children({is_searching, search_query_result})} + + + + {#snippet children({ item, index})} + open_item_edit(item)}> + + + {/snippet} + +
+ {/snippet} +
+ + {:else if current_table === "Ingredients"} + {@const cur_cols = ["name", "price"]} + + {#snippet children({ form })} + + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} + + + + + + + + + + { + await sql_insert_short(current_table, cur_cols); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + + {/snippet} +
+ + { + current_item = { + id: 0, + name: "", + price: 0, + }; + }}> + {#snippet children({ is_searching, search_query_result})} + + {#snippet children({ item, index})} + open_item_edit(item)}> + + + {/snippet} +
+ {/snippet} +
+ + {:else if current_table === "Clients"} + + {#snippet children({ form })} + + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} + + + + + + + + + + + + + + + + + + {@const cur_cols = ["phone", "fname", "sname", "tname"]} + { + await sql_insert_short(current_table, cur_cols); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + {/snippet} +
+ + { + current_item = { + id: 0, + phone: "+7 (", + fname: "", + sname: "", + tname: "", + }; + }}> + {#snippet children({ is_searching, search_query_result })} + + {#snippet children({ item, index })} + open_item_edit(item)}> + + + + {/snippet} +
{item.sname} {item.fname[0]}.{item.tname[0]}
+ {/snippet} +
+ + {:else if current_table === "Requests"} + + {#snippet children({ form })} + + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} + + + + + + + + + + {#if !is_dialog_item_add} + + + + {/if} + + + + + + {#if !is_dialog_item_add} +

Дата начала {current_item.initial_date}

+ {/if} + + + + + + {#if !is_dialog_item_add} + + + + {/if} + + {@const cur_cols = [ "client_id", "employee_id", "request_status_id", "request_type_id", + "initial_date", "description", "duration", - ]); + ]} + + { + await sql_insert_short(current_table, [ + "client_id", + "employee_id", + "request_status_id", + "request_type_id", + "description", + "duration", + ]); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + {/snippet} +
+ + { + current_item = { + id: 0, + client_id: 0, + employee_id: 1, + request_status_id: 1, + request_type_id: 1, + description: "", + duration: null, + }; }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- { - current_item = { - id: 0, - client_id: 0, - employee_id: 1, - request_status_id: 1, - request_type_id: 1, - description: "", - duration: null, - }; - }} - columns={[ - "client_id", - "employee_id", - "request_status_id", - "request_type_id", - "initial_date", - "description", - "duration", - ]} - ex_params={[ - { - name: "Clients.fname", - is_number: false, - }, - { - name: "Clients.sname", - is_number: false, - }, - { - name: "Clients.tname", - is_number: false, - }, - { - name: "Employees.fname", - is_number: false, - }, - { - name: "Employees.sname", - is_number: false, - }, - { - name: "Employees.tname", - is_number: false, - }, - { - name: "RequestStatus.name", - is_number: false, - }, - { - name: "RequestType.name", - is_number: false, - }, - ]} - joins={` + columns={[ + "client_id", + "employee_id", + "request_status_id", + "request_type_id", + "initial_date", + "description", + "duration", + ]} + ex_params={[ + { + name: "Clients.fname", + is_number: false, + }, + { + name: "Clients.sname", + is_number: false, + }, + { + name: "Clients.tname", + is_number: false, + }, + { + name: "Employees.fname", + is_number: false, + }, + { + name: "Employees.sname", + is_number: false, + }, + { + name: "Employees.tname", + is_number: false, + }, + { + name: "RequestStatus.name", + is_number: false, + }, + { + name: "RequestType.name", + is_number: false, + }, + ]} + joins={` JOIN Clients on Clients.id = Requests.client_id JOIN Employees on Employees.id = Requests.employee_id JOIN RequestStatus on RequestStatus.id = Requests.request_status_id JOIN RequestType on RequestType.id = Requests.request_type_id -`} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - - - - - - -
- {item.id} - - {#await db.select(`select fname, sname, tname from Clients where id = ${item.client_id}`) then data} - {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. - {/await} - - {#await db.select(`select id, fname, sname, tname from Employees where id = ${item.employee_id}`) then data} - {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. - {/await} - - {#await db.select(`select id, name from RequestStatus where id = ${item.request_status_id}`) then data} - {data[0].name} - {/await} - - {#await db.select(`select id, name from RequestType where id = ${item.request_type_id}`) then data} - {data[0].name} - {/await} -
-
- {/if} - {:else if is_view_open} - {#if current_view === "RequestCount"} - - {:else if current_view === "AvgRequestTime"} - + {#snippet children({ is_searching, search_query_result})} + + {#snippet children({ item, index})} + open_item_edit(item)}> + + + + + + + + {/snippet} +
+ {item.id} + + {#await db?.select(`select fname, sname, tname from Clients where id = ${item.client_id}`) then data} + {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. + {/await} + + {#await db?.select(`select id, fname, sname, tname from Employees where id = ${item.employee_id}`) then data} + {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. + {/await} + + {#await db?.select(`select id, name from RequestStatus where id = ${item.request_status_id}`) then data} + {data[0].name} + {/await} + + {#await db?.select(`select id, name from RequestType where id = ${item.request_type_id}`) then data} + {data[0].name} + {/await} +
+ {/snippet} + + {/if} + + {:else if is_view_open} + {#if current_view === "RequestCount"} + + + {:else if current_view === "AvgRequestTime"} + - {:else if current_view === "StatByType"} - + + {:else if current_view === "StatByType"} + + columns={["название", "среднее время", "количество"]} + names={["type_name", "avg_duratino", "type_count"]} + >
+ {/if} {/if} - {/if} - {#if current_page === "Сотрудники"} - {#if current_table === "Employees"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} + {#if current_page === "Сотрудники"} + {#if current_table === "Employees"} + + {#snippet children({ form })} + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} - - - + + + - - - + + + - - - + + + - - - + + + - {@const cur_cols = ["employee_status_id", "fname", "sname", "tname"]} + {@const cur_cols = ["employee_status_id", "fname", "sname", "tname"]} - { + await sql_insert_short(current_table, cur_cols); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + {/snippet} +
+ + { + current_item = { + id: 0, + employee_status_id: 1, + fname: "", + sname: "", + tname: "", + }; + }} + columns={["employee_status_id", "fname", "sname", "tname"]} + joins={`JOIN EmployeeStatus on EmployeeStatus.id = Employees.employee_status_id`} + ex_params={[ + { + name: "EmployeeStatus.name", + is_number: false, + }, + ]}> + {#snippet children({ is_searching, search_query_result })} + + + {#snippet children({ item, index })} + open_item_edit(item)}> + + + + + {/snippet} +
+ {#await db?.select(`select name from EmployeeStatus where id = ${item.employee_status_id}`) then data} + {data[0].name} + {/await} + {item.sname} {item.fname[0]}.{item.tname[0]}
+ {/snippet} +
+ + {:else if current_table === "Accounts"} + + {#snippet children({ form })} + + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} + + + + + + + + + + + + + + + + + + {@const cur_cols = [ + "employee_id", + "account_access_level_id", + "login", + "password", + ]} + + { + await sql_insert_short(current_table, cur_cols); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > + {/snippet} +
+ + { + current_item = { + id: 0, + employee_id: 0, + account_access_level_id: 0, + login: "", + password: "", + }; + }} + columns={[ + "employee_id", + "account_access_level_id", + "login", + "password", + ]} + joins={"JOIN Employees on Accounts.employee_id = Employees.id"} + ex_params={[ + { + name: "Employees.fname", + is_number: false, + }, + { + name: "Employees.sname", + is_number: false, + }, + { + name: "Employees.tname", + is_number: false, + }, + ]}> + {#snippet children({ is_searching, search_query_result })} + + {#snippet children({ item, index })} + open_item_edit(item)}> + + + + + + {/snippet} +
+ {#await db?.select(`select id, fname, sname, tname from Employees where id = ${item.employee_id}`) then data} + {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. + {/await} + + {#await db?.select(`select name from AccountAccessLevel where id = ${item.account_access_level_id}`) then data} + {data[0].name} + {/await} +
+ {/snippet} +
+ + {:else if current_table === "AccountAccessLevel"} + { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
+ bind:current_item + query={db?.select(`select * from ${current_table}`)} + columns={["id", "name"]} + columns_display={["id", "название"]} + {toggle_add_dialog} + {open_item_edit} + {sql_insert_short} + {sql_update_short} + {sql_delete_short} + > + {/if} + {/if} - { - current_item = { - id: 0, - employee_status_id: 1, - fname: "", - sname: "", - tname: "", - }; - }} - columns={["employee_status_id", "fname", "sname", "tname"]} - joins={`JOIN EmployeeStatus on EmployeeStatus.id = Employees.employee_status_id`} - ex_params={[ - { - name: "EmployeeStatus.name", - is_number: false, - }, - ]} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - - - - -
- {#await db.select(`select name from EmployeeStatus where id = ${item.employee_status_id}`) then data} - {data[0].name} - {/await} - {item.sname} {item.fname[0]}.{item.tname[0]}
-
- {:else if current_table === "Accounts"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - - - - - - - - - - - - {@const cur_cols = [ - "employee_id", - "account_access_level_id", - "login", - "password", - ]} - - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- - { - current_item = { - id: 0, - employee_id: 0, - account_access_level_id: 0, - login: "", - password: "", - }; - }} - columns={[ - "employee_id", - "account_access_level_id", - "login", - "password", - ]} - joins={"JOIN Employees on Accounts.employee_id = Employees.id"} - ex_params={[ - { - name: "Employees.fname", - is_number: false, - }, - { - name: "Employees.sname", - is_number: false, - }, - { - name: "Employees.tname", - is_number: false, - }, - ]} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - - - - -
- {#await db.select(`select id, fname, sname, tname from Employees where id = ${item.employee_id}`) then data} - {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. - {/await} - - {#await db.select(`select name from AccountAccessLevel where id = ${item.account_access_level_id}`) then data} - {data[0].name} - {/await} -
-
- {:else if current_table === "AccountAccessLevel"} - + bind:current_item + query={db?.select(`select * from ${current_table}`)} + columns={["id", "name"]} + columns_display={["id", "название"]} + {toggle_add_dialog} + {open_item_edit} + {sql_insert_short} + {sql_update_short} + {sql_delete_short} + > + {/if} {/if} - {/if} - - {#if !is_view_open && current_page === "Списки"} - {#if ["RequestStatus", "RequestType", "EmployeeStatus"].includes(current_table)} - - {/if} - {/if} + {/snippet}