<template>
    <div>
        <el-form :model="formData" ref="dataForm">
            <el-button-group style="display: flex;margin: -50px 0 10px 80%; height: 35px">
                <el-button
                    type="primary"
                    :disabled="this.detailList.length > 0"
                    @click.native="selectInvProduct()"
                >录入产品
                </el-button>
                <el-button
                    type="primary"
                    @click.native="saveOrder()"
                >保存
                </el-button>
            </el-button-group>

            <el-row :gutter="20" style="margin-top: 20px;">
                <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.createTime" auto-complete="off" :disabled="true"></el-input>
                    </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="invWarehouseCode">
                        <el-select v-model="formData.invWarehouseCode" placeholder="盘点分库信息"
                                   :disabled="detailList.length>0"
                                   @change="warehouseChange"
                                   clearable>
                            <el-option
                                v-for="item in warehouseList"
                                :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 class="query-form-item" prop="invSpaceCode">
                        <el-select v-model="formData.invSpaceCode" placeholder="盘点货位信息"
                                   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-row :gutter="20" style="margin-top: 12px">
                <el-col :span="2">
                    <div class="ao-text">
                        <el-link
                            target="_blank">{{ scanText }}
                        </el-link>
                    </div>
                </el-col>
                <el-col :span="16">
                    <el-form-item prop="code">
                        <el-input
                            id="inputer"
                            @focus="getInputFocus($event)"
                            @keypress.enter.native="enterKey($event)"
                            ref='inputRef'
                            style="ime-mode:disabled"
                            type="tel"
                            v-model="formData.code"
                        ></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="2">
                    <el-button-group style="display: flex">
                        <el-button
                            type="primary"
                            size="small"
                            @click.native.stop="addCode()"
                            style="height: 31px; margin-left: 20px"
                        >添加
                        </el-button
                        >
                    </el-button-group>

                </el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="11" style="margin-left: 40px">

                </el-col>
                <el-col :span="3">
                    <div class="text item" style="margin-left: 30px"> 养护产品数量：{{ detailList.length }}
                    </div>
                </el-col>
            </el-row>

            <el-table v-loading="loading" :data="detailList" style="width: 100%; margin-top: 10px;"
                      max-height="300" height="300" ref="multipleTable">
                <el-table-column label="序号" type="index" width="50"></el-table-column>
                <el-table-column label="条码" type="index" width="150"></el-table-column>
                <el-table-column width="150"
                                 label="产品通用名"
                                 prop="productName"
                                 show-overflow-tooltip
                ></el-table-column>
                <el-table-column width="150"
                                 label="规格型号"
                                 prop="ggxh"
                                 show-overflow-tooltip
                ></el-table-column>
                <el-table-column width="150" label="批次号" prop="batchNo">
                </el-table-column>
                <el-table-column width="150" label="库存数量" prop="invNum"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column width="150" label="盘点数量" prop="countNum"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column width="180" label="生产日期(yyMMdd)" prop="produceDate">
                </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 label="操作" fixed="right" width="150">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.stop="true"
                            @click.native="deleteOrderDetail(scope.$index, scope.row)"
                        >减一
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

        </el-form>
    </div>
</template>

<script>
import {filterSubAll} from "@/api/basic/invSubWarehouse";
import {getSpaceCodeList} from "@/api/inventory/invSpace";
import {
    addOrder,
    addOrderDetail,
    batchAddDetail,
    updateDetail,
    updateOrder,
    deleteDetail,
    getMAOrderDetailList
} from "@/api/inventory/invMAOrder";
import {isBlank} from "@/utils/strUtil";

export default {
    name: "",
    props: {
        maOrder: {
            type: Object,
            required: true,
        },
        closeDialog: {
            type: Function,
            required: true,
        },
    },

    data() {
        return {
            warehouseList: [],
            spaceList: [],
            formData: {
                orderId: null,
                createTime: null,
                invWarehouseCode: null,
                invSpaceCode: null,
                code: null,
            },
            detailList: [],
            loading: false,
            scanText: "扫码录入：",

        }
    },
    methods: {
        getAllWarehouse() {
            filterSubAll().then((res) => {
                if (res.code === 20000) {
                    this.warehouseList = res.data || [];
                }
            });
        },
        warehouseChange() {
            this.formData.invSpaceCode = null;
            this.spaceList = [];
            let params = {invWarehouseCode: this.formData.invWarehouseCode};
            getSpaceCodeList(params).then((res) => {
                this.spaceList = res.data || [];
            });
        },
        selectInvProduct() {
            if (isBlank(this.formData.orderId)) {
                this.saveOrder();
            }

        },
        saveOrder() {
            if (isBlank(this.formData.invWarehouseCode)) {
                this.$message.warning("请选择养护仓库！");
                return;
            }
            if (this.detailList.length === 0) {
                this.$message.warning("请录入养护产品信息！");
                return;
            }
            addOrder(this.formData).then((res) => {
                if (res.code === 200000) {
                    this.formData.orderId = res.data;
                    this.$message.success("保存成功！");
                }
            });
        },
        getInputFocus(event) {
            event.currentTarget.select();
        },
        enterKey(event) {
            this.addCode();
            this.$refs.inputRef.focus();
            this.$refs.inputRef.select();
        },
        addCode() {
            if (isBlank(this.formData.invWarehouseCode)) {
                this.$message.warning("请先选择养护仓库！");
                return;
            }
            if (isBlank(this.formData.orderId)) {
                this.saveOrder();
            }
        },
        deleteOrderDetail(index, row) {

        },
        getList() {
            let params = {orderIdFk: this.formData.orderId};
            getMAOrderDetailList(params).then((res) => {
                if (res.code === 20000) {
                    this.detailList = res.data.list || [];
                }
            })
        },
    },
    created() {
        this.getAllWarehouse();
        if (!isBlank(this.maOrder.orderId)) {
            this.formData.orderId = this.maOrder.orderId;
            this.formData.createTime = this.maOrder.createTime;
            this.getList();
        }
    },
};
</script>

