<!-- 国家 -->
<template>
    <basic-container>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   :page.sync="page"
                   :permission="permissionList"
                   id="out-table"
                   :header-cell-class-name="headerClassName"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @row-del="rowDel"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @refresh-change="refreshChange"
                   @resetColumn="resetColumnTwo('crud', 'option', 'optionBack', 287)"
                   @saveColumn="saveColumnTwo('crud', 'option', 'optionBack', 287)"
                   @on-load="onLoad" >
            <template slot="remarks" slot-scope="scope">
                <avue-text-ellipsis :text="scope.row.remarks" :height="30">
                <small slot="more">...</small>
                </avue-text-ellipsis>
            </template>
            <template slot="menuLeft">
                <el-button type="danger"
                           size="small"
                           icon="el-icon-delete"
                           plain
                           v-if="permission.bcountrys_delete"
                           @click="handleDelete">删 除
                </el-button>
            </template>
        </avue-crud>
    </basic-container>
</template>

<script>
import {bcountrysList, bcountrysDetail, bcountrysSubmit, bcountrysRemove} from "@/api/iosBasicData/bcountrys";
import {mapGetters} from "vuex";

export default {
    data() {
        return {
            form: {},
            query: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0
            },
            selectionList: [],
            option:{},
            optionBack: {
                height:'auto',
                calcHeight: 30,
                tip: false,
                searchShow: true,
                searchMenuSpan: 6,
                border: true,
                index: true,
                viewBtn: true,
                selection: true,
                dialogClickModal: false,
                column: [
                    {
                        label: "编码",
                        prop: "code",
                        rules: [{
                            required: true,
                            message: "请输入编码",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "国际三字码",
                        prop: "unCode",
                        rules: [{
                            // max: 3,
                            required: true,
                            message: "请输入三个字符",
                            trigger: "blur"
                        },{
                            pattern:/^[a-zA-Z0-9]{1,3}$/,
                            message:"请输入英文字母或数字",
                            trigger:'blur'
                        }]
                    },
                    {
                        label: "中文名称",
                        prop: "cnName",
                        rules: [{
                            required: true,
                            message: "请输入中文名称",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "英文名称",
                        prop: "enName",
                        rules: [{
                            required: true,
                            message: "请输入英文名称",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "版本",
                        prop: "version",
                        rules: [{
                            required: true,
                            message: "请输入版本",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "状态",
                        prop: "status",
                        type:'select',
                        dicData:[{
                            label:'正常',
                            value:0
                        },{
                            label:'停用',
                            value:1
                        }],
                    },
                    {
                        label: "主键",
                        prop: "id",
                        hide:true,
                        display:false,
                    },
                    {
                        label: "创建人 Id",
                        prop: "createUser",
                        hide:true,
                        display:false,
                    },
                    {
                        label: "创建人",
                        prop: "createUserName",
                        display:false,
                    },
                    {
                        label: "创建部门 Id",
                        prop: "createDept",
                        hide:true,
                        display:false,
                    },
                    {
                        label: "创建部门",
                        prop: "createDeptName",
                        display:false,
                    },
                    {
                        label: "创建时间",
                        prop: "createTime",
                        hide:true,
                        display:false,
                    },
                    {
                        label: "修改人 Id",
                        prop: "updateUser",
                        hide:true,
                        display:false,
                    },
                    {
                        label: "修改人",
                        prop: "updateUserName",
                        display:false,
                    },
                    {
                        label: "修改时间",
                        prop: "updateTime",
                        display:false,
                    },
                    {
                        label: "是否已删除（0 否 1是）",
                        prop: "isDeleted",
                        hide:true,
                        display:false,
                    },
                    {
                        label: "备注",
                        prop: "remarks",
                        type: 'textarea',
                        width: "180",
                        slot: true,
                        span: 24,
                        minRows: 3,
                    },
                ]
            },
            data: []
        };
    },
    computed: {
        ...mapGetters(["permission"]),
        // 表格多个按钮权限控制，采用函数方式可以精确到行控制
        permissionList() {
            return {
                // addBtn: this.vaildData(this.permission.bcountrys_add, false),
                // viewBtn: this.vaildData(this.permission.bcountrys_view, false),
                // delBtn: this.vaildData(this.permission.bcountrys_delete, false),
                // editBtn: this.vaildData(this.permission.bcountrys_edit, false)
            };
        },
        ids() {
            let ids = [];
            this.selectionList.forEach(ele => {
                ids.push(ele.id);
            });
            return ids.join(",");
        }
    },
    async created() {
        this.option = await this.getColumnData(this.getColumnName(287), this.optionBack);
    },
    methods: {
        // 新增数据后点击确定触发该事件 （新增确认）
        rowSave(row, done, loading) {
            bcountrysSubmit(row).then(() => {
                this.onLoad(this.page);
                this.$message({
                    type: "success",
                    message: "操作成功!"
                });
                done();
            }, error => {
                loading();
                window.console.log(error);
            });
        },
        // 更新数据后确定触发该事件 （编辑确认）
        rowUpdate(row, index, done, loading) {
            bcountrysSubmit(row).then(() => {
                this.onLoad(this.page);
                this.$message({
                    type: "success",
                    message: "操作成功!"
                });
                done();
            }, error => {
                loading();
                console.log(error);
            });
        },
        // 行数据删除触发 （删除）
        rowDel(row) {
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    return bcountrysRemove(row.id);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                });
        },
        // 多选删除触发
        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning("请选择至少一条数据");
                return;
            }
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    return bcountrysRemove(this.ids);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    this.$refs.crud.toggleSelection();
                });
        },
        // 打开前的回调，会暂停Dialog的打开，done用于关闭Dialog,type为当前窗口的类型
        beforeOpen(done, type) {
            if (["edit", "view"].includes(type)) {
                bcountrysDetail(this.form.id).then(res => {
                    this.form = res.data.data;
                });
            }
            done();
        },
        // 清空搜索回调方法
        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        // 点击搜索后触发该事件(由于page分页信息和search搜索信息，可以直接通过this.page和this.search拿到)
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        // 当选择项发生变化时会触发该事件 （多选）
        selectionChange(list) {
            this.selectionList = list;
        },
        // 用于多选表格，传递数组进去，会勾选数组中的对象，如果已经勾选则会取消勾选
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        // 分页页数
        currentChange(currentPage){
            this.page.currentPage = currentPage;
        },
        // 分页条数
        sizeChange(pageSize){
            this.page.pageSize = pageSize;
        },
        // 点击刷新
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        // 获取列表数据
        onLoad(page, params = {}) {
            this.loading = true;
            bcountrysList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                const data = res.data.data;
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        },
        //自定义列保存
        async saveColumnTwo(ref, option, optionBack, code) {
            /**
             * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
             * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
             * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
             */
            const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
            if (inSave) {
                this.$message.success("保存成功");
                //关闭窗口
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },
        //自定义列重置
        async resetColumnTwo(ref, option, optionBack, code) {
            this[option] = this[optionBack];
            const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
            if (inSave) {
                this.$message.success("重置成功");
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },
        // 更改表格颜色
        headerClassName(tab){
            //颜色间隔
            let back = ""
            if (tab.columnIndex >= 0 && tab.column.level === 1) {
                if (tab.columnIndex % 2 === 0) {
                    back = "back-one"
                } else if (tab.columnIndex % 2 === 1) {
                    back = "back-two"
                }
            }
            return back;
        },

    }
};
</script>

<style lang="scss" scoped>

::v-deep#out-table .back-one {
        background: #ecf5ff !important;
    }
    ::v-deep#out-table .back-two {
        background: #ecf5ff !important;
    }

/deep/ .el-col-md-8 {
  width: 24.33333%;
}

</style>
