<template>
    <div class="p-3">
        <div class="bg-fff flex1 ov-hd d-flex flex-cln">
            <div class="pd-16 border-bottom">
                <div class="f-s-20 c-333 f-w-7 mb-10">原料求购</div>
                <div class="d-flex j-ed">
                    <div class="d-flex pl-20">
                        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="auto">
                            <el-form-item label="原料类型" prop="type">
                                <el-select style="width: 180px;" @change="resetQuery" v-model="queryParams.type" placeholder="搜原料类型" clearable>
                                    <el-option v-for="item in dm_medicine_find_type" :key="item.value" :label="item.label" :value="item.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="原料名称" prop="name">
                                <el-input style="width: 180px;" v-model="queryParams.name" placeholder="搜原料名称" clearable />
                            </el-form-item>
                            <el-form-item label="联系电话" prop="tel">
                                <el-input style="width: 180px;" v-model="queryParams.tel" placeholder="搜联系电话" clearable />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                                <el-button @click="exportList">导出</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
            </div>
            <div class="flex1 ov-hd pd-16 d-flex flex-cln">
                <div class="flex1 ov-hd">
                    <vxe-table ref="tableRef" :loading="loading" border :data="list" min-height="0" max-height="auto" :column-config="{ resizable: true }" :row-config="{ keyField: 'id',  isHover: true }">
                        <!-- 序号 -->
                        <vxe-column type="seq" width="60" title="序号" align="center" />
                        <vxe-column title="原料类型" width="100">
                            <template #default="{ row }">
                                {{ selectDictLabel(dm_medicine_find_type, row.type) }}
                            </template>
                        </vxe-column>
                        <vxe-column title="原料名称" field="name" min-width="100" :formatter="colNoData" />

                        <vxe-column title="需求量" width="100">
                            <template #default="{ row }">{{ row?.amount }}{{ row?.unit }}</template>
                        </vxe-column>
                        <vxe-column title="具体厚度/直径" field="spec" width="120" :formatter="colNoData" />
                        <vxe-column title="具体需求" show-overflow field="needsDetail" min-width="100" :formatter="colNoData" />
                        <vxe-column title="所在城市" field="adcdName" min-width="100" :formatter="colNoData" />
                        <vxe-column title="联系电话" field="tel" width="120" :formatter="colNoData" />
                        <vxe-column title="发布时间" field="createTime" min-width="100" :formatter="colNoData" />
                        <vxe-column title="供应信息" width="140">
                            <template #default="{ row }">
                                <div class="d-flex j-c a-c">
                                    <el-button @click="clickLookSupply(row)" type="primary" v-if="row?.resCount" text>
                                        <span>{{ row?.resCount }}条</span>
                                    </el-button>
                                    <span v-else>0条</span>
                                </div>
                            </template>
                        </vxe-column>
                        <vxe-column title="操作" align="center" fixed="right" width="130">
                            <template #default="{ row }">
                                <el-button @click="clickAddSupply(row)" type="primary" text>填写供应信息</el-button>
                            </template>
                        </vxe-column>
                    </vxe-table>
                </div>
            </div>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
            <div class="pd-5"></div>
        </div>
    </div>
    <CreateSupplyInfo @success="handleQuery" v-if="showSupplyRow" v-model:show="showSupplyRow" :info="rowInfo" :dict="{ dm_medicine_find_type, yes_no, solon_sample_free, solon_report_type }"></CreateSupplyInfo>
    <ViewsSupplyInfo v-if="showSupplyInfo" v-model:show="showSupplyInfo" :info="rowInfo" :dict="{ dm_medicine_find_type, yes_no, solon_sample_free, solon_report_type }"></ViewsSupplyInfo>
</template>
<script setup lang="ts" name="raw-materials-index">
import { colNoData } from '@/utils/noData';
import NP from 'number-precision';
import { priceReportExport } from '@/api/price/report';
import { httpRequests } from '@/utils/httpRequests';
import { CreateSupplyInfo, ViewsSupplyInfo } from '../models';
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { dm_medicine_find_type, yes_no, solon_sample_free, solon_report_type } = toRefs<any>(proxy?.useDict('dm_medicine_find_type', 'yes_no', 'solon_sample_free', 'solon_report_type'));
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    auditStatus: '',
    startDate: '',
    endDate: '',
    dateRange: []
});
const showSupplyRow = ref(false); // 显示填写供应信息弹窗
const showSupplyInfo = ref(false); // 显示填写供应信息弹窗
const loading = ref(false);
const total = ref(0);
const list = ref<any>([]);
const getList = async () => {
    loading.value = true;
    const res: any = await httpRequests.get('/solon/medicine-identify/findPageListNoLogin', queryParams.value);
    if (!res || res.code !== 200) return;
    list.value = res.rows;
    total.value = res.total;
    loading.value = false;
};
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};
const queryFormRef = ref<any>();
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    handleQuery();
};
const rowInfo = ref<any>(null);
const clickAddSupply = (row: any) => {
    rowInfo.value = { ...row };
    showSupplyRow.value = true;
};
const clickLookSupply = (row: any) => {
    rowInfo.value = { ...row };
    showSupplyInfo.value = true;
};
const tableRef = ref<any>();
const exportList = async () => {
    // 获取表格选中信息
    const selected = tableRef.value?.getCheckboxReserveRecords(true).concat(tableRef.value?.getCheckboxRecords());
    console.log(selected);
    const ids = selected.map((item: any) => item.id);
    const res = await priceReportExport({ ids });
    if (!res || res.code !== 200) return;
};
onMounted(() => {
    handleQuery();
});
</script>
<style lang="scss"></style>