<template>
    <div>

        <el-form :model="formData" :rules="formRules" ref="dataForm">
            <el-row type="flex" justify="end">
                <el-button-group style="display: flex;margin-bottom: 15px; margin-right: 50px">
                    <el-button
                        type="primary"
                        @click.native="saveOrder()"
                        :loading="loading"
                    >保存
                    </el-button
                    >
                </el-button-group>
            </el-row>

            <el-card style="margin-top: -5px;">
                <el-row :gutter="20" style="margin-top: 10px;">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>单据号：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="orderId">
                            <el-input v-model="formData.orderId" auto-complete="off" :disabled="true"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>单据日期：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="billDate">
                            <!--                            <el-input v-model="formData.billdate" auto-complete="off"></el-input>-->
                            <el-date-picker
                                v-model="formData.billDate"
                                type="datetime"
                                placeholder="选择日期"
                                style="width: 100%"
                                :clearable="false"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" style="margin-top: 0px">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前仓库：</span>
                        </div>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item prop="locStorageCode">
                            <el-select v-model="formData.locStorageCode" placeholder="当前仓库信息"
                                       @change="locInChange"
                                       :disabled="codeArray.length>0"
                                       clearable>
                                <el-option
                                    v-for="item in storageList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>


                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前分库：</span>
                        </div>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item prop="invWarehouseCode">
                            <el-select v-model="formData.invWarehouseCode" placeholder="当前分库信息"
                                       :disabled="codeArray.length>0"
                                       @change="subStorageChange"
                                       clearable>
                                <el-option
                                    v-for="item in subInvList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-row :gutter="20">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前货位：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item class="query-form-item" prop="invSpaceCode"
                        >
                            <el-select v-model="formData.invSpaceCode" placeholder="当前货位信息"
                                       :disabled="codeArray.length>0"
                                       clearable>
                                <el-option
                                    v-for="item in spaceList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>

            <el-card>
                <el-button-group style="display: flex">
                    <el-button
                        type="primary"
                        size="small"
                        @click.native.stop="selectProductFunction()"
                        style="margin: 0px 60px 10px auto; height: 35px"
                        :loading="loading"
                    >产品录入
                    </el-button
                    >
                </el-button-group>

                <el-table v-loading="loading" :data="codeArray" style="width: 100%;"
                          max-height="300" height="300" ref="multipleTable">
                    <el-table-column label="序号" type="index" width="50"></el-table-column>
                    <el-table-column width="150"
                                     label="产品通用名"
                                     prop="productName"
                                     show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column width="150"
                                     label="规格型号"
                                     prop="spec"
                                     show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column width="150" label="批次号" prop="batchNo">
                    </el-table-column>
                    <el-table-column width="180" label="生产日期(yyMMdd)" prop="productDate">
                    </el-table-column>
                    <el-table-column width="180" label="失效日期(yyMMdd)" prop="expireDate" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column width="250" label="注册/备案凭证号" prop="zczbhhzbapzbh" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column width="150" label="生产厂家" prop="ylqxzcrbarmc"
                                     show-overflow-tooltip></el-table-column>
                    <el-table-column width="150" label="库存数量" prop="invCount"
                                     show-overflow-tooltip></el-table-column>
                    <el-table-column label="操作" fixed="right" width="150">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click.stop="true"
                                @click.native="deleteCodeArray(scope.$index, scope.row)"
                            >删除
                            </el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>

            </el-card>
        </el-form>

        <el-dialog
            title="库存产品录入"
            :visible.sync="selectInvProductVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            width="85%"
            v-if="selectInvProductVisible"
            :append-to-body='true'
        >
            <el-card>
                <el-form :inline="true" :model="filterQuery" class="query-form" size="mini">
                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input v-model="filterQuery.nameCode" placeholder="产品标识DI"></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input v-model="filterQuery.cpmctymc" placeholder="产品名称"></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input v-model="filterQuery.batchNo" placeholder="批次号"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button-group>
                                <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                                <el-button type="primary" icon="search" @click="getInvProducts"
                                >查询
                                </el-button
                                >
                                <el-button type="primary" icon="search" @click="combine">确定</el-button>
                            </el-button-group>
                        </el-form-item>
                    </el-row>
                </el-form>

                <el-table v-loading="loading" :data="productList" style="width: 100%"
                          highlight-current-row="false"
                          ref="multipleTable">
                    <el-table-column
                        type="selection"
                        width="55">
                    </el-table-column>
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column label="产品标识" prop="nameCode"></el-table-column>
                    <el-table-column label="产品名称" prop="cpmctymc"></el-table-column>
                    <el-table-column label="规格型号" prop="ggxh"></el-table-column>
                    <el-table-column label="批次号" prop="batchNo"></el-table-column>
                    <el-table-column label="生产日期" prop="productionDate"></el-table-column>
                    <el-table-column label="失效日期" prop="expireDate"></el-table-column>
                    <el-table-column label="入库数量" prop="inCount"></el-table-column>
                    <el-table-column label="出库数量" prop="outCount"></el-table-column>
                    <el-table-column label="结余数量" prop="reCount">
                        <template slot-scope="scope">
                            {{ (scope.row.inCount - scope.row.outCount) }}
                        </template>
                    </el-table-column>
                </el-table>

                <el-pagination
                    :page-size="filterQuery.limit"
                    @current-change="handleCurrentChange"
                    layout="prev, pager, next"
                    :total="total"
                ></el-pagination>
            </el-card>
        </el-dialog>
    </div>
