<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="订单编号" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="商品ID" prop="goodsId">
                <el-input v-model="queryParams.goodsId" placeholder="请输入商品ID" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="是否退款" prop="isRefund">
                <el-input v-model="queryParams.isRefund" placeholder="请输入是否退款" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--           -->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['goods:record:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" fit highlight-current-row border stripe :data="recordList">
            <el-table-column label="订单编号" align="center" prop="orderNo" width="220px" />
            <el-table-column label="商品名称" align="center" prop="goodsName" width="250px" />
            <el-table-column label="商品类型" align="center" prop="goodsType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.goods_type" :value="scope.row.goodsType" />
                </template>
            </el-table-column>
            <el-table-column label="物料名称" align="center" prop="goodsSkuName" width="250px">
                <template slot-scope="scope">
                    <div style="text-align: center">{{ scope.row.goodsSkuName.replace(' ', '/') }}</div>
                </template>
            </el-table-column>
            <el-table-column label="是否增加" align="center" prop="isAdd" width="100px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isAdd" />
                </template>
            </el-table-column>
            <el-table-column label="数量" align="center" prop="num" width="80px" />
            <el-table-column label="是否退款" align="center" prop="isRefund" width="100px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isRefund" />
                </template>
            </el-table-column>
            <el-table-column label="买方" align="center" width="200px" v-if="userInfoVO.userType != '02'">
                <template slot-scope="scope">
                    <div v-if="scope.row.buySourceType == '00'">总部</div>
                    <div v-if="scope.row.buySourceType != '00'">{{ getOrgNameBuy(scope.row) }}</div>
                </template>
            </el-table-column>
            <el-table-column label="卖方" align="center" width="200px" v-if="userInfoVO.userType != '00'">
                <template slot-scope="scope">
                    <div v-if="scope.row.sellSourceType == '00'">总部</div>
                    <div v-if="scope.row.sellSourceType != '00'">{{ getOrgNameSell(scope.row) }}</div>
                </template>
            </el-table-column>
            <el-table-column label="更新时间" align="center" prop="createTime" width="200px" />
            <el-table-column label="备注" align="center" prop="remark" />
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, updateRecordStatus } from '@/api/core/stockRecord'
import { allOrg } from '@/api/system/store'
export default {
    name: 'stockRecord',
    dicts: ['sys_yes_no', 'goods_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 商品库存变更记录表格数据
            recordList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                goodsSkuId: null,
                buyOrgId: null,
                buySourceType: null,
                num: null,
                isAdd: null,
                sellOrgId: null,
                sellSourceType: null,
                orderId: null,
                orderNo: null,
                goodsId: null,
                goodsType: null,
                isRefund: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            goodsSkuId: null,
            orgData: [],
            userInfoVO: null
        }
    },
    created() {
        allOrg().then((res) => {
            this.orgData = []
            res.data.forEach((org) => {
                this.orgData.push({
                    key: org.sourceType + ',' + org.id,
                    label: org.sourceType == '01' ? '工厂-' + org.name : '门店-' + org.name
                })
            })
        })

        this.handleUser()
        this.goodsSkuId = this.$route.params.goodsSkuId
        this.queryParams.goodsSkuId = this.goodsSkuId
        this.getList()
    },
    computed: {},
    methods: {
        getOrgNameBuy(row) {
            const buyVo = this.orgData.find((item) => item.key == row.buySourceType + ',' + row.buyOrgId)
            if (buyVo != null) {
                return buyVo.label
            }
            return ''
        },
        getOrgNameSell(row) {
            const sellVo = this.orgData.find((item) => item.key == row.sellSourceType + ',' + row.sellOrgId)
            if (sellVo != null) {
                return sellVo.label
            }
            return ''
        },
        handleUser() {
            this.userInfoVO = this.getUserInfo()
        },
        /** 查询商品库存变更记录列表 */
        getList() {
            this.loading = true
            listRecord(this.queryParams).then((response) => {
                this.recordList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                goodsSkuId: null,
                buyOrgId: null,
                buySourceType: null,
                num: null,
                isAdd: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                sellOrgId: null,
                sellSourceType: null,
                orderId: null,
                orderNo: null,
                goodsId: null,
                goodsType: null,
                isRefund: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加商品库存变更记录'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getRecord(id).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改商品库存变更记录'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRecord(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addRecord(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除商品库存变更记录编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delRecord(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/goods/record/export',
                {
                    ...this.queryParams
                },
                `record_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
