<template>
    <div>
        <!-- <div>
            <el-radio-group v-model="queryParams.is_adopt" size="small" @change="changeType">
                <el-radio-button label="全部" value="" />
                <el-radio-button label="未采纳" value="0" />
                <el-radio-button label="已采纳" value="1" />
            </el-radio-group>
        </div> -->
        <el-table :data="pager.lists" border style="width: 100%; margin-top: 10px">
            <el-table-column prop="id" label="ID" width="60" />
            <el-table-column label="商品图片" prop="image" width="120">
                <template #default="{ row }">
                    <el-image
                        style="width: 50px; height: 50px"
                        :src="row.image"
                        :preview-teleported="true"
                    />
                </template>
            </el-table-column>
            <el-table-column label="商品名称" prop="store_name" show-overflow-tooltip width="120" />
            <el-table-column label="单位" prop="unit_name" width="80" show-overflow-tooltip />

            <el-table-column label="需求数量" prop="need_num" show-overflow-tooltip width="100" />
            <el-table-column
                label="供应商"
                prop="supplier_name"
                show-overflow-tooltip
                width="100"
            />
            <el-table-column label="采购数量" prop="buyer_nums" show-overflow-tooltip width="100" />
            <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>{{ row.is_buyer_name }}</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="采购人员" prop="buyer_name" show-overflow-tooltip width="100">
                <template #default="{ row }">
                    <span
                        v-if="row.buyer_confirm == 0"
                        @click="handleBuyer(1, row)"
                        style="color: #67c23a; cursor: pointer"
                        >{{ row.buyer_name }}</span
                    >
                    <span v-else>
                        {{ row.buyer_name }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column
                label="采购状态"
                prop="buyer_confirm_name"
                show-overflow-tooltip
                width="100"
            />
            <el-table-column
                label="是否入库"
                prop="is_storage_name"
                show-overflow-tooltip
                width="100"
            />
            <el-table-column label="操作" fixed="right">
                <template #default="{ row }">
                    <el-button type="primary" link @click="handleWarehouse(row)"> 入库 </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="flex mt-4 justify-end">
            <pagination v-if="pager.lists" v-model="pager" @change="getLists" />
        </div>
        <el-dialog v-model="dialogBuyer" title="设置采购" width="600">
            <el-form ref="formRef" :model="updateInfo" label-width="90px">
                <el-form-item label="采购人员">
                    <el-select
                        v-model="updateInfo.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="buyer_nums">
                    <el-input
                        v-model="updateInfo.buyer_nums"
                        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>
        <warehouse-edit-popup
            v-if="dialogWarehousing"
            ref="warehouseRef"
            @success="getLists"
            @close="dialogWarehousing = false"
        />
    </div>
</template>

<script lang="ts" setup name="subOrder">
import { usePaging } from '@/hooks/usePaging'
import {
    apiPurchaseProductOfferLists,
    apiPurchaseProductOfferEdit
} from '@/api/purchase_product_offer'
import { useRoute } from 'vue-router'
import { apiDeliveryServiceLists } from '@/api/delivery_service'
import warehouseEditPopup from './warehouseEdit.vue'
const warehouseRef = shallowRef<InstanceType<typeof warehouseEditPopup>>()

const route = useRoute()

const queryParams = reactive({
    order_id: route.query.id,
    is_adopt: '',
    is_mer: 1
})
// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiPurchaseProductOfferLists,
    params: queryParams
})
defineExpose({
    getLists
})

const changeType = (e: any) => {
    queryParams.is_adopt = e
    getLists()
}

const dialogWarehousing = ref(false)
const dialogBuyer = ref(false)
const updateInfo = ref({
    id: '',
    type: '',
    is_buyer: '',
    buyer_id: '',
    buyer_nums: ''
})

const handleWarehouse = async (row) => {
    console.log(row)
    dialogWarehousing.value = true
    await nextTick()
    warehouseRef.value?.open('edit')
    row.nums = row.buyer_nums
    row.purchase = row.price
    warehouseRef.value?.setFormData(row)
}
const goodsOfferUpdate = () => {
    apiPurchaseProductOfferEdit(updateInfo.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) {
        updateInfo.value.id = row.id
        updateInfo.value.buyer_nums = row.need_num
        updateInfo.value.is_buyer = '1'
        updateInfo.value.type = 'buyer'
        DeliveryService()
        dialogBuyer.value = true
    } else {
        apiPurchaseProductOfferEdit({
            id: row.id,
            type: 'buyer',
            is_buyer: -1
        }).then((res) => {
            getLists()
        })
    }
}
</script>