</template>

<script>
import {filterSubByInv} from "@/api/basic/invSubWarehouse";
import {isBlank} from "@/utils/strUtil";
import {filterAllByUser} from "@/api/basic/invWarehouse";
import {getSpaceCodeList} from "@/api/inventory/invSpace";
import {filterProducts} from "@/api/inventory/InvProducts";
import {saveCountOrder} from "@/api/inventory/invCountOrder";

export default {
    name: "closeDialog",
    props: {
        data: {
            type: Object,
            required: true,
        },
        closeDialog: {
            type: Function,
            required: true,
        },
    },
    data() {
        return {
            formData: {
                orderId: null,
                billDate: null,
                locStorageCode: null,
                invWarehouseCode: null,
                invSpaceCode: null
            },
            formRules: {
                billDate: [
                    {required: true, message: "请选择单据日期", trigger: "blur"}
                ],
                locStorageCode: [
                    {required: true, message: "请选择当前仓库", trigger: "blur"}
                ],
                invWarehouseCode: [
                    {required: true, message: "请选择当前分库", trigger: "blur"}
                ],
                invSpaceCode: [
                    {required: true, message: "请选择当前货位", trigger: "blur"}
                ]
            },
            codeArray: [],
            total: 0,
            loading: false,
            index: null,
            selectInvProductVisible: false,
            storageList: [],
            subInvList: [],
            spaceList: [],
            filterQuery: {
                invStorageCode: null,
                invWarehouseCode: null,
                nameCode: null,
                cpmctymc: null,
                batchNo: null,
                page: 1,
                limit: 10
            },
            productList: []
        };
    },
    methods: {
        saveOrder() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    if (this.codeArray.length === 0) {
                        this.$message.error("请选择盘点产品");
                        return;
                    }

                    let invProductList = [];
                    this.codeArray.forEach(product => {
                        invProductList.push(product);
                    });

                    let orderData = {
                        invCountOrder: {
                            createTime: this.formData.billDate,
                            invStorageCode: this.formData.locStorageCode,
                            invWarehouseCode: this.formData.invWarehouseCode,
                            invSpaceCode: this.formData.invSpaceCode
                        },
                        invProductList: invProductList
                    };

                    saveCountOrder(orderData).then((res) => {
                        if (res.code === 20000) {
                            this.$message.success("保存成功");
                            this.closeDialog();
                        } else {
                            this.$message.error(res.data.message);
                        }
                    });
                }
            })
        },
        // 刷新表单
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getInvProducts();
        },
        locInChange() {
            //查询分库
            let params = {pcode: this.formData.locStorageCode}
            filterSubByInv(params).then((res) => {
                this.subInvList = res.data || [];
            });
            this.formData.invWarehouseCode = null;
            this.formData.invSpaceCode = null;
        },
        subStorageChange() {
            //查询货位
            let params = {
                invStorageCode: this.formData.locStorageCode,
                invWarehouseCode: this.formData.invWarehouseCode
            };
            getSpaceCodeList(params).then((res) => {
                this.spaceList = res.data || [];
            });
            this.formData.invSpaceCode = null;
        },
        selectProductFunction() {
            if (isBlank(this.formData.locStorageCode) || isBlank(this.formData.invWarehouseCode)) {
                this.$message.warning("请先选择当前仓库和分库");
                return;
            }
            this.selectInvProductVisible = true;
            this.onReset();
            this.getInvProducts();
        },
        getInvProducts() {
            this.filterQuery.invStorageCode = this.formData.locStorageCode;
            this.filterQuery.invWarehouseCode = this.formData.invWarehouseCode;
            filterProducts(this.filterQuery).then((res) => {
                if (res.code === 20000) {
                    this.productList = res.data.list || [];
                    this.total = res.data.total || 0;
                }
            })
        },
        getStorage() {
            filterAllByUser().then((res) => {
                if (res.code === 20000) {
                    this.storageList = res.data || [];
                }
            })
        },
        onReset() {
            this.filterQuery = {
                invStorageCode: null,
                invWarehouseCode: null,
                nameCode: null,
                cpmctymc: null,
                batchNo: null,
                page: 1,
                limit: 10
            };
            this.getInvProducts();
        },
        combine() {
            let selection = this.$refs.multipleTable.selection;
            if (selection.length === 0) {
                this.$message.warning("请选择产品");
                return;
            }
            selection.forEach(product => {
                let data = {
                    productName: product.cpmctymc,
                    relIdFk: product.relIdFk,
                    spce: product.ggxh,
                    batchNo: product.batchNo,
                    productDate: product.productionDate,
                    expireDate: product.expireDate,
                    zczbhhzbapzbh: null,
                    ylqxzcrbarmc: null,
                    invCount: product.inCount - product.outCount
                };
                this.codeArray.push(data);
            });
            this.selectInvProductVisible = false;
        },
        deleteCodeArray(index, row) {
            for (let i = 0; i < this.codeArray.length; i++) {
                if (this.codeArray[i] === row) {
                    this.codeArray.splice(i, 1);
                    return;
                }
            }
        },
    },
    created() {
        this.getStorage();
    },
};
</script>
<style>

.ao-text {
    width: 100%;
    font-size: 13px;
    font-family: "Microsoft YaHei";
    float: right;
    text-align: right;
    margin-top: 10px;
}

</style>
