import {
    defineStore
} from 'pinia';
import {
    ElMessage
} from 'element-plus'
import req from "../request"

export const useExpressStore = defineStore('expressStore', {
    state: () => ({
        expressList: []
    }),
    getters: {
        getExpressList() {
            return this.expressList
        },
        getSignedList() {
            return this.expressList.filter(item => item.isSigned)
        },
        getUnSignedList() {
            return this.expressList.filter(item => !item.isSigned)
        }
    },
    actions: {
        modifyExpress(form, formIndex) {
            let item = this.expressList[formIndex]
            const formTable = form.value
            item.senderCity = formTable.senderCity
            item.senderName = formTable.senderName
            item.receiverCity = formTable.receiverCity
            item.receiverName = formTable.receiverName
            item.signedTime = formTable.signedTime
            if (item.signedTime) {
                item.isSigned = 1
            }
            req({
                method: "put",
                url: "/express",
                data: item
            }).then(({
                data
            }) => {
                console.log(data, "modifyedData")
                ElMessage({
                    message: '修改成功',
                    type: 'success',
                    plain: true,
                })
            })
        },
        addExpress(form) {
            req({
                method: "post",
                url: "/express",
                data: form
            }).then((res) => {
                console.log(res.data, "addExpressData")
            })
            this.expressList.push(form);
        },
        setExpress(datas) {
            this.expressList = datas
        },
        deleteExpress(form, formIndex) {
            let item = this.expressList[formIndex]
            this.expressList.splice(formIndex, 1)
            req({
                method: "delete",
                url: "/express",
                data: item
            }).then(({
                data
            }) => {
                console.log(data, "deletedData")
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                    plain: true,
                })
            })
        }
    }
});