<!-- 查询管理-仓单查询-仓单汇总查询 -->
<template>
    <app-view>
        <template #header>
            <el-form class="el-form--filter" :model="queryParams">
                <el-form-item :label="t('query.warehouse_receipt.wrsumm.isHis')" prop="ishis">
                    <el-select v-model="queryParams.ishis" @change="onTypeChange">
                        <el-option :label="t('common.current')" :value="0" />
                        <el-option :label="t('common.history')" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.warehouse_receipt.wrsumm.date')" prop="date" v-if="queryParams.ishis === 1">
                    <el-date-picker type="daterange" v-model="dateValue" value-format="YYYY-MM-DD"
                    :start-placeholder="t('common.start')" :end-placeholder="t('common.end')" @change="onDateChange" />
                </el-form-item>
                <el-form-item :label="t('query.warehouse_receipt.wrsumm.wrsids')" prop="wrsids" v-if="warehouse">
                    <el-select v-model="wrsids" multiple collapse-tags>
                        <template v-for="item in warehouse.wrStandardList" :key="item.wrstandardid">
                            <el-option :label="item.wrstandardname" :value="item.wrstandardid" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.warehouse_receipt.wrsumm.wareids')" prop="wareids" v-if="warehouse">
                    <el-select v-model="wareids" multiple collapse-tags>
                        <template v-for="item in warehouse.warehouseInfoList" :key="item.autoid">
                            <el-option :label="item.warehousename" :value="item.autoid" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch(false)">{{ t('operation.search') }}</el-button>
                    <el-button type="primary" @click="onSearch(true)">{{ t('operation.reset') }}</el-button>
                </el-form-item>
            </el-form>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons()"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { initWarehouse, summaryQuery } from '@/services/api/order'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n
const dateValue = ref<string[] | null>([])
const wrsids = ref<number[]>([])
const wareids = ref<number[]>([])

const queryParams = ref<Partial<Model.SummaryQueryReq>>({
    ishis: 0
})

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.SummaryQueryRsp>({
    onClose: () => onSearch()
})

const { data: warehouse } = useRequest(initWarehouse, {
    onError: (err) => {
        ElMessage.error(err)
    }
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(summaryQuery, {
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'wrstandardname', label: 'query.warehouse_receipt.wrsumm.wrstandardname' },
    { field: 'warehousename', label: 'query.warehouse_receipt.wrsumm.warehousename' },
    { field: 'totalqty', label: 'query.warehouse_receipt.wrsumm.totalqty' },
    { field: 'avaliableqty', label: 'query.warehouse_receipt.wrsumm.avaliableqty' },
    { field: 'freezeqty', label: 'query.warehouse_receipt.wrsumm.freezeqty' },
    { field: 'increaseqty', label: 'query.warehouse_receipt.wrsumm.increaseqty' },
    { field: 'decreaseqty', label: 'query.warehouse_receipt.wrsumm.decreaseqty' },
    { field: 'enumdicname', label: 'query.warehouse_receipt.wrsumm.enumdicname' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

// 选择查询类型
const onTypeChange = (value: number) => {
    if (!value) {
        dateValue.value = []
        onDateChange()
    }
}

// 选择日期
const onDateChange = () => {
    const [startDate, endDate] = dateValue.value || []
    queryParams.value.hisstartdate = startDate
    queryParams.value.hisenddate = endDate
}

const onSearch = (clear = false) => {
    if (clear) {
        queryParams.value = {
            ishis: 0,
        }
        dateValue.value = []
        wareids.value = []
        wrsids.value = []
    } else {
        queryParams.value.wareids = wareids.value.join(',')
        queryParams.value.wrsids = wrsids.value.join(',')
    }
    run(queryParams.value)
}
</script>