<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="generateQRCodeText"
                        :loading="loading"
                    >生成内部码
                    </el-button
                    >
                    <el-switch
                        v-model="filterUDIType"
                        style="margin-top: 7px;margin-left: 20px"
                        active-text="过滤已有UDI码的产品">
                    </el-switch>
                </el-button-group>
            </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 prop="billNo">
                        <el-input v-model="formData.billNo" 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" :disabled="true"></el-input>
                    </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 prop="corpName">
                        <el-input v-model="formData.corpName" 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="billType">
                        <el-select v-model="formData.billType" placeholder="请选择业务类型" clearable="true"
                                   disabled="true">
                            <el-option
                                v-for="item in busTypes"
                                :key="item.localName"
                                :label="item.localName"
                                :value="item.localAction">
                                <span style="float: left">{{ item.localName }}</span>
                                <!--<span style="float: right; color: #8492a6; font-size: 13px">{{ item.action }}</span>-->
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>


            </el-row>


            <el-table v-loading="loading" :data="codeArray" style="width: 100%;" max-height="350" height="350"
                      ref="multipleTable">
                <!--                <el-table-column-->
                <!--                    type="selection"-->
                <!--                    width="55">-->
                <!--                </el-table-column>-->
                <el-table-column label="序号" type="index" width="50"></el-table-column>
                <el-table-column label="码" prop="udiCode"></el-table-column>
                <!--<el-table-column label="订单数量">-->
                <!--<template slot-scope="scope">-->
                <!--<span>{{ scope.row.count }}</span>-->
                <!--</template>-->
                <!--</el-table-column>-->
                <!--<el-table-column label="实际数量">-->
                <!--<template slot-scope="scope">-->
                <!--<span>{{ scope.row.reCount }}</span>-->
                <!--</template>-->
                <!--</el-table-column>-->
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <span>{{ statusMap[scope.row.printStatus] }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="时间"
                    prop="create_time"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span>{{ scope.row.updateTime }}</span>
                    </template>
                </el-table-column>
                <!--                <el-table-column label="操作" fixed="right" width="150">-->
                <!--                    <template slot-scope="scope">-->
                <!--                        <el-button-->
                <!--                            type="text"-->
                <!--                            size="small"-->
                <!--                            @click.native="printStockOrderDetailPDF(scope.row)"-->
                <!--                        >打印-->
                <!--                        </el-button-->
                <!--                        >-->
                <!--                    </template>-->
                <!--                </el-table-column>-->
            </el-table>

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

<script>
import {
    generateStockOrderQRCodeText,
    stockOrderDetail,
    submitStockOrder,
    submitStockOrderError,
    uploadStockOrderDetailCount
} from "../../api/warehouse/stockOrder";
import {stockQRCodeText, filterPrint} from "../../api/warehouse/stockQRCode";
import {getLocalJoinBusType} from "../../api/basic/busLocalType";

import draggable from "vuedraggable";
import {saveAs} from "file-saver";
import {parseTime} from "../../filtres/index";
import store from "../../store";
import {
    inspectionOrderDetailPDFFromTemplateFile,
    inspectionStockQRCodeTextPDFFromTemplateFile,
    orderDetailPDFFromTemplateFile, stockQRCodeTextPDFFromTemplateFile
} from "../../api/itextpdf/itextpdf";

export default {
    name: "idQuery",
    props: {
        closeDialog: {
            type: Function,
            required: true,
        },
        idQuery: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            code: "",
            query: {
                sOrderId: null,
                orderIdFk: "",
                page: 1,
                limit: 10,
                filterUDIType: null,
            },
            busTypes: [],
            formData: {},
            codeArray: [],
            total: 0,
            loading: false,
            index: null,
            formLoading: false,
            formVisible: false,
            deleteLoading: false,
            orderNo: null,
            filterUDIType: true,
            statusMap: {
                0: "未打印",
                1: "已打印",
            }
        };
    },
    components: {
        draggable,
    },
    methods: {
        submit(formName) {
            if (this.total < 1) {
                this.$message.error('未选择条码');
                return;
            }
            this.$confirm("是否提交？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let tQuery = {
                    id: this.idQuery.id
                }
                this.loading = true;
                submitStockOrder(tQuery)
                    .then((response) => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.$message.success("提交成功");
                            this.closeDialog(true);
                        } else {
                            this.$message.error(response.message);
                        }
                    });
            }).catch(() => {
                this.loading = false;
            });
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        getInputFocus(event) {
            event.currentTarget.select();
        },
        tableSelection() {
            this.$refs.multipleTable.clearSelection();
            this.$refs.multipleTable.toggleAllSelection();
        },
        handleSizeChange(val) {
            this.query.limit = val;
            // this.getStockOrderDetailList();
            this.getStockQRCodeTextList();
        },
        handleCurrentChange(val) {
            this.query.page = val;
            // this.getStockOrderDetailList();
            this.getStockQRCodeTextList();
        },
        getStockOrderDetailList() {
            this.loading = true;
            filterPrint(this.query) //查找该单号下的所有条码
                .then((response) => {
                    console.log(response)
                    this.codeArray = response.data.list || [];
                    this.total = response.data.total || 0;
                    this.loading = false;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        getStockQRCodeTextList() {
            this.loading = true;
            filterPrint(this.query) //查找该单号下的所有条码
                .then((response) => {
                    console.log(response)
                    this.codeArray = response.data.list || [];
                    this.total = response.data.total || 0;
                    this.loading = false;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        printStockOrderDetailPDF(row) {
            if (row.count <= 0) {
                return this.$message.error('数量错误');
            }
            let tQuery = {queryId: row.id,}
            this.loading = true;
            inspectionStockQRCodeTextPDFFromTemplateFile(tQuery).then((response) => {
                if (response.code === 20000) {
                    tQuery = {
                        id: row.id,
                        rowCount: row.count,
                        text: row.text
                    }
                    stockQRCodeTextPDFFromTemplateFile(tQuery).then((response) => {
                        //将pdf文件转换为url。
                        const binaryData = [];
                        binaryData.push(response);
                        //获取blob链接。
                        let url = window.URL.createObjectURL(
                            new Blob(binaryData, {type: "application/pdf"})
                        );
                        this.loading = false;
                        window.open(url);//打开新标签页，预览pdf。
                    }).catch(() => {
                        this.loading = false;
                    });
                } else {
                    this.loading = false;
                    this.$message.error(response.message);
                }
            }).catch(() => {
                this.loading = false;
            });
        },
        intentBack() {
            this.$router.go(-1);
        },
        getActionName(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].action === action) {
                    return this.busTypes[i].name;
                }
            }
        },
        getBusType() {
            let query = {
                enabled: true,
            };
            getLocalJoinBusType(query)
                .then((response) => {
                    this.busTypes = response.data.list || [];
                    this.filterQuery.billAction = this.busTypes[0].action;
                    // this.getList();
                })
                .catch(() => {
                });
        },

        generateQRCodeText() {
            let tip = "确定生成内部码？";
            if(this.codeArray .length>1){
                tip = "是否确定重新生成内部码？";
            }

            this.$confirm(tip, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let tQuery = {
                    id: this.query.sOrderId,
                    filterUDIType: this.filterUDIType
                };
                this.loading = true;
                generateStockOrderQRCodeText(tQuery)
                    .then(response => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.$message.success(response.data);
                            this.getStockOrderDetailList();
                            this.detailList = [];
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.loading = false;
                    })
            }).catch(() => {

            });
        },
    },
    filters: {},
    mounted() {
        document.body.ondrop = function (event) {
            event.preventDefault();
            event.stopPropagation();
        };
    },
    created() {
        this.getBusType();
        this.formData = {};
        this.codeArray = [];
        if (this.$isNotBlank(this.idQuery.id)) {
            this.formData = this.idQuery.formData;
            // this.query.orderIdFk = this.idQuery.id;
            // this.getStockOrderDetailList();
            this.query.sOrderId = this.idQuery.id;
            this.query.filterUDIType = this.idQuery.filterUDIType;
            this.getStockQRCodeTextList();
        }
    },
};
</script>
<style>

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

</style>



