<template>
    <div>
        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-row>
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.genKey" placeholder="记录ID"></el-input>
                </el-form-item>

                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.status" placeholder="处理状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="处理中" value="0"></el-option>
                        <el-option label="已处理" value="1"></el-option>
                        <el-option label="异常" value="2"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.thirdSysFk" placeholder="请选择第三方系统" @change="sysChange">
                        <el-option
                            v-for="item in thirdSys"
                            :key="item.value"
                            :label="item.thirdName"
                            :value="item.thirdId">
                            <span style="float: left">{{ item.thirdName }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button-group style="display:flex;">
                        <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                        <el-button type="primary" icon="search" @click="getList">查询</el-button>
                        <el-upload
                            :action="uploadFileUrl"
                            multiple
                            :limit="1"
                            :data="uploadData"
                            :show-file-list="false"
                            :on-success="handleChange"
                            v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==1"
                            :file-list="fileList"
                        >
                            <el-button size="mini" type="primary">导入业务单据</el-button>
                        </el-upload>
                        <el-button type="primary" icon="search" @click="jumpDl"
                                   v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==1">模板下载
                        </el-button>
                        <el-button type="primary" icon="search" @click="downloadOrderss"
                                   v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==3">
                            下载业务单据
                        </el-button>
                    </el-button-group>
                </el-form-item>
            </el-row>
        </el-form>

        <el-table
            v-loading="loading"
            :data="list"
            style="width: 100%"
            @selection-change="handleSelectionChange"
        >
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column
                label="记录ID"
                prop="genKey"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column
                label="来源"
                prop="fromType"
                show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
                label="更新日期"
                prop="updateTime"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="来源系统"
                prop="thirdSysFk"
                :formatter="formatterThirdSys"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="状态"
                prop="status"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <el-tag
                        :type="statusFilterType(scope.row.status)"
                    >{{ status[scope.row.status] }}
                    </el-tag
                    >
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="160">
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="handleDetailClick(scope.row)"
                    >详情
                    </el-button
                    >
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="deleteDialog(scope.row.id)"
                    >删除
                    </el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
            title="导入订单详情"
            :visible.sync="udiImportDetailVisible"
            width="85%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="udiImportDetailVisible"
        >
            <thrOrderImportDetail
                :currentRow="currentRow"
            ></thrOrderImportDetail>
        </el-dialog>

        <el-dialog
            title="下载业务单据"
            :visible.sync="thrOrderSelectVisible"
            width="85%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="thrOrderSelectVisible"
        >
            <thrOrderSelect :selectType="selectType" v-on:cancelDialog="cancelDialog" :isDownThrSys="true"
            ></thrOrderSelect>
        </el-dialog>


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

<script>
import axios from "axios";
import {filterLog, deleteLog} from "../../api/thrsys/thrOrderImport";
import {getBasicThirdSys, filterDetailByKey} from "../../api/basic/basicThirdSys";
import {downloadAll} from "../../api/thrsys/thrOrder";
import {getBussinessType} from "../../api/basic/bussinessType";
import thrOrderImportDetail from "./ThrOrderImportDetail";
import thrOrderSelect from "./ThrOrderSelect";
import {selectIp} from "../../api/param/systemParamConfig";

export default {
    data() {
        return {
            filterQuery: {
                genKey: null,
                status: null,
                fromType: null,
                page: 1,
                limit: 20,
                thirdSysFk: null,
            },
            udiImportDetailVisible: false,
            checked: false,
            list: [],
            detailList: [],
            thirdSys: [],
            thirdSysDetail: null,
            total: 0,
            currentRow: null,
            editQuery: null,
            fromStatus: {
                0: "产品信息",
                1: "库存信息",
                2: "异常第三方上传"
            },
            status: {
                0: "等待处理",
                1: "正在处理",
                2: "处理异常",
                3: "处理完成"
            },
            uploadFileUrl: null,
            uploadData: {
                thirdSys: "thirdId",
            },
            templateDlUrl: null,
            busTypes: [],
            thrOrderSelectVisible: false,
        };
    },

    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                fromType: null,
                genKey: null,
                status: null,
                page: 1,
                limit: 20,
                thirdSysFk: null,
            };
            this.getList();
        },
        getList() {
            this.loading = true;
            filterLog(this.filterQuery)
                .then((response) => {
                    if (response.code == 20000) {
                        this.list = response.data.list || [];
                        this.total = response.data.total || 0;
                    } else {
                        this.$message.error(response.message);
                    }
                    this.loading = false;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },

        handleDetailClick(row) {
            this.currentRow = row;
            console.log(this.currentRow.genKey)
            this.udiImportDetailVisible = true;
        },

        sysChange() {
            this.getThirdSysDetail();
            // console.log( this.thirdSysDetail.thirdId+"\n"+ this.thirdSysDetail.fromType+"\n"+this.thirdSysDetail.enable);
        },

        cancelDialog(val) {
            this.udiImportDetailVisible = false;
            this.thrOrderSelectVisible = false;

            if (val) {
                this.getList();
            }
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        jumpDl() {
            window.open(this.templateDlUrl, '_blank');
        },
        deleteDialog(rowId) {
            this.$confirm("此操作将删除该业务单据信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let dQuery = {
                        id: rowId,
                    };
                    deleteLog(dQuery)
                        .then((response) => {
                            this.loading = false;
                            if (response.code == 20000) {
                                this.$message.success("删除成功");
                            } else {
                                this.$message.error(response.message);
                            }
                            this.getList();
                        })
                        .catch(() => {
                            this.loading = false;
                        });
                })
                .catch(() => {
                });
        },
        getBasicThirdSys() {
            let query = {
                enabled: true,
            };
            getBasicThirdSys(query)
                .then((response) => {
                    this.thirdSys = response.data.list || [];
                    this.filterQuery.thirdSysFk = this.thirdSys[0].thirdId;
                    this.uploadData.thirdSys = this.filterQuery.thirdSysFk;
                    this.getList();
                    this.getThirdSysDetail();
                    this.selectSysParam();
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
            axios.get("./config.json").then(res => {
                // 基础地址
                let response = res.data.BASE_URL;
                // this.uploadFileUrl = response + "/udiwms/thrOrder/importLog/upload";
                let response2 = res.data.SERVER_IP;
                this.templateDlUrl = response2 + "/第三方业务单据导入模板.xlsx";
                console.log(res.data.BASE_URL + "\n");
            });
        },

        selectSysParam() {
            let query = {
                key: "thirdIpUrl",
                thirdSysFk: this.filterQuery.thirdSysFk
            };
            selectIp(query).then((response) => {
                if (response.code == 20000) {
                    this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/order/upload";
                }
            });
        },

        formatterThirdSys(key) {
            let data = null;
            this.thirdSys.forEach((item, index) => {
                if (key.thirdSysFk == item.thirdId) {
                    data = item.thirdName;
                }
            });
            return data;
        },
        handleChange(response, files, fileList) {
            console.log(response);
            if (response.code != 20000) {
                this.$message.error(response.message);
            } else {
                // console.log(files[0] + "\n" + this.fileList[0] + "\n" + fileList[0]);
                this.$message.success("文件上传成功,请稍后刷新查看！");
                this.getList();
            }
        },
        statusFilterType(status) {
            const statusMap = {
                0: "warning",
                1: "warning",
                2: "danger",
                3: "success",
            };
            return statusMap[status];
        },
        getThirdSysDetail() {
            let query = {
                thirdSysFk: this.filterQuery.thirdSysFk,
                key: "orderQueryUrl",
            };
            filterDetailByKey(query)
                .then((response) => {
                    this.thirdSysDetail = response.data;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
            this.uploadData.thirdSys = this.filterQuery.thirdSysFk;

        },
        downloadOrderss() {
            this.selectType = 3;
            this.thrOrderSelectVisible = true;
        },

        downloadDatas() {
            this.$confirm("此操作从第三方系统下载全部业务单据信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let query = {
                    billAction: this.filterQuery.billAction,
                    thirdSysFk: this.filterQuery.thirdSysFk
                };
                downloadAll(query).then((response) => {
                    if (response.code == 20000) {
                        this.$message.success(response.data);
                    } else {
                        this.$message.error(response.message);
                    }
                });
            });


        },
        getBusType() {
            let query = {
                enabled: true,
            };
            getBussinessType(query)
                .then((response) => {
                    this.busTypes = response.data.list || [];
                    this.filterQuery.billAction = this.busTypes[0].action;
                })
                .catch(() => {
                });
        },
        formatter(row, index) {
            if (row.isE == true) {
                row.Registrationstate = "已注册"
            }
            if (row.Registrationstate == false) {
                row.Registrationstate = "未注册"
            }
            return row.Registrationstate
        }
    },

    mounted() {
    },
    components: {thrOrderImportDetail, thrOrderSelect},
    created() {
        this.getBasicThirdSys();
        this.getBusType();
        this.getList();
    },
};
</script>
<style>
.itemTag {
    float: left;
    text-align: left;
    margin-top: 10px;
    width: 100px;
}

.text {
    font-size: 13px;
    font-family: "Microsoft YaHei";
}

.el-row {
    display: flex;
    flex-wrap: wrap;

}

.el-col {
    border-radius: 4px;
    flex-wrap: wrap;
}
</style>
