<template>
    <div>
        <!-- <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="金额" prop="money">
                    <el-input class="w-[280px]" v-model="queryParams.money" clearable placeholder="请输入金额" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card> -->
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div>
                <el-table :data="pager.lists" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
                    <el-table-column label="ID" prop="id" min-width="55"/>
                    <el-table-column label="店铺ID" prop="store_id" show-overflow-tooltip />
                    <el-table-column label="时间" prop="create_time" show-overflow-tooltip />
                    <el-table-column label="收款方" prop="store_name" show-overflow-tooltip />
                    <el-table-column label="结算金额" prop="money" show-overflow-tooltip />
                    <el-table-column label="结算方式" prop="extract_type" show-overflow-tooltip />
                    <el-table-column label="确认时间" prop="initiation_time" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ timeFormat(row.initiation_time, 'yyyy-mm-dd') }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <div class="flex" v-if="row.receivable">
                                <el-button link type="primary" @click="handleDelete(row.id)" v-if="row.status == 0">
                                    发送
                                </el-button>
                                <el-button link type="primary" @click="handleClick(row)" v-if="row.status == 2">
                                    确认
                                </el-button>
                            </div>
                            <div v-else></div>
                        </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 v-model="showDialog" title="确认结算" width="680px">
            <el-descriptions :column="1" border>
                <el-descriptions-item label="姓名"> {{ rowDate.store_realName }} </el-descriptions-item>
                <el-descriptions-item label="银行卡号"> {{ rowDate.store_bank_code }} </el-descriptions-item>
                <el-descriptions-item label="银行名称"> {{ rowDate.store_bank }} </el-descriptions-item>
                <el-descriptions-item label="开户地址"> {{ rowDate.store_bank_address }} </el-descriptions-item>
                <el-descriptions-item label="操作">
                    <el-radio-group v-model="form.status">
                        <el-radio label="1">通过</el-radio>
                        <el-radio label="0">不通过</el-radio>
                    </el-radio-group>
                </el-descriptions-item>
                <el-descriptions-item label="附件">
                    <material-picker v-model="form.transfer_picture" />
                </el-descriptions-item>
                <el-descriptions-item label="备注">
                    <el-input class="w-[280px]" v-model="form.fail_msg" clearable placeholder="请输入备注" type="textarea" />
                </el-descriptions-item>
            </el-descriptions>
            <el-form class="mt-4">
                <el-form-item>
                    <el-button type="primary" @click="handleConfirm">确定</el-button>
                    <el-button @click="showDialog = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="financialTransfersLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiFinancialTransfersLists, apiFinancialTransfersSend, apiFinancialTransfersConfirm } from '@/api/financial_transfers'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'

// 查询条件
const queryParams = reactive({
    store_id: '',
    admin_id: '',
    uid: '',
    status: '',
    initiation_time: '',
    confirmation_time: '',
    mark: '',
    money: '',
    remark_time: ''
})

const tableRowClassName = ({
  row,
  rowIndex,
}) => {
    console.log(row.receivable);
    
  if (!row.receivable) {
    return 'warning-row'
  }
  return ''
}


// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

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


// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要发送吗？')
    await apiFinancialTransfersSend({ id })
    getLists()
}

const showDialog = ref(false)
const rowDate = ref('')

const handleClick = (row: any) => {
    rowDate.value = row
    showDialog.value = true
}

const form = reactive({
    id: "",
    transfer_picture: "",
    status: "",
    fail_msg: ""
})

// 删除
const handleConfirm = async (id: number | any[]) => {
    form.id = rowDate.value.id
    await apiFinancialTransfersConfirm({ ...form })
    getLists()
    showDialog.value = false
}

getLists()
</script>

<style>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-danger-light-9);
}
</style>