<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="80px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="时间筛选">
                            <el-date-picker v-model="date" type="daterange" range-separator="-" start-placeholder="开始时间"
                                end-placeholder="结束时间" value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="店员">
                            <el-select class="flex-1" v-model="queryParams.staff_id" placeholder="请选择店员">
                                <el-option :label="item.staff_name" :value="item.id" v-for="(item, index) in staffList" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="用户搜索">
                            <el-input v-model="queryParams.order_id" clearable placeholder="请输入订单编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" @click="handleResetPage">查询</el-button>
                            <el-button @click="handleResetParams">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <el-table-column label="订单号" prop="order_id" show-overflow-tooltip />
                    <el-table-column label="用户信息" prop="build_area_text" show-overflow-tooltip>
                        <template #default="{ row }">
                            <div class="flex items-center">
                                <el-image style="width: 50px; height: 50px" :src="row.avatar" class="mr-2" />
                                {{ row.nickname }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="支付金额" prop="" show-overflow-tooltip />
                    <el-table-column label="充值类型" prop="" show-overflow-tooltip />
                    <el-table-column label="支付时间" prop="" show-overflow-tooltip />
                    <el-table-column label="关联店员" prop="" show-overflow-tooltip />
                    <el-table-column label="操作" width="170" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleRe(row)">
                                备注
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <el-dialog title="添加备注" v-model="showDialog" width="550px">
            <el-form-item>
                <el-input v-model="remarks" type="textarea" placeholder="请输入备注内容"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleRemarks">确定</el-button>
                <el-button @click="showDialog = false">取消</el-button>
            </el-form-item>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="manageProjectLists">
import orderDetail from './../../components/orderDetail/index.vue'
import { ref, reactive } from 'vue'
import { usePaging } from '@/hooks/usePaging'
import { apiStoreOrderLists, apiStoreOrderDetail, apiStoreOrderTitle } from '@/api/store_order'
import feedback from '@/utils/feedback'

// 查询条件
const queryParams = reactive({
    order_id: "",
    pay_type: "",
    start_time: "",
    end_time: '',
    status: "",
    staff_id: ""

})

const date = ref([])
const showDetail = ref(false)

// 核销
const handleWriteOff = async (id: number) => {
    await feedback.confirm('确定要核销订单吗？')
    // await adminDelete({ id })
    getLists()
}


// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiStoreOrderLists,
    params: queryParams
})

const handleResetPage = () => {
    if (date.value.length) {
        queryParams.start_time = date.value[0]
        queryParams.end_time = date.value[1]
    }
    resetPage()
}


const handleResetParams = () => {
    date.value = []
    resetParams()
}

const detailRef = ref(null)
const detailData = ref({})
// 编辑
const handleDetail = async (id: any) => {
    let res = await apiStoreOrderDetail({ id })
    console.log(res)
    detailData.value = res
    showDetail.value = true
    await nextTick()
    detailRef.value.open()
}


const payTypeList = ref([])
const staffList = ref([])
const order_status = ref({
    finish: '',
    wait_receive: '',
    wait_send: ""
})

const getTitle = async () => {
    let res = await await apiStoreOrderTitle()
    payTypeList.value = res.pay_type
    staffList.value = res.staff
    order_status.value = res.order_status
}

// getTitle()



const showDialog = ref(false)
const remarks = ref('')
const rows = ref({})

const handleRe = (row) => {
    showDialog.value = true
    rows.value = row
}

const handleRemarks = async (row: any) => {
    await apiStorFinanceFlowRemarks({ row })
    showDialog.value = false
}

getLists()
</script>