<div id="app" v-cloak>
    <el-card class="box-card">
        <el-form class="search" :inline="true" :model="vm" ref="vm" label-position="right" label-width="90px">
            <el-form-item label="角色代码" prop="code">
                <el-input v-model="vm.code" clearable size="mini" placeholder="请输入角色代码"></el-input>
            </el-form-item>
            <el-form-item label="角色名称" prop="name">
                <el-input v-model="vm.name" clearable size="mini" placeholder="请输入角色名称"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="comment">
                <el-input v-model="vm.comment" clearable size="mini" placeholder="请输入角色描述"></el-input>
            </el-form-item>
            <el-form-item label="开始日期" prop="startDate">
                <el-date-picker v-model="vm.startDate" clearable size="mini" placeholder="请输入开始日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
                <el-date-picker v-model="vm.endDate" clearable size="mini" placeholder="请输入结束日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="mini" icon="el-icon-search" @click="onSearch">搜索</el-button>
                <el-button type="warning" size="mini" icon="el-icon-refresh-left" @click="onReset('vm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card class="box-card data">
        <el-row>
            <el-col :span="12">
                <el-button type="success" size="mini" icon="el-icon-plus" @click="onCreate">新增</el-button>

                <el-button type="warning" size="mini" icon="el-icon-download" @click="onExport">导出</el-button>
            </el-col>

            <el-col :span="12">
                <el-button-group style="float: right;">
                    <el-tooltip effect="dark" content="Excel模板下载" placement="bottom">
                        <el-button size="mini" icon="el-icon-date" @click="onTemplate"></el-button>
                    </el-tooltip>

                    <el-tooltip effect="dark" content="Excel导入" placement="bottom">
                        <el-button size="mini" icon="el-icon-upload2" @click="onImport"></el-button>
                    </el-tooltip>

                    <el-tooltip effect="dark" content="批量删除" placement="bottom">
                        <el-button size="mini" icon="el-icon-delete" @click="onBitchDelete"></el-button>
                    </el-tooltip>

                    <el-tooltip effect="dark" content="刷新" placement="bottom">
                        <el-button size="mini" icon="el-icon-refresh" @click="onFind"></el-button>
                    </el-tooltip>
                </el-button-group>
            </el-col>
        </el-row>

        <el-table
                class="data"
                @selection-change="onSelectionChange"
                empty-text="无数据"
                :stripe="true"
                :data="result"
                size="mini">
            <el-table-column
                    align="center"
                    width="45"
                    type="selection">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="140"
                    prop="id"
                    label="主键">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="100"
                    prop="code"
                    label="角色代码">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="name"
                    label="角色名称">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="comment"
                    label="角色描述">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="140"
                    prop="createTime"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="120"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <wb-dropdown :arg="scope.row">
                        <wb-dropdown-item value="授权" icon="el-icon-edit" @click="onResEdit"></wb-dropdown-item>
                        <wb-dropdown-item value="编辑" icon="el-icon-edit" @click="onEdit"></wb-dropdown-item>
                        <wb-dropdown-item value="删除" icon="el-icon-delete" @click="onDelete"></wb-dropdown-item>
                    </wb-dropdown>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                background
                v-if="vm.totalCount > vm.pageSize"
                style="margin-top: 10px"
                @current-change="onPageChange"
                @size-change="onPageSizeChange"
                :current-page="vm.pageNumber"
                :page-size="vm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="vm.totalCount">
        </el-pagination>
    </el-card>

    <el-dialog
            :custom-class="'dialog'"
            :title="form.formTitle"
            :close-on-click-modal="false"
            :visible.sync="form.formShow">
        <el-form class=form" :model="form" :inline="true" :rules="formRules" ref="form"
                 label-position="right" label-width="90px">
            <el-form-item label="角色代码" prop="code">
                <el-input v-model="form.code" clearable size="mini" placeholder="请输入角色代码"></el-input>
            </el-form-item>
            <el-form-item label="角色名称" prop="name">
                <el-input v-model="form.name" clearable size="mini" placeholder="请输入角色名称"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="comment">
                <el-input v-model="form.comment" clearable size="mini" placeholder="请输入角色描述"></el-input>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
			<el-button size="mini" @click="form.formShow = false">取 消</el-button>
			<el-button size="mini" type="primary" @click="onSave">保存</el-button>
		</span>
    </el-dialog>

    <el-dialog class="form-dialog" :title="formResource.title" :visible.sync="formResource.dialog">
        <el-tree
                :data="treeData"
                show-checkbox
                node-key="id"
                ref="tree"
                @check="onCheck"
                :check-strictly="true"
                :default-expand-all="true"
                :expand-on-click-node="false">
            <div class="tree" slot-scope="{ node, data }">
                <span>{{ data.resName }}<span style="color: #409EFF">[{{data.resCode}}]</span></span>
            </div>
        </el-tree>

        <span slot="footer" class="dialog-footer">
			<el-button size="mini" @click="formResource.dialog = false">关 闭</el-button>
		</span>
    </el-dialog>
