<!-- 入库单列表 -->
<template>
    <div class="table-box">
        <!-- 按钮 -->
        <ListBtns :selectionList="selectionList" @handleParentCallback="handleParentCallback" :ruleForm="datas.exportList" />
        <Search
            :formData="datas.formData"
            :ruleForm="datas.ruleForm"
            @handleSearch="handleSearch"
            @handleReset="handleReset"
            @handleRemoteMehod="handleRemoteMehod"
        />
        <!-- 表格 -->
        <ProTable
            ref="proTable"
            :columns="datas.columns"
            :request-api="getInStockListApi"
            :init-param="datas.initParam"
            :ruleForm="datas.ruleForm"
            @selectionChange="selectionChange"
        >
            <!-- 点击跳转到详情去 -->
            <template #billNo="scope">
                <a @click="handleToDetail(scope.row)" class="break-word to-details">
                    {{ scope.row.billNo }}
                </a>
            </template>
            <!-- successSync -->
            <template #successSync="scope">
                <a @click="handleSuccessSync(scope.row)" :class="scope.row.successSync == '失败' ? 'break-word to-detail1' : ''">
                    {{ scope.row.successSync }}
                </a>
            </template>
        </ProTable>
    </div>
</template>

<script setup lang="ts" name="warehouseReceiptList">
import ListBtns from "@/components/ListBtns/index.vue";
import Search from "@/components/Search/index.vue";
import ProTable from "@/components/ProTable/index.vue";

import { cloneDeep } from "lodash-es";
import { RULEFORM, FORMDATA, COLUMNS } from "./constant/list/index";
import { getInStockListApi } from "@/api/modules/receiptAndWarehousing";
import { useRouter } from "vue-router";
import { initSearch } from "./init/index";
import { useSearchInfoArray, useSetSearchData } from "@/hooks/useSearch";
import { ElMessageBox } from "element-plus";
const proTable = ref<any>(null);
// 数据源
const datas = reactive({
    exportList: cloneDeep(RULEFORM),
    columns: COLUMNS, //列表配置项
    initParam: cloneDeep(RULEFORM), // 初始化搜索条件|重置搜索条件
    ruleForm: cloneDeep(RULEFORM), // 搜索条件
    formData: FORMDATA //搜索配置项
});
//选中表格的行
let selectionList = ref([]);
initSearch(datas);
//路由
const $router = useRouter();
/**
 *
 * @description 获取按钮操作结果
 * @param type {string} 按钮类型
 * @param result {Object} 操作结果
 */
//result?: Record<string, any>
//按钮回调
const handleParentCallback = () => {
    datas.ruleForm = cloneDeep(RULEFORM);
    proTable!.value.getTableList(datas.initParam);
};
//搜索
const handleSearch = () => {
    proTable!.value.initPage(datas.ruleForm);
    datas.exportList = datas.ruleForm;
    datas.exportList.pageNo = proTable!.value.pageable.pageNo;
    datas.exportList.pageSize = proTable!.value.pageable.pageSize;
};
//搜索置空
const handleReset = () => {
    datas.ruleForm = cloneDeep(RULEFORM);
    datas.exportList = cloneDeep(RULEFORM);
    proTable!.value.initPage(datas.initParam);
};
//表格复选框回调
const selectionChange = (selection: any) => {
    selectionList.value = selection;
};
const handleSuccessSync = (row: any) => {
    if (row.successSync === "失败") {
        ElMessageBox.alert(row.remark, "失败原因", {
            confirmButtonText: "确认",
            callback: (action: any) => {
                console.log(action);
            }
        });
    }
};

//跳转到详情
const handleToDetail = (row: any) => {
    $router.push({
        path: "/receiptAndWarehousing/warehouseReceiptList/details",
        query: {
            id: row.id
        }
    });
};
const handleRemoteMehod = (params: any) => {
    const { data, prop } = params;
    if (prop === "supplierId") {
        datas.formData = useSetSearchData(datas.formData, {
            supplierId: useSearchInfoArray(data)
        });
    }
    if (prop === "orgId") {
        datas.formData = useSetSearchData(datas.formData, {
            orgId: useSearchInfoArray(data)
        });
    }
};
</script>

<style scoped>
.to-details {
    color: #4178d5;
    cursor: pointer;
}
.to-detail1 {
    color: red;
    cursor: pointer;
}
</style>
