<template>
    <div>
        <el-card class="el-card">
            <el-form :inline="true" :model="filterQuery"
                     class="query-form" size="mini">
                <el-form-item class="query-form-item">
                    <el-input
                        v-model="filterQuery.billNo"
                        placeholder="单据号"
                        clearable
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.action" placeholder="请选择业务类型">
                        <el-option
                            v-for="item in busTypes"
                            :key="item.name"
                            :label="item.name"
                            :value="item.action">
                            <span style="float: left">{{ item.name }}</span>
                            <!--<span style="float: right; color: #8492a6; font-size: 13px">{{ item.action }}</span>-->
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.thirdSysFk" placeholder="请选择第三方系统">
                        <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-button type="primary" icon="search" @click="postSmpOrder">同步至自助平台</el-button>

                    </el-button-group>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row="true"
                      @current-change="handleDetail"
                      @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="序号" type="index" width="60"></el-table-column>
                <el-table-column label="单据号" prop="billNo"></el-table-column>
                <el-table-column label="往来单位ID" prop="corpId" width="100"></el-table-column>
                <el-table-column label="往来单位" prop="corpName" width="300"
                                 show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="扫码单据类型" prop="billType">

                    <template slot-scope="scope">
                        <span>{{ geActionName(scope.row.billType) }}</span>
                    </template>

                </el-table-column>
                <el-table-column label="单据状态" prop="billFlag" width="80"></el-table-column>
                <el-table-column label="单据日期" prop="billdate"></el-table-column>
                <el-table-column label="单据类型" prop="originType"></el-table-column>
            </el-table>
            <el-pagination
                :page-size="filterQuery.limit"
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="total"
                :current-page="filterQuery.page"
            ></el-pagination>
        </el-card>
        <el-card class="el-card">
            <el-table v-loading="loading" :data="detailList" style="width: 100%">
                <el-table-column label="序号" type="index" width="120"></el-table-column>
                <el-table-column label="产品编码" prop="productId"></el-table-column>
                <el-table-column label="产品名称" prop="productName" show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="规格型号" prop="spec" show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="批次号" prop="batchNo"></el-table-column>
                <el-table-column label="生产日期" prop="productDate"></el-table-column>
                <el-table-column label="失效日期" prop="expireDate"></el-table-column>
                <el-table-column label="单据数量" prop="reCount"></el-table-column>
                <el-table-column label="实际数量" prop="count"></el-table-column>
            </el-table>
        </el-card>
    </div>
</template>
<script>
    import {postOrder} from "../../api/thrsys/smp"
    import {getBussinessType} from "../../api/basic/bussinessType";
    import {getBasicThirdSys, filterDetailByKey} from "../../api/basic/basicThirdSys";
    import {selectIp} from "../../api/param/systemParamConfig";
    import {
        getCloudErp,
    } from "../../api/inout/erpOrder";

    export default {
        data() {
            return {
                filterQuery: {
                    billNo: "",
                    thirdSysFk: "",
                    page: 1,
                    limit: 10,
                },
                total: 0,
                thirdSys: [],
                thirdSysDetail: null,
                busTypes: [],
                list: [],
                detailList: [],
                uploadFileUrl: null,
                fileList: [],
                uploadData: {
                    thirdSys: "thirdId",
                },
                multipleSelection: [],
            };
        },

        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.filterQuery = {
                    billNo: "",
                    thirdSysFk: "",
                    billFlag: null,
                    action: null,
                    startDate: null,
                    endDate: null,
                    page: 1,
                    limit: 10,
                };
                this.getList();
            },
            handleSelectionChange(val) {
                console.log("----" + val);
                this.multipleSelection = val;
            },
            getList() {
                this.loading = true;
                getCloudErp(this.filterQuery)
                    .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;
                    });
            },

            postSmpOrder() {
                let selectData = this.multipleSelection;
                var ids = [];
                selectData.forEach((obj) => {
                    ids.push(obj.id);
                });
                let query = {
                    ids: ids,
                }
                postOrder(query)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.$message.success(response.data);
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                    });
            },
            handleDetail(row) {
                this.detailList = row.subErpOrders;
            },
            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.selectSysParam();
                        this.getThirdSysDetail();
                        this.getList();
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });
            },
            handleCurrentChange(val) {
                this.filterQuery.page = val;
                this.getList();
            },
            selectSysParam() {
                let query = {
                    key: "thirdIpUrl",
                    thirdSysFk: this.filterQuery.thirdSysFk
                };
                selectIp(query).then((response) => {
                    if (response.code == 20000) {
                        this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/pi/upload";
                    }
                });
            },
            thirdSysChange() {
                this.uploadData.thirdSys = this.filterQuery.thirdSysFk;
                this.getThirdSysDetail();
            },
            getThirdSysDetail() {
                let query = {
                    thirdSysFk: this.filterQuery.thirdSysFk,
                    key: "orderQueryUrl",
                };
                filterDetailByKey(query)
                    .then((response) => {
                        this.thirdSysDetail = response.data;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });

            },
            getBusType() {
                let query = {
                    enabled: true,
                };
                getBussinessType(query)
                    .then((response) => {
                        this.busTypes = response.data.list || [];
                        this.filterQuery.action = this.busTypes[0].action;
                    })
                    .catch(() => {
                    });
            },
            geActionName(action){
                for (let i = 0; i < this.busTypes.length; i++) {
                    if (this.busTypes[i].action === action) {
                        return this.busTypes[i].name;
                    }
                }
            },

        },
        components: {},
        mounted() {
        },
        created() {
            this.getBasicThirdSys();
            this.getBusType();
        },
    };
</script>
