<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.originName"
                        placeholder="单据类型名称"
                        style="width: 200px"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        v-model="filterQuery.originAction"
                        placeholder="单据类型代码"
                        style="width: 200px"
                    ></el-input>
                </el-form-item>
<!--                <el-form-item class="query-form-item">-->
                <!--                    <el-select v-model="filterQuery.type">-->
                <!--                        <el-option v-for="(item,index) in typeMap" :key="index" :label="item" :value="index">-->
                <!--                        </el-option>-->
                <!--                    </el-select>-->
                <!--                </el-form-item>-->
                <el-form-item>
                    <el-button-group style="margin-left: 10px;display:flex;">
                        <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"
                        >新增
                        </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="55"></el-table-column>
                <el-table-column label="领用类型名称" prop="originName"></el-table-column>
                <el-table-column label="领用类型代码" prop="originAction"></el-table-column>
                <el-table-column label="目标业务单据类型" prop="targetBusName"></el-table-column>
                <el-table-column label="类型" prop="type">
                    <template slot-scope="scope">
                        <span>{{ typeMap[scope.row.type] }}</span>
                    </template>

                </el-table-column>
                <el-table-column label="状态" prop="enable">
                    <template slot-scope="scope">
                        <span>{{ enableMap[scope.row.enable] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark"></el-table-column>
                <el-table-column label="操作" width="100" 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="scope.row.systemDefault"
                            @click.native.stop="handleDeleteClick(scope.row)"
                        >删除
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <el-dialog
                :title="formMap[formName]"
                :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"
                    >提交
                    </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 {
    getBusChange,
    updateBusChange, delBusChange,
    insertBusChange
} from "../../api/basic/busTypeChange";

import modifyDialog from "./OtherTypeChangeModify";
import axios from "axios";

export default {
    data() {
        return {
            filterQuery: {
                name: "",
                page: 1,
                limit: 20,
            },
            addDialogVisible: false,
            list: [],
            inputQuery: {},
            enableMap: {
                true: "禁用",
                false: "启用",
            },
            typeMap: {
                1: "耗材领用",
                2: "产品申购",
                3: "采购计划",
            },
            total: 0,
            loading: false,
            configParms: {},
            formMap: {
                add: "新增单据流转设置",
                update: "编辑单据流转设置",
            },
            formName: "add",
        };
    },

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

        onAddSubmit() {
            if (this.$isBlank(this.inputQuery.originName)) {
                this.$message.error("单据类型名称不能为空！");
                return;
            }
            if (this.$isBlank(this.inputQuery.originAction)) {
                this.$message.error("单据类型代码不能为空！");
                return;
            }
            if (this.$isBlank(this.inputQuery.targetBusAction)) {
                this.$message.error("目标单据类型不能为空！");
                return;
            }
            // if (this.$isBlank(this.inputQuery.type)) {
            //     this.$message.error("请选择设置类型！");
            //     return;
            // }
            if (this.$isBlank(this.inputQuery.enable)) {
                this.$message.error("请选择启用状态！");
                return;
            }
            if (this.formName == "add") {

                let numRegExp = '^[0-9]*$';
                let numReg = new RegExp(numRegExp);
                if (numReg.test(this.inputQuery.name)) {
                    this.$message.error("单据类型名称不得为纯数字类型！");
                    return;
                }
                this.inputQuery.type = 1;           //默认类型为耗材领用
                insertBusChange(this.inputQuery)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.loading = false;
                            this.cancelDialog();
                            this.getList();
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.loading = false;
                        this.cancelDialog();
                    });
            } else {
                updateBusChange(this.inputQuery)
                    .then((response) => {
                        this.loading = false;
                        this.cancelDialog();
                        this.getList();
                    })
                    .catch(() => {
                        this.loading = false;
                        this.cancelDialog();
                    });
            }


        },


        handleAddClick() {
            this.inputQuery = {};
            this.formName = "add";
            this.addDialogVisible = true;
        },
        handleModifyClick(row) {

            this.inputQuery = row;
            this.formName = "update";
            this.addDialogVisible = true;
        },


        handleDeleteClick(row) {
            this.deleteDialog(row.id)
        },


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


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

        thirdSysChange() {
            this.getList();
        },

    },
    components: {
        modifyDialog,
    },
    mounted() {
    },
    created() {
        this.getList();

    },
};
</script>
