<!-- 我的仓储-我的库存 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :loading="loading" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <template #header>
                <app-auth-operation :menus="['warehousing_goods_add', 'warehousing_goods_sell', 'warehousing_goods_import']"
                    @closed="onRefresh" />
            </template>
            <!-- 价格 -->
            <template #price="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.price) }}
            </template>
            <!-- 克拉单价 -->
            <template #priceper="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.priceper) }}
            </template>
            <!-- 颜色 -->
            <template #zscolortype="{ row }">
                {{ [row.zscolortype1display, row.zscolortype2display].join('-') }}
            </template>
            <!-- 净度 -->
            <template #zsclaritytype="{ row }">
                {{ [row.zsclaritytype1display, row.zsclaritytype2display].join('-') }}
            </template>
            <!-- 是否下架 -->
            <template #marketflag="{ value }">
                {{ getGoodsStatusName(value) }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-auth-operation type="dropdown" :menus="handleOperateButtons(row)" :options="{ selectedRow: row }"
                    @closed="onRefresh" />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { formatDecimal } from '@/filters'
import { useDataFilter } from '@/hooks/datatable'
import { useDiamond } from '@/business/goods'
import { Category } from '@/constants/diamond'
import { useComposeTable } from '@pc/components/base/table'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'

const { loading, dataList, total, pageIndex, pageSize, warehouseList, categoryList, getDiamondList } = useDiamond()
const { tableColumns } = useComposeTable<Ermcp.MyWRPositionRsp>({ rowKey: 'goodsno', columnKey: 'warehousing_diamond' })
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.MyWRPositionReq>()

const handleOperateButtons = (row: Ermcp.MyWRPositionRsp) => {
    const buttons = ['warehousing_goods_details']
    if (row.marketflag === 1) {
        buttons.push('warehousing_goods_pull')
    }
    if (row.marketflag === 2) {
        buttons.push('warehousing_goods_put', 'warehousing_goods_delete')
    }
    return buttons
}

const onSearch = (clear = false) => {
    getQueryParams((qs) => {
        pageIndex.value = 1
        getDiamondList(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => {
        getDiamondList(qs).catch((err) => ElMessage.error(err))
    })
}

// 获取商品状态
const getGoodsStatusName = (status: number) => {
    switch (status) {
        case 1:
            return '已上架'
        case 2:
            return '未上架'
        case 3:
            return '履约中'
    }
    return '--'
}

filterOptons.selectList = [
    {
        label: '商品分类',
        key: 'zscategorys',
        selectedValue: Category.Diamond,
        locked: true,
        options: categoryList,
    },
    {
        label: '仓库',
        key: 'warehouseid',
        options: warehouseList,
    },
]

filterOptons.inputList = [
    { label: '商品', keys: ['zsallproperties'] },
]

filterOptons.buttonList = [
    { lable: '重置', onClick: () => onSearch(true) },
    { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() }
]

onRefresh()
</script>