<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="160px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="商户分类" prop="category_id">
                            <el-select class="w-[280px]" v-model="queryParams.category_id" clearable placeholder="请选择商户分类">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.mer_category_type" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="店铺类型" prop="type_id">
                            <el-select class="w-[280px]" v-model="queryParams.type_id" clearable placeholder="请选择店铺类型">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.merchat_type" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="商户名称" prop="mer_name">
                            <el-input class="w-[280px]" v-model="queryParams.mer_name" clearable placeholder="请输入商户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="是否支持先货后款" prop="credit_buy">
                            <el-select class="w-[280px]" v-model="queryParams.credit_buy" clearable
                                placeholder="请选择是否支持先货后款">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.show_status" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="商户是否禁用" prop="status">
                            <el-select class="w-[280px]" v-model="queryParams.status" clearable placeholder="请选择商户是否禁用">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.off_status" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="提成比例" prop="commission_rate">
                            <el-input class="w-[280px]" v-model="queryParams.commission_rate" clearable
                                placeholder="请输入提成比例" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="店铺电话" prop="service_phone">
                            <el-input class="w-[280px]" v-model="queryParams.service_phone" clearable
                                placeholder="请输入店铺电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="商户余额" prop="mer_money">
                            <el-input class="w-[280px]" v-model="queryParams.mer_money" clearable placeholder="请输入商户余额" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="银行卡转账信息" prop="financial_bank">
                            <el-input class="w-[280px]" v-model="queryParams.financial_bank" clearable
                                placeholder="请输入银行卡转账信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="微信转账信息" prop="financial_wechat">
                            <el-input class="w-[280px]" v-model="queryParams.financial_wechat" clearable
                                placeholder="请输入微信转账信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="支付宝转账信息" prop="financial_alipay">
                            <el-input class="w-[280px]" v-model="queryParams.financial_alipay" clearable
                                placeholder="请输入支付宝转账信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" @click="resetPage">查询</el-button>
                            <el-button @click="resetParams">重置</el-button>
                        </el-form-item>

                    </el-col>
                </el-row>


            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['merchat.merchant/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button v-perms="['merchat.merchant/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="商户id" prop="mer_id" show-overflow-tooltip />
                    <el-table-column label="商户分类" prop="category_id">
                        <template #default="{ row }">
                            <dict-value :options="dictData.mer_category_type" :value="row.category_id" />
                        </template>
                    </el-table-column>
                    <el-table-column label="店铺类型" prop="type_id">
                        <template #default="{ row }">
                            <dict-value :options="dictData.merchat_type" :value="row.type_id" />
                        </template>
                    </el-table-column>
                    <el-table-column label="商户名称" prop="mer_name" show-overflow-tooltip />
                    <el-table-column label="是否支持先货后款" prop="credit_buy">
                        <template #default="{ row }">
                            <dict-value :options="dictData.show_status" :value="row.credit_buy" />
                        </template>
                    </el-table-column>
                    <el-table-column label="结算周期(天)" prop="settle_cycle" show-overflow-tooltip />
                    <el-table-column label="利率" prop="interest_rate" show-overflow-tooltip />
                    <el-table-column label="城市" prop="city_id" show-overflow-tooltip />
                    <el-table-column label="区县" prop="area_id" show-overflow-tooltip />
                    <el-table-column label="镇街表" prop="street_id" show-overflow-tooltip />
                    <el-table-column label="村主键" prop="village_id" show-overflow-tooltip />
                    <el-table-column label="商户地址" prop="mer_address" show-overflow-tooltip />
                    <el-table-column label="商户头像" prop="mer_avatar" show-overflow-tooltip />
                    <el-table-column label="商户备注" prop="mark" show-overflow-tooltip />
                    <el-table-column label="提成比例" prop="commission_rate" show-overflow-tooltip />
                    <el-table-column label="店铺电话" prop="service_phone" show-overflow-tooltip />
                    <el-table-column label="商户余额" prop="mer_money" show-overflow-tooltip />
                    <el-table-column label="银行卡转账信息" prop="financial_bank" show-overflow-tooltip />
                    <el-table-column label="微信转账信息" prop="financial_wechat" show-overflow-tooltip />
                    <el-table-column label="支付宝转账信息" prop="financial_alipay" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['merchat.merchant/edit']" type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-perms="['merchat.merchant/delete']" type="danger" link
                                @click="handleDelete(row.mer_id)">
                                删除
                            </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>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="merchantLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiMerchantLists, apiMerchantDelete } from '@/api/merchant'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    category_id: '',
    type_id: '',
    mer_name: '',
    credit_buy: '',
    status: '',
    commission_rate: '',
    service_phone: '',
    mer_money: '',
    financial_bank: '',
    financial_wechat: '',
    financial_alipay: ''
})

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

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

// 获取字典数据
const { dictData } = useDictData('mer_category_type,merchat_type,show_status,off_status')

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

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

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

getLists()
</script>

