<template>
    <el-row class="page">
        <el-row class="block-view" v-for="(item, index) in expressList">
            <el-row class="top">
                <el-text>运单号: {{ item.expressid }}</el-text>
                <el-text class="top-time">签收时间: {{ item.signedTime }}</el-text>
            </el-row>
            <el-row class="express-view">
                <el-row class="express-no-view">
                    <el-text class="city">{{ item.senderCity }}</el-text>
                    <el-text class="user">{{ item.senderName }}</el-text>
                </el-row>
                <el-row class="express-no-view">
                    <el-image class="arrow" src="/arrow.png" />
                    <el-text class="sign">{{ item.isSigned ? "已签收" : "未签收" }}</el-text>
                </el-row>
                <el-row class="express-no-view">
                    <el-text class="city">{{ item.receiverCity }}</el-text>
                    <el-text class="user">{{ item.receiverName }}</el-text>
                </el-row>
                <el-row class="express-icon-view">
                    <el-icon @click="deleteByIndex(index)">
                        <Delete />
                    </el-icon>
                    <el-icon @click="modify(item, index)">
                        <Edit />
                    </el-icon>
                </el-row>
            </el-row>
            <el-row class="line-view"></el-row>
        </el-row>

        <el-dialog v-model="dialogVisible" title="警告" width="500" :before-close="handleClose">
            <span>你确定要删除这条快递信息吗？</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="deleteByIndexConfirm()">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>


        <el-dialog v-model="dialogFormVisible" title="快递表单修改" width="500">
            <el-form :model="form">
                <el-form-item label="寄送城市" :label-width="formLabelWidth">
                    <el-input v-model="form.senderCity" autocomplete="off" />
                </el-form-item>
                <el-form-item label="寄送人" :label-width="formLabelWidth">
                    <el-input v-model="form.senderName" autocomplete="off" />
                </el-form-item>
                <el-form-item label="收件城市" :label-width="formLabelWidth">
                    <el-input v-model="form.receiverCity" autocomplete="off" />
                </el-form-item>
                <el-form-item label="收件人" :label-width="formLabelWidth">
                    <el-input v-model="form.receiverName" autocomplete="off" />
                </el-form-item>
                <el-form-item label="签收时间" :label-width="formLabelWidth">
                    <el-input v-model="form.signedTime" autocomplete="off" />
                </el-form-item>

            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="modifyConfirm()">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </el-row>
</template>

<script setup>
import { Delete, Edit } from '@element-plus/icons-vue';
import { ref, watchEffect, onMounted } from 'vue';
import { ElMessageBox } from 'element-plus';
import { useExpressStore } from "../../store"
import { getExpressForm } from "../../utils/tools"

const expressStore = useExpressStore()
const expressList = ref(expressStore.getSignedList)
// watchEffect(async () => {
//   if (expressStore.expressList.length === 0) {
//     await expressStore.getExpress();
//   }
//   expressList.value = expressStore.expressList;
// });

const form = ref(getExpressForm())

const dialogVisible = ref(false)
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

// 定义删除和编辑的处理函数  
let formIndex = -1



const deleteByIndex = (index) => {
    dialogVisible.value = true
    formIndex = index
}

const deleteByIndexConfirm = () => {
    dialogVisible.value = false
    expressStore.deleteExpress(form, formIndex)
}

const modify = (item, index) => {
    dialogFormVisible.value = true
    const formTable = form.value
    formTable.senderCity = item.senderCity
    formTable.senderName = item.senderName
    formTable.receiverCity = item.receiverCity
    formTable.receiverName = item.receiverName
    formTable.signedTime = item.signedTime
    formIndex = index
}
const modifyConfirm = () => {
    dialogFormVisible.value = false
    expressStore.modifyExpress(form, formIndex)
}

const handleClose = (done) => {
    ElMessageBox.confirm('你确定要关闭吗？')
        .then(() => {
            done()
        })
        .catch(() => {
            // catch error
        })
}

</script>

<style>
.page {
    display: flex;
    flex-direction: column;
    width: 100%;
    background-color: white;
    line-height: 20px;
}

.block-view {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.top {
    display: flex;
    justify-content: flex-start;
    width: 100%;
    /* margin-left: 20px; */
    padding-left: 20px;
    color: gray;
    font-size: 12px;
}

.top-time {
    margin-left: 690px;
}

.express-view {
    display: flex;
    flex-flow: row;
    justify-content: space-between;
    width: 100%;
    padding-left: 20px;
}

.express-no-view {
    display: flex;
    flex-direction: column;
    /* justify-content: space-around; */
}

.arrow {
    width: 40px;
    height: 10px;
}

.city {
    font-size: 20px;
    margin-bottom: 10px;
}

.user {
    color: gray;
}

.sign {
    color: red;
    margin-top: 10px;
}

.express-icon-view {
    /* width: 100%; */
    width: 50px;
    margin-right: 50px;
    justify-content: space-around;
    flex-direction: row;
}

.line-view {
    height: 0.5px;
    width: 95%;
    background-color: lightgray;
    margin-top: 20px;
    margin-bottom: 20px;
}
</style>