<template>
    <el-dialog title="详情" width="80%" :visible.sync="dialogVisible" @close="dialogVisible = false">
        <div class="head-container">
            <span>【{{ row.type }}】</span>
        </div>
        <div class="head-container">
            <el-table :data="tableData.detail.stockSnap" v-loading="tableData.loading" height="400px">
                <el-table-column :label="`商品名称`" prop="name"></el-table-column>
                <el-table-column label="规格" prop="specSnap"></el-table-column>
                <el-table-column label="原库存" prop="stockNumber"></el-table-column>
                <el-table-column label="变动数量" prop="number">
                    <template v-slot="scope">
                        <span v-if="tableData.detail.subType == -1">-</span> {{ scope.row.number }} {{
                            scope.row.unitName
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="现有库存" prop="number">
                    <template v-slot="scope">
                        {{ returnNowHasNumbr(scope.row) }}
                        <!-- {{ scope.row.stockNumber*1 + scope.row.number*1   }} {{ scope.row.unitName }} -->
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="head-container">
            <div class="row">备注：{{ tableData.detail.remark }}</div>
            <div class="row">操作人：{{ tableData.detail.operatorSnap.name }}</div>
            <div class="row">创建时间：{{ dayjs(tableData.detail.createdAt).format('YYYY-MM-DD HH:mm:ss') }}</div>
        </div>
    </el-dialog>
</template>

<script>
import dayjs from 'dayjs'
import { formatDecimal } from '@/utils/index.js'
import { tbProductStockOperateDetail } from '@/api/invoicing'
export default {
    data() {
        return {
            dayjs,
            row: '',
            dialogVisible: false,
            tableData: {
                loading: false,
                detail: {
                    stockSnap: [],
                    operatorSnap: {
                        name: ''
                    }
                }
            }
        }
    },
    methods: {
        //返回现有库存数量
        returnNowHasNumbr(row) {
            const subType = this.tableData.detail.subType || 1
            if (!row.hasOwnProperty('number')) {
                return ''
            }
            return formatDecimal(parseFloat(row.stockNumber * 1 + row.number * subType + row.unitName), 2, true)
        },
        show(row) {
            this.dialogVisible = true
            this.row = row
            this.getTableData(row.id)
        },
        async getTableData(id) {
            this.tableData.loading = true
            try {
                const res = await tbProductStockOperateDetail(id)
                this.tableData.loading = false
                this.tableData.detail = res

            } catch (error) {
                console.log(error);
            }
        }
    }
}
</script>

<style scoped lang="scss">
.row {
    padding-bottom: 10px;
}
</style>