<template>
    <div>
        <el-card class="el-card">
            <el-form
                :inline="true"
                :model="query"
                class="query-form"
                size="mini"
            >
                <el-row>
                    <el-form-item class="query-form-item">
                        <el-input v-model="query.billNo" 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="onSubmit">查询</el-button>
                            <!--                            <el-button type="primary" icon="search" @click="handleForm(null, null)">新增模板</el-button>-->
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>
            <el-table v-loading="loading"
                      :data="list"
                      style="width: 100%; height: 350px" highlight-current-row
                      @row-click="getStockOrderDetailList">
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column label="单据号" prop="billNo" width="250"></el-table-column>
                <el-table-column label="往来单位" prop="corpName" width="250"></el-table-column>
                <el-table-column label="单据日期" prop="billdate" width="100"></el-table-column>
                <el-table-column label="单据状态" width="100">
                    <template slot-scope="scope">
                        {{ scope.row.status | statusFilterName }}
                    </template>
                </el-table-column>
                <el-table-column v-if="query.status === '401'" label="异常信息" prop="statusInfo"></el-table-column>
                <el-table-column label="业务类型" prop="billType">
                    <template slot-scope="scope">
                        <span>{{ getActionName(scope.row.billType) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="采购类型" width="100">
                    <template slot-scope="scope">
                        {{ scope.row.type | typeFilterName }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="250">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native="againSubmit(scope.row)"
                        >重新提交
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native="errorDistributionForm(scope.$index, scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native="deleteStockOrder(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-card>
        <el-dialog
            title="编辑"
            :visible.sync="errorDistributionVisible"
            width="60%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="errorDistributionVisible"
            @close='closeDialog'
        >
            <stockOrderErrorDistribution
                :closeDialog="closeDialog"
                :idQuery="idQuery"
            ></stockOrderErrorDistribution>
        </el-dialog>

    </div>
</template>

<script>
import {
    stockOrderList, stockOrderDetail,
    deleteErrorStockOrderById, deleteStockOrderDetailById,
    copyStockOrderDetail, submitStockOrder,
    uploadStockOrderDetail, uploadStockOrderDetailCount, submitStockOrderError
} from "../../api/warehouse/stockOrder";
import {BASE_URL} from "../../config/app";
import stockOrderErrorDistribution from "./stockOrderErrorDistribution";
import draggable from "vuedraggable";
import store from "../../store";
import {getBussinessType} from "../../api/basic/bussinessType";

export default {
    name: "stockOrderError",
    data() {
        return {
            query: {
                billNo: "",
                status: '401',
                page: 1,
                limit: 20
            },
            detailQuery: {
                orderIdFk: "",
                page: 1,
                limit: 20
            },
            list: [],
            detailList: [],
            total: 0,
            detailTotal: 0,
            loading: false,
            detailLoading: false,
            idQuery: {
                id: "",
            },
            errorDistributionVisible: false,
            rowStatus: '',
            detailFormRules: {
                batchNo: [
                    {required: true, message: "请输入批次号", trigger: "blur"}
                ],
                productDate: [
                    {required: true, message: "请输入生产日期", trigger: "blur"}
                ],
                expireDate: [
                    {required: true, message: "请输入失效日期", trigger: "blur"}
                ],
                sweepCount: [
                    {required: true, message: "请输入扫码数量", trigger: "blur"}
                ]
            },
            detailFormVisible: false,
            detailFormData: {},
            detailFormLoading: false,
            busTypes: [],
        }
    },
    methods: {
        onReset() {
            this.$router.push({
                path: ""
            });
            this.query = {
                billNo: "",
                status: '201',
                page: 1,
                limit: 20
            };
            this.getList();
            this.detailList = [];
        },
        onSubmit() {
            this.getList();
            this.detailList = [];
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getList();
        },
        detailHandleCurrentChange(val) {
            this.query.page = val;
            this.getStockOrderDetailList();
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        getList() {
            this.loading = true;
            this.query.corpId = store.getters.customerId;
            stockOrderList(this.query)
                .then(response => {
                    this.loading = false;
                    this.list = response.data.list || [];
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        errorDistributionForm(index, row) {
            this.idQuery.id = '';
            if (this.$isNotBlank(row.id)) {
                this.idQuery.id = row.id;
                this.idQuery.formData = row;
                this.errorDistributionVisible = true;
            }
        },
        againSubmit(row) {
            this.$confirm("是否重新提交？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let tQuery = {
                    id: row.id
                }
                this.loading = true;
                submitStockOrderError(tQuery)
                    .then((response) => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.$message.success("提交成功");
                            this.getList();
                        } else {
                            this.$message.error(response.message);
                        }
                    });
            }).catch(() => {
                this.loading = false;
            });
        },
        closeDialog(type) {
            this.errorDistributionVisible = false;
            this.getList();
            if (this.$isNotBlank(type)) {
                this.detailList = [];
            }
        },
        getStockOrderDetailList(row) {
            // if (this.$isNotBlank(row)) {
            //     this.detailQuery.orderIdFk = row.id;
            //     this.rowStatus = row.status;
            // }
            // this.detailLoading = true;
            // stockOrderDetail(this.detailQuery)
            //     .then((response) => {
            //         console.log(response)
            //         this.detailLoading = false;
            //         this.detailList = response.data.list || [];
            //     })
            //     .catch(() => {
            //         this.detailLoading = false;
            //         this.detailList = [];
            //     });
        },
        deleteStockOrder(row) {
            this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                deleteErrorStockOrderById({id: row.id})
                    .then(response => {
                        if (response.code === 20000) {
                            this.$message.success(response.data);
                            this.getList();
                            this.detailList = [];
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {

                    })
            }).catch(() => {

            });
        },
        copyDetail(row) {
            this.$confirm("是否复制该记录?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let tQuery = {
                    id: row.id
                }
                this.detailLoading = true;
                copyStockOrderDetail(tQuery)
                    .then((response) => {
                        this.detailLoading = false;
                        if (response.code === 20000) {
                            this.$message.success("复制成功");
                            this.getStockOrderDetailList();
                        } else {
                            this.$message.error(response.message);
                        }
                    });
            }).catch(() => {
                this.detailLoading = false;
            });
        },
        deleteStockOrderDetail(row) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                deleteStockOrderDetailById({id: row.id})
                    .then(response => {
                        if (response.code === 20000) {
                            this.$message.success(response.data);
                            this.getStockOrderDetailList();
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {

                    })
            }).catch(() => {

            });
        },
        // 显示表单
        handleDetailForm(index, row) {
            this.detailFormVisible = true;
            this.detailFormData = {};
            if (row !== null) {
                this.detailFormData = Object.assign({}, row);
            }
        },
        hideDetailForm() {
            // 更改值
            this.detailFormVisible = !this.detailFormVisible;
            return true;
        },
        getBusType() {
            let query = {
                enabled: true,
            };
            getBussinessType(query)
                .then((response) => {
                    this.busTypes = response.data.list || [];
                    this.filterQuery.billAction = this.busTypes[0].action;
                    // this.getList();
                })
                .catch(() => {
                });
        },
        getActionName(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].action === action) {
                    return this.busTypes[i].name;
                }
            }
        },
        submitUploadDetail() {
            this.$refs["dataForm"].validate(valid => {
                if (valid) {
                    this.detailLoading = true;
                    let tQuery = this.detailFormData;
                    uploadStockOrderDetail(tQuery).then((response) => {
                        this.detailLoading = false;
                        if (response.code === 20000) {
                            this.$message.success("修改完成");
                            this.hideDetailForm();
                            this.getStockOrderDetailList();
                        } else {
                            this.$alert(response.message, '提示', {
                                confirmButtonText: '确定',
                                type: 'warning',
                                closeOnClickModal: true,
                                callback: action => {
                                    this.$refs.inputRef.focus();
                                }
                            });
                        }
                    });
                }
            });
        }
    },
    components: {
        draggable,
        stockOrderErrorDistribution,
    },
    filters: {
        statusFilterType(status) {
            const statusMap = {
                0: "gray",
                1: "success"
            };
            return statusMap[status];
        },
        statusFilterName(status) {
            const statusMap = {
                101: "未处理",
                102: "已处理",
                201: "未配货",
                202: "已配货",
                301: "已打印",
                401: "异常",
                501: "未提交",
                502: "已提交",
            };
            return statusMap[status];
        },
        typeFilterName(type) {
            const typeMap = {
                1: "预入库",
                2: "普通采购",
            };
            return typeMap[type];
        }
    },
    created() {
        this.getBusType();
        this.getList();
    }
}
</script>

<style scoped>

</style>
