<template>
    <div>
        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-form-item class="query-form-item">
                <el-input v-model="query.code" placeholder="搜索"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button-group>
                    <el-button type="primary" icon="search" @click="onSubmit"
                    >查询
                    </el-button
                    >
                    <el-button type="primary" icon="search" @click="deleteDialog"
                    >删除
                    </el-button
                    >
                </el-button-group>
            </el-form-item>
        </el-form>
        <el-table
            v-loading="loading"
            :data="codeArry"
            style="width: 100%"
            ref="multipleTable"
            :row-style="rowStyle"
            tooltip-effect="dark"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="序号" type="index" fixed></el-table-column>
            <el-table-column label="条码" prop="code" show-overflow-tooltip fixed></el-table-column>
            <el-table-column
                label="生产日期"
                prop="produceDate"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="失效日期"
                prop="expireDate"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="批次号"
                prop="batchNo"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="序列号"
                prop="serialNo"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="供应商"
                prop="supName"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="扫码数量"
                prop="count"
                width="220"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column label="操作" fixed="right" width="250">
                <template slot-scope="scope">

                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="editCode(scope.row)"
                    >编辑
                    </el-button
                    >
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="bindRl(scope.row)"
                    >选择产品
                    </el-button
                    >
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="handleUnitClick(scope.row)"
                    >选择供应商
                    </el-button
                    >
                </template>
            </el-table-column>

        </el-table>
        <el-pagination
            :page-size="query.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>


        <el-dialog
            title="选择供应商"
            :visible.sync="dialogTableVisible"
            v-if="dialogTableVisible"
            width="70%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            append-to-body
        >
            <DialogSelectUnit
                :codeId="curRow"
                v-on:selectSupUnit="selectSupUnit"
                v-on:closeBindDialog="closeBindDialog"
            ></DialogSelectUnit>
        </el-dialog>
        <el-dialog
            title="绑定产品ID"
            :visible.sync="selectRlVisible"
            width="80%"
            v-if="selectRlVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            append-to-body
        >
            <selectRlDialog
                :curRow="curRow"
                v-on:selectBindRl="selectBindRl"
                v-on:closeBindDialog="closeBindDialog"
            ></selectRlDialog>
        </el-dialog>


        <el-dialog
            :title="editTitle"
            :visible.sync="editCodeVisible"
            append-to-body width="70%"
            v-if="editCodeVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <editCodeDialog
                editTye="2"
                :closeCodeDialog="closeCodeDialog"
                :codeDetail="codeDetail">
            </editCodeDialog>
        </el-dialog>

    </div>
</template>

<script>
import {
    orderList,
    errorCodeList,
    deleteCodes, updateCodeBindSup,
} from "../../api/warehouse/order";
import draggable from "vuedraggable";
import {saveAs} from "file-saver";
import DialogSelectUnit from "./DialogSelectUnit";
import selectRlDialog from "./DialogSelectRl";
import editCodeDialog from "@/views/warehouse/editCode";

export default {
    name: "idQuery",
    props: {
        idQuery: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            query: {
                code: "",
                corpOrderId: "",
                page: 1,
                limit: 20,
            },
            codeArry: [],
            total: 0,
            loading: true,
            index: null,
            formLoading: false,
            formVisible: false,
            deleteLoading: false,
            orderNo: null,
            curRow: null,
            multipleSelection: [],
            selectRlVisible: false,
            dialogTableVisible: false,

            codeDetail: null,
            editTitle: "补齐三期",
            editCodeVisible: false,
        };
    },
    components: {
        draggable, DialogSelectUnit, selectRlDialog, editCodeDialog
    },
    methods: {
        handleSelectionChange(val) {
            console.log(val);
            this.multipleSelection = val;
        },

        onReset() {
            this.$router.push({
                path: "",
            });
            this.query = {
                code: "",
                corpOrderId: "",
                page: 1,
                limit: 20,
            };
            this.getCodeList();
        },
        onSubmit() {
            this.$router.push({
                path: "",
                query: this.query,
            });
            this.query.page = 1;
            this.getCodeList();
        },
        handleSizeChange(val) {
            this.query.limit = val;
            this.getCodeList();
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getCodeList();
        },
        closeCodeDialog() {
            this.editCodeVisible = false;
            this.getCodeList();
        },
        deleteDialog() {
            this.$confirm("是否删除所选条码?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.onDelete();
                })
                .catch(() => {
                });
        },

        onDelete() {
            var selectData = this.multipleSelection;

            var codeList = [];
            selectData.forEach((obj, index) => {
                codeList.push(obj.id);
            });
            var tquery = {
                codes: codeList,
            };
            deleteCodes(tquery).then((response) => {
                this.getCodeList();
                this.$message({
                    type: "success",
                    message: "删除成功！",
                });
            });
        },

        getCodeList() {
            this.loading = true;
            this.query.orderId = this.idQuery.id;
            errorCodeList(this.query) //查找该单号下的所有条码
                .then((response) => {
                    this.loading = false;
                    this.codeArry = response.data.list || [];
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        intentBack() {
            this.$router.go(-1);
        },
        //绑定数据
        bindRl(val) {
            this.curRow = val;
            this.selectRlVisible = true;
        },
        handleUnitClick(row) {
            this.curRow = row;
            this.dialogTableVisible = true;
        },
        selectSupUnit(row) {
            let query = {
                id: this.curRow.id,
                supId: row.erpId,
            };
            console.log("row.erpId = " + row.erpId);
            updateCodeBindSup(query).then((response) => {
                if (response.code == 20000) {
                    this.$message.success("绑定成功");
                    this.idQuery.id = this.curRow.orderId;
                    this.getCodeList();
                } else {
                    this.$message.error(response.message);
                }
            }).catch(() => {

            });
        },


        selectBindRl(row) {
            let query = {
                id: this.curRow.id,
                relId: row.id,
            };
            console.log("row.relId = " + row.id);
            updateCodeBindSup(query).then((response) => {
                if (response.code == 20000) {
                    this.$message.success("绑定成功");
                    this.idQuery.id = this.curRow.orderId;
                    this.getCodeList();
                } else {
                    this.$message.error(response.message);
                }
            }).catch(() => {

            });
        },

        closeBindDialog(val) {
            console.log("closeBindDialog" + val);
            this.selectRlVisible = false;
            this.dialogTableVisible = false;
        },

        rowStyle({row, rowIndex}) {
            let rowBackground = {};
            if (row.supId == null || row.relId == null) {
                rowBackground.color = '#f60303';
            }

            return rowBackground;
        },
        editCode(row) {
            this.codeDetail = row;
            this.editCodeVisible = true;
        },
    },
    filters: {},
    mounted() {
        document.body.ondrop = function (event) {
            event.preventDefault();
            event.stopPropagation();
        };
    },
    created() {
        // 将参数拷贝进查询对象
        let query = this.$route.query;
        this.orderNo = query.id;

        this.query = Object.assign(this.query, query);
        this.query.limit = parseInt(this.query.limit);
        this.query.corpOrderId = query.id;

        // 加载表格数据
        this.getCodeList();
    },
};
</script>

<style type="text/scss" lang="scss">
</style>
