<template>
    <div>
        <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.name"
                        placeholder="业务类型"
                        style="width: 400px"
                    ></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="search">查询</el-button>
                        <el-button type="primary" icon="search" @click="handleAddClick"
                                   :disabled="!configParms.typeBus"
                        >新增
                        </el-button
                        >
                    </el-button-group>

                </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="list" style="width: 100%">
                <el-table-column label="序号" type="index" width="60" fixed></el-table-column>
                <el-table-column label="业务单据类型名称" prop="name" fixed></el-table-column>
                <el-table-column label="业务单据类型代码" prop="action" fixed></el-table-column>
                <el-table-column label="第三方单据类型" prop="originName" fixed></el-table-column>
                <el-table-column label="是否寄售" prop="advanceType" fixed>
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.advanceType] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="预验收" prop="preIn" fixed>
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.preIn] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="缺量自动补单" prop="advanceType" fixed>
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.changeEnable] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="允许供应商使用" prop="spUse" fixed>
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.spUse] }}</span>
                    </template>
                </el-table-column>


                <el-table-column label="操作" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small" 
                            @click.native.stop="handleModifyClick(scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            :disabled="!configParms.typeBus"
                            @click.native.stop="deleteDialog(scope.row)"
                        >删除
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <el-dialog
                title="新增业务类型关联"
                :visible.sync="addDialogVisible"
                width="70%"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                v-if="addDialogVisible"
            >
                <modifyDialog :inputQuery="inputQuery"></modifyDialog>
                <div style="text-align: center">
                    <el-button type="primary" size="small" icon="search" @click="onAddSubmit"
                               :disabled="!configParms.typeBus"
                    >提交
                    </el-button
                    >
                    <el-button type="primary" size="small" icon="search" @click="cancelDialog"
                    >取消
                    </el-button
                    >
                </div>
            </el-dialog>

            <el-dialog
                title="编辑业务类型关联"
                :visible.sync="modifyDialogVisible"
                width="70%"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                v-if="modifyDialogVisible"
            >
                <modifyDialog :inputQuery="inputQuery"></modifyDialog>
                <div style="text-align: center">
                    <el-button type="primary" size="small" icon="search" @click="onModifySubmit"
                               :disabled="!configParms.typeBus"
                    >提交
                    </el-button
                    >
                    <el-button type="primary" size="small" icon="search" @click="cancelDialog"
                    >取消
                    </el-button
                    >
                </div>
            </el-dialog>

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

import {getLocalBusType, addLocalBusType, updateLocalBusType, deleteLocalBusType} from "@/api/basic/busLocalType";
import modifyDialog from "./BussinessTypeLoclModify";
import {findConfig} from "@/api/thrsys/spsSyncStatus";
import {isBlank} from "@/utils/strUtil";

export default {
    data() {
        return {
            filterQuery: {
                enabled: "1",
                udiwmsType: "",
                thirdType: "",
                page: 1,
                limit: 20,
            },
            addDialogVisible: false,
            modifyDialogVisible: false,
            list: [],
            inputQuery: {
                action: "",
                name: "",
                enable: "",
                remark: "",
                mainAction: "",
                thirdSysFk: "",
                id: "",
                originAction: null,
                localAction: null,
                advanceType: null,
                changeEnable: null,
                spUse: null,
                prefix: null,
                preIn: null,
                supplementAll: false,
            },
            enableMap: {
                true: "是",
                false: "否",
            },

            total: 0,
            multipleSelection: [],
            configParms: {},
        };
    },

    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                udiwmsType: "",
                thirdType: "",
                page: 1,
                limit: 20,
            };
            this.getList();
        },
        cancelDialog() {
            this.modifyDialogVisible = false;
            this.addDialogVisible = false;
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        search() {
            this.filterQuery.page = 1;
            this.getList();
        },
        getList() {
            this.loading = true;
            getLocalBusType(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;
                });
        },


        onAddSubmit() {
            if (isBlank(this.inputQuery.name)) {
                this.$message.warning("业务单据类型名称为空");
                return;
            }
            addLocalBusType(this.inputQuery)
                .then((response) => {
                    this.loading = false;
                    if (response.code == 20000) {
                        this.cancelDialog();
                        this.getList();
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                    this.loading = false;
                    this.cancelDialog();
                });
        },

        onModifySubmit() {
            updateLocalBusType(this.inputQuery)
                .then((response) => {
                    this.loading = false;
                    this.cancelDialog();
                    this.getList();
                })
                .catch(() => {
                    this.loading = false;
                    this.cancelDialog();
                });
        },

        handleModifyClick(row) {
            this.modifyDialogVisible = true;
            this.inputQuery = {
                id: row.id,
                remark: row.remark,
                action: row.action,
                name: row.name,
                enable: row.enable,
                mainAction: row.mainAction,
                localAction: row.localAction,
                thirdSysFk: row.thirdSysFk,
                originAction: row.originAction,
                advanceType: row.advanceType,
                changeEnable: row.changeEnable,
                spUse: row.spUse,
                prefix: row.prefix,
                preIn: row.preIn,
                supplementAll: row.supplementAll,
            };
        },

        handleAddClick() {
            this.addDialogVisible = true;
            this.inputQuery = {
                action: "",
                name: "",
                enable: "",
                remark: "",
                mainAction: null,
                thirdSysFk: "",
                id: null,
                advanceType: null,
                localAction: null,
                changeEnable: null,
                prefix: null,
                preIn: null,
            };
        },

        deleteDialog(rowId) {
            this.$confirm("此操作将永久删除该业务类型信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.deleteOrders(rowId);
                })
                .catch(() => {
                });
        },

        deleteOrders(data) {
            this.loading = true;
            let tquery = {
                id: data.id + "",
            };

            deleteLocalBusType(tquery)
                .then((response) => {
                    this.getList();
                    if (response.code == 20000) {
                        this.$message({
                            type: "success",
                            message: "删除成功！",
                        });
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                });
        },
        getSyncConfig() {
            findConfig()
                .then((response) => {
                    if (response.code == 20000) {
                        this.configParms = response.data;
                    }
                })
                .catch(() => {
                });
        },
    },
    components: {
        modifyDialog,
    },
    mounted() {
    },
    created() {
        this.getSyncConfig();
        this.getList();
    },
};
</script>
