<template>
    <div>
        <div class="pb-16 d-flex j-sb">
            <div>
                <div>
                    <searchTabs v-model="queryParams.auditStatus" @change="handleQuery" :list="tabs" key-label="name"
                        key-value="type" key-count="num"></searchTabs>
                </div>
            </div>
            <div>
                <el-button @click="allPass" type="primary" :disabled="nodataHandle">全部通过</el-button>
                <el-button @click="allNoPass" type="danger" :disabled="nodataHandle">全部不通过</el-button>
            </div>
        </div>
        <vxe-table :loading="loading" border :data="list">
            <!-- 序号 -->
            <vxe-column type="seq" width="60" title="序号" align="center" />
            <!-- 品名 -->
            <vxe-column title="品名" field="varietyName" min-width="100" :formatter="colNoData" />
            <!-- 规格 -->
            <vxe-column title="规格" field="specification" min-width="100" :formatter="colNoData" />
            <!-- 产地 -->
            <vxe-column title="产地价格（元/kg）" field="price" min-width="100" />
            <vxe-column title="对比近7天价格" min-width="100">
                <template #default="{ row }">{{ row.growthRate }}%</template>
            </vxe-column>
            <vxe-column title="积分" field="points" width="60" :formatter="colNoData" />
            <vxe-column title="上报人" field="createByName" width="100" :formatter="colNoData" />
            <vxe-column title="上报时间" field="createTime" width="160" :formatter="colNoData" />
            <vxe-column title="审核状态" fixed="right" width="110">
                <template #default="{ row }">
                    <div :class="['c-res-' + row?.auditStatus]">{{ selectDictLabel(check_status, row?.auditStatus) }}</div>
                </template>
            </vxe-column>
            <!-- 操作 -->
            <vxe-column title="操作" align="center" fixed="right" width="180" v-if="queryParams.auditStatus== '0'">
                <template #default="{ row }">
                    <template v-if="row.auditStatus === '0'">
                        <el-button @click.stop="passRow(row)" text type="primary">通过</el-button>
                        <el-button @click.stop="noPassRow(row)" text type="danger">不通过</el-button>
                    </template>
                </template>
            </vxe-column>
        </vxe-table>
    </div>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
</template>
<script setup name="priceRepordCheck" lang="ts">
import { colNoData } from '@/utils/noData';
import { listVipPayment } from '@/api/dgtmedicine/vipPayment/index';
import { approvalPrice, originPriceDetail, priceDetailCount } from '@/api/price/report';
import { searchTabs } from '@/views/models';
const { query }: any = useRoute()
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { check_status } = toRefs<any>(proxy?.useDict('check_status'));
// 字典
const list = ref<any[]>([]);
const loading = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const data = reactive<any>({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditStatus: '0'
    }
});
const { queryParams } = toRefs(data);
/** 点击行上传发票 */

/** 查询会员信息列表 */
const getList = async () => {
    loading.value = true;
    const res = await originPriceDetail({ ...queryParams.value, originPriceId: query?.id, cpyid: query?.cpyid });
    list.value = res.rows;
    total.value = res.total;
    loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = (level?: any) => {
    queryParams.value.pageNum = 1;
    getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};
// tabs count获取
const tabs = ref<any[]>([]);
const nodataHandle = computed(() => {
    return tabs.value.filter(o => o.type === '0').map(o => o.num).reduce((a, b) => a + b, 0) == 0;
});
const getTabs = async () => {
    const res = await priceDetailCount({ originPriceId: query?.id, cpyid: query?.cpyid });
    if (!res || res.code !== 200) return;
    tabs.value = res.data
};
const allPass = async () => {
    const confirm = await proxy?.$modal.confirm('是否确认将本次上报品种的产地价格审核为通过？');
    if (confirm === 'confirm') {
        const params = {
            priceId: query?.id,
            auditStatus: '1',
        }
        auditSubmit(params);
    }
};
const allNoPass = async () => {
    ElMessageBox.prompt('<div>是否确认将本次上报的全部品种产地价格审核为不通过！</div><div>不通理由：</div>', '审核不通过', {
        confirmButtonText: '确认不通过',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        inputValidator: (value: any) => {
            if (!value) {
                return '请填写不通过理由';
            }
        },
        inputPlaceholder: '请填写不通过理由...',
        inputErrorMessage: '请填写不通过理由...',
    })
        .then(({ value }) => {
            console.log('确认', value);
            const params = {
                priceId: query?.id,
                auditStatus: '2',
                msg: value
            }
            auditSubmit(params);
        })
        .catch(() => {
            console.log('取消');

        })
};
const passRow = async (row: any) => {
    const confirm = await proxy?.$modal.confirm(`是否确认将本次上报品种${row?.varietyName}的产地价格审核为通过？`);
    if (confirm === 'confirm') {
        console.log('confirm');
        const params = {
            targetIds: [row.id],
            auditStatus: '1',
        }
        auditSubmit(params);
    }
};
const noPassRow = async (row: any) => {
    ElMessageBox.prompt(`<div>是否确认将品种${row?.varietyName}的产地价格审核为不通过！</div><div>不通理由：</div>`, '审核不通过', {
        confirmButtonText: '确认不通过',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        inputValidator: (value: any) => {
            if (!value) {
                return '请填写不通过理由';
            }
        },
        inputPlaceholder: '请填写不通过理由...',
        inputErrorMessage: '请填写不通过理由...',
    })
        .then(({ value }) => {
            const params = {
                targetIds: [row.id],
                auditStatus: '2',
                msg: value
            }
            auditSubmit(params);
        })
        .catch(() => {
            console.log('取消');
        })
};
// 审核提交
const auditSubmit = async (params) => {
    proxy?.$modal.loading('审核中...');
    const res = await approvalPrice(params).finally(() => {
        proxy?.$modal.closeLoading();
    });
    if (!res || res.code !== 200) return;
    proxy?.$modal.msgSuccess('审核成功');
    getList();
    getTabs();
};

onMounted(() => {
    getTabs();
    getList();
});
</script>
