<template>
    <div>
        <el-card>
            <el-form :inline="true" :model="filterQuery" size="mini">
                <el-row style="width: 100%">
                    <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" :disabled="!addBtnEnabled" @click="addSetting"
                            >添加盘点设置
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>
            <el-table v-loading="loading" :data="list" style="width: 100%"
                      highlight-current-row="true"
            >
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="盘盈入库单据类型" prop="inBusTypeName" width="150"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column label="盘亏入库单据类型" prop="outBusTypeName" width="150"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="handleEdit(scope.row)"
                        >编辑
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

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

        <el-dialog
            :title="formMap[formName]"
            :visible.sync="settingFormVisible"
            width="50%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="settingFormVisible"
            append-to-body
        >
            <el-form :model="formData" ref="dataForm" label-width="180px">
                <el-form-item label="盘盈入库单据类型：">
                    <el-select v-model="formData.inAction" placeholder="请选择单据类型">
                        <el-option
                            v-for="item in inBusList"
                            :key="item.name"
                            :label="item.name"
                            :value="item.action">
                            <span style="float: left">{{ item.name }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="盘亏入库单据类型：">
                    <el-select v-model="formData.outAction" placeholder="请选择单据类型">
                        <el-option
                            v-for="item in outBusList"
                            :key="item.name"
                            :label="item.name"
                            :value="item.action">
                            <span style="float: left">{{ item.name }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="settingFormVisible = false">取消 </el-button>
                <el-button type="primary" size="small" @click.native="save">提交</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {getSettingList, saveCountSetting, verifyAdd} from "@/api/inventory/invCountSetting";
import {getJoinBussinessType} from "@/api/basic/bussinessType";

export default {
    data() {
        return {
            filterQuery: {
                id: null,
                page: 1,
                limit: 20,
            },
            list: [],
            total: 0,
            loading: true,
            formName: null,
            formMap: {
                add: "添加盘点设置参数",
                edit: "编辑盘点设置参数"
            },
            settingFormVisible: false,
            addBtnEnabled: false,
            formData: {
                inAction: null,
                outAction: null
            },
            inBusList: [],
            outBusList: []
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                id: null,
                page: 1,
                limit: 20,
            };
            this.invChange();
            this.getList();
        },
        onSubmit() {
            this.loading = true;
            this.filterQuery.page = 1;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        handleEdit(row) {
            this.settingFormVisible = true;
            this.formName = 'edit';
            this.formData = row;
        },
        addSetting() {
            this.settingFormVisible = true;
            this.formName = 'add';
            this.formData = {
                invAction: null,
                outAction: null
            }
        },
        getList() {
            this.loading = true;
            let params = {};
            getSettingList(params).then((res) => {
                this.loading = false;
                if (res.code === 20000) {
                    this.list = res.data.list || [];
                    this.total = res.data.total || 0;
                }
            }).catch((error) => {
                this.loading = false;
                this.list = [];
                this.total = 0;
            })
        },
        save() {
            saveCountSetting(this.formData, this.formName).then((res) => {
                if (res.code === 20000) {
                    this.settingFormVisible = false;
                    this.getList();
                    this.addBtnEnabled = false;
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        getInBusType() {
            let params = {
                enable: true,
                mainAction: "WareHouseIn"
            }
            getJoinBussinessType(params).then((res) => {
                this.inBusList = res.data.list || [];
            })
        },
        getOutBusType() {
            let params = {
                enable: true,
                mainAction: "WareHouseOut"
            };
            getJoinBussinessType(params).then((res) => {
                this.outBusList = res.data.list || [];
            })
        },
    },
    created() {
        this.getList();
        this.getInBusType();
        this.getOutBusType();
        //获取用户默认仓库信息
        verifyAdd().then((res) => {
            if (res.code === 20000) {
                this.addBtnEnabled = true;
            }
        });
    },
};
</script>

<style type="text/scss" lang="scss">
</style>
