<script setup lang="ts">
import { useRouter } from 'vue-router'
import { GlobalStore } from '@/stores/index'
import { View } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import TableBase from '@/components/TableBase/index.vue'
import type { ColumnProps } from '@/components/TableBase/interface/index'
import { StockIn_ListProducts,StockinExcel } from '@/api/storehouse/index'
import { depotHooks } from '@/hooks/useDepot'

const router = useRouter()
const globalStore = GlobalStore()
const TableRef = ref<InstanceType<typeof TableBase> | null>(null)

const columns: ColumnProps[] = [
    { type: 'index', label: '序号', width: 80 },
    { prop: 'T_number', label: '入库单号' },
    { prop: 'T_batch_number', label: '入库批次' },
    { prop: 'T_submit_name', label: '经办人' },
    { prop: 'T_date', label: '入库日期' },
    { prop: 'T_depot_name', label: '入库仓库' },
	{ prop: 'T_type', label: '入库类型', name: 'T_type' },


	{ prop: 'T_product_name', label: '产品名称' },
    { prop: 'T_product_model', label: '产品型号' },
    { prop: 'T_num', label: '数量' },

    { prop: 'T_product_relation_sn', label: '是否关联SN', name: 'T_product_relation_sn' },
    { prop: 'T_remark', label: '备注' },

    { prop: 'operation', label: '操作', width: 260, fixed: 'right' }
]

/**
 * 查看详情
 */
const dialogTableVisible = ref(false)
const gridData:any = ref([])
const preview = (list:any) => {
    let arr = [...list] || []
    gridData.value = arr.map((item:any)=>({sn:item}))
    dialogTableVisible.value = true
}
// 搜索
const T_date = ref<string[]>([])


const initParam = reactive({
    User_tokey: globalStore.GET_User_tokey,
    T_end_date: '',
    T_start_date: '',
    T_depot_id: '',
    T_product_name: '',
    T_product_model: '',
    T_name: '',
})
const searchHandle = () => {
    initParam.T_end_date = T_date.value ? T_date.value[1] : ''
    initParam.T_start_date = T_date.value ? T_date.value[0] : ''
    TableRef.value?.searchTable()
}
// 拿到仓库列表
const { options } = depotHooks()


/**
 * Excel
 */
 const onclickExcel = async ()=>{
    const result:any = await StockinExcel(initParam)
    console.log('导出',result)

    if (result.Code === 200)window.open(result.Data)
}
</script>

<template>
    <div class="inStorageDetails">
        <TableBase ref="TableRef" :columns="columns" :requestApi="StockIn_ListProducts"
            :initParam="initParam">
            <template #table-header>
                <div class="input-suffix">
                    <el-row :gutter="20" style="margin-bottom: 0">
                        <el-col :xl="4" :lg="4" :md="4" style="display: flex">
                            <span class="inline-flex items-center">关键字：</span>
                            <el-input v-model="initParam.T_name" placeholder="入库单号，备注搜索" />
                        </el-col>
                        <el-col :xl="4" :lg="4" :md="4" style="display: flex">
                            <span class="inline-flex items-center">入库日期：</span>
                            <el-date-picker v-model="T_date" type="daterange" range-separator="~"
                                start-placeholder="开始时间" end-placeholder="结束时间" format="YYYY-MM-DD"
                                value-format="YYYY-MM-DD" />
                        </el-col>
                        <el-col :xl="4" :lg="4" :md="4" style="display: flex">
                            <span class="inline-flex items-center">产品名称：</span>
                            <el-input v-model="initParam.T_product_name" placeholder="产品名称" />
                        </el-col>
                        <el-col :xl="4" :lg="4" :md="4" style="display: flex">
                            <span class="inline-flex items-center">产品型号：</span>
                            <el-input v-model="initParam.T_product_model" placeholder="产品型号" />
                        </el-col>
                        <el-col :xl="4" :lg="4" :md="4" style="display: flex">
                            <span class="inline-flex items-center">仓库：</span>
                            <el-select v-model="initParam.T_depot_id" clearable placeholder="请选择仓库~">
                                <el-option v-for="item in options" :key="item.Id" :label="item.T_name"
                                    :value="item.Id" />
                            </el-select>
                            <el-button type="primary" @click="searchHandle">搜索</el-button>
                        </el-col>
                        <el-col :xl="4" :lg="4" :md="4" style="display: flex">
                            <el-button type="primary" icon="Download" @click="onclickExcel">导出</el-button>
                        </el-col>
                    </el-row>
                    
                </div>
            </template>
			<template #T_type="{ row }">
				<el-text class="mx-1" :type="row.T_type==1?'success':'primary'">
					{{row.T_type==1?'入库':'退库'}}
				</el-text>
			</template>
            <template #T_product_relation_sn="{ row }">
                <el-text class="mx-1" :type="row.T_product_relation_sn==1?'primary':'danger'">
                    {{row.T_product_relation_sn==1?'是':'否'}}
                </el-text>
            </template>
            <template #right="{ row }">
                <el-button type="primary" size="small" v-show="row.T_product_relation_sn == 1" :icon="View"
                    @click="preview(row.T_device_list)">查看SN</el-button>
            </template>
        </TableBase>
        <el-dialog v-model="dialogTableVisible" title="查看SN" draggable>
            <el-table :data="gridData" border max-height="500">
                <el-table-column type="index" label="序号" width="100" />
                <el-table-column property="sn" label="SN" />
            </el-table>
        </el-dialog>
    </div>
</template>

<style scoped lang="scss">
@import '@/styles/var.scss';

.inStorageDetails {
    @include f-direction;

    .input-suffix {
        width: 100%;

        .inline-flex {
            white-space: nowrap;
        }

        .w-50 {
            width: 12.5rem;
        }

        .btn {
            display: flex;
            justify-content: end;

            .el-button {
                padding: 0 20px;
            }
        }
    }
}
</style>
