<template>
    <div>
        <el-button type="primary" @click="beforehand_order_cart_info">导出表格 </el-button>
        <el-radio-group v-model="queryParams.top_cate_value" style="margin-left: 30px">
            <el-radio
                :value="0"
                label="全部"
                @change="topCateChange({ name: '全部', id: 0 })"
            ></el-radio>
            <el-radio
                :value="item.id"
                :label="item.name"
                v-for="item in formData.top_cate"
                :key="item.id"
                @change="topCateChange(item)"
            ></el-radio>
        </el-radio-group>
        <div>
            <el-table :data="pager.lists" v-loading="pager.loading">
                <el-table-column label="id" prop="id" show-overflow-tooltip />
                <el-table-column label="商品信息" prop="store_name" show-overflow-tooltip>
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-image
                                :src="row.image"
                                class="w-16 h-16 mr-2"
                                :preview-teleported="true"
                            ></el-image>
                            <div>{{ row.store_name }}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="单位" prop="unit_name" show-overflow-tooltip />
                <el-table-column label="仓库库存" prop="warehouse_stock" show-overflow-tooltip />
                <el-table-column label="需要数量" prop="cart_num" show-overflow-tooltip />
                <el-table-column label="零售价" prop="price" show-overflow-tooltip />
                <el-table-column label="合计" prop="total_price" show-overflow-tooltip />
                <el-table-column
                    label="是否需采购"
                    prop="is_buyer"
                    show-overflow-tooltip
                    width="100"
                >
                    <template #default="{ row }">
                        <div v-if="row.is_buyer == 0">
                            <el-button
                                type="danger"
                                round
                                size="small"
                                @click="handleBuyer(-1, row)"
                                >否</el-button
                            >
                            <el-button
                                type="success"
                                round
                                size="small"
                                @click="handleBuyer(1, row)"
                                >是</el-button
                            >
                        </div>
                        <div v-else>
                            <span v-if="row.is_buyer == 1">{{ row.status_name }}</span>
                            <span v-if="row.is_buyer == -1">无需采购</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="mark" show-overflow-tooltip />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['beforehand_order_cart_info.beforehand_order_cart_info/edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="[
                                'beforehand_order_cart_info.beforehand_order_cart_info/delete'
                            ]"
                            type="danger"
                            link
                            @click="handleDeletes(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="flex mt-4 justify-end" v-if="pager.lists.length < pager.count">
            <pagination v-model="pager" @change="getLists" />
        </div>
        <el-dialog v-model="dialogBuyer" title="设置采购" width="600">
            <el-form ref="formRef" :model="purchase_product_offer" label-width="90px">
                <el-form-item label="采购人员">
                    <el-select
                        v-model="purchase_product_offer.buyer_id"
                        placeholder="请选择采购人员"
                        size="large"
                        style="width: 240px"
                    >
                        <el-option
                            v-for="item in delivery_list"
                            :label="item.nickname"
                            :value="item.uid"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="采购数量" prop="need_num">
                    <el-input
                        v-model="purchase_product_offer.need_num"
                        clearable
                        placeholder="请输入采购数量"
                        :readonly="false"
                    />
                </el-form-item>
                <el-form-item label="备注" prop="mark">
                    <el-input
                        v-model="purchase_product_offer.mark"
                        clearable
                        placeholder="请输入备注"
                        :readonly="false"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogBuyer = false">取消</el-button>
                    <el-button type="primary" @click="goodsOfferUpdate"> 确认 </el-button>
                </div>
            </template>
        </el-dialog>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="subOrder">
import { usePaging } from '@/hooks/usePaging'
import {
    apiBeforehandOrderCartInfoLists,
    apiBeforehandOrderCartInfoDelete,
    apiBeforehandOrderCartInfoProcurementStatus
} from '@/api/beforehand_order_cart_info'
import { apiDeliveryServiceLists } from '@/api/delivery_service'
import feedback from '@/utils/feedback'
import { apiPurchaseProductOfferAdd } from '@/api/purchase_product_offer'
import EditPopup from './editProduct.vue'
import { useRoute } from 'vue-router'
const route = useRoute()

const editRef = shallowRef<InstanceType<typeof EditPopup>>()

// 是否显示编辑框
const showEdit = ref(false)
const ids = ref()
const dialogBuyer = ref(false)
// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}
// 删除
const handleDeletes = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiBeforehandOrderCartInfoDelete({ id })
    getLists()
}
const formData = ref({
    id: '',
    top_cate: []
})
const queryParams = reactive({
    bhoid: '',
    pay_type: '',
    top_cate: '',
    top_cate_value: ''
})

const purchase_product_offer = ref({
    order_id: '',
    product_id: '',
    need_num: '',
    unit: '',
    is_buyer: '',
    buyer_id: '',
    mark: ''
})
const goodsOfferUpdate = () => {
    apiPurchaseProductOfferAdd(purchase_product_offer.value).then((res) => {
        getLists()
        dialogBuyer.value = false
    })
}
const delivery_list = ref([])
const DeliveryService = () => {
    apiDeliveryServiceLists({ type: 3 }).then((res) => {
        delivery_list.value = res.lists
    })
}

/**
 * 是否需采购
 * @param e
 */
const handleBuyer = (e, row) => {
    if (e == 1) {
        purchase_product_offer.value.order_id = row.bhoid
        purchase_product_offer.value.product_id = row.product_id
        purchase_product_offer.value.unit = row.unit
        purchase_product_offer.value.need_num = row.cart_num
        purchase_product_offer.value.mark = row.mark
        purchase_product_offer.value.is_buyer = '1'
        DeliveryService()
        dialogBuyer.value = true
    } else {
        apiBeforehandOrderCartInfoProcurementStatus({
            id: row.id
        }).then(() => {
            getLists()
        })
    }
}
// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiBeforehandOrderCartInfoLists,
    params: queryParams
})
const topCateChange = (row: any) => {
    if (row.id == 0) {
        queryParams.top_cate = ''
        queryParams.top_cate_value = '全部'
        getLists()
    } else {
        queryParams.top_cate = row.id
        queryParams.top_cate_value = row.name

        getLists()
    }
}

const beforehand_order_cart_info = () => {
    apiBeforehandOrderCartInfoLists({
        export: 2,
        bhoid: formData.value.id
    }).then((res) => {
        window.open(res.url, '_blank')
        ElMessage.success('导出成功')
    })
}

function getList(data: any) {
    console.log(213213)
    formData.value = data
    queryParams.bhoid = data.id
    getLists()
}

onMounted(() => {
    queryParams.bhoid = route.query.id
    formData.value.id = route.query.id
    getLists()
})
defineExpose({
    getList
})
</script>