</div>
<script>
    var app = new Vue({
        mixins: [mixin],
        el: "#app",
        data: {
            module: 'wsys',
            target: 'role',
            vm: {//条件及分页参数
                code: null,
                name: null,
                comment: null,
                startDate: null,
                endDate: null,
                pageNumber: 1,
                pageSize: 10,
                totalCount: 0,
                sortKey: 'CREATE_TIME',
                sortType: 'DESC'
            },
            form: {//待提交表单
                formTitle: null,
                formShow: false,
                id: null,
                code: null,
                name: null,
                comment: null,
                rowVersion: null
            },
            formResource: {
                title: null,
                dialog: false,
                roleId: null,
                roleCode: null
            },
            formRules: {
                code: [
                    {required: true, message: '角色代码不能为空', trigger: 'blur'},
                    {min: 1, max: 50, message: '角色代码长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                name: [
                    {required: true, message: '角色名称不能为空', trigger: 'blur'},
                    {min: 1, max: 50, message: '角色名称长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                comment: [
                    {min: 1, max: 50, message: '角色描述长度在 1 到 50 个字符', trigger: 'blur'}
                ],
            },
            treeData: []
        },
        methods: {
            onCreate: function () {
                this.form.formTitle = "角色新增";
                this.form.formShow = true;
                this.form.id = "";
                this.form.code = "";
                this.form.name = "";
                this.form.comment = "";
            },
            onEdit: function (item) {
                this.form.formTitle = "角色编辑";
                this.form.formShow = true;
                this.form.id = item.id;
                this.form.code = item.code;
                this.form.name = item.name;
                this.form.comment = item.comment;
                this.form.rowVersion = item.rowVersion;
            },
            onDelete: function (item) {
                this.$confirm('将删除该项, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    new Ajax("wsys", "role").delete({id: item.id}, function (response) {
                        if (response.errors.length > 0) {
                            nav.e(response.errors[0].message);
                        } else {
                            nav.s("删除成功");
                            this.onFind();
                        }
                    }.bind(this))
                }.bind(this)).catch(function (action) {

                });
            },
            onResEdit: function (item) {
                this.formResource.title = "分配资源";
                this.formResource.dialog = true;
                this.formResource.roleId = item.id;
                this.formResource.roleCode = item.code;
                new Ajax("wsys", "roleRes").find({
                    roleId: item.id,
                    pageSize: 0
                }, function (response) {
                    if (response.errors.length > 0) {
                        nav.e(response.errors[0].message);
                    } else {
                        this.$refs.tree.setCheckedKeys([]);
                        var ids = [];
                        for (var i = 0; i < response.result.length; i++) {
                            ids.push(response.result[i].resId);
                        }
                        this.$refs.tree.setCheckedKeys(ids);
                    }
                }.bind(this))
            },

            onCheck: function (item, status) {
                var roleId = this.formResource.roleId;
                var roleCode = this.formResource.roleCode;
                var isChecked = status.checkedKeys.indexOf(item.id) !== -1;

                this.doCheck(roleId, roleCode, item.id, item.resCode, isChecked);

                (function fetch(children, _this) {
                    if (children && children.length > 0) {
                        for (var i = 0; i < children.length; i++) {
                            var obj = children[i];
                            if ((status.checkedKeys.indexOf(obj.id) !== -1) !== isChecked && !obj.disabled) {
                                _this.doCheck(roleId, roleCode, obj.id, obj.resCode, isChecked);
                            }
                            fetch(obj.children, _this);
                        }
                    }
                })(item.children, this);
            },
            doCheck: function (roleId, roleCode, resId, resCode, checked) {
                // 优先处理当前节点
                new Ajax("wsys", "roleRes").method("check").post({
                    roleId: roleId,
                    roleCode: roleCode,
                    resId: resId,
                    resCode: resCode,
                    checked: checked,
                }, function (response) {
                    if (response.errors.length > 0) {
                        nav.e(response.errors[0].message);
                    } else {
                        this.$refs.tree.setChecked(resId, checked);
                    }
                }.bind(this));
            },
            onLoadTree: function () {
                new Ajax("wsys", "res").method("tree").post({}, function (response) {
                    if (response.errors.length > 0) {
                        nav.e(response.errors[0].message);
                    } else {
                        this.treeData = response.result;
                        for (var i = 0; i < response.result.length; i++) {
                            this.treeData[i].disabled = this.treeData[i].free;
                        }
                    }
                }.bind(this))
            },
        },
        mounted: function () {
            this.onFind();
            this.onLoadTree();
        }
    })
</script>
