<template>
    <ap-container>
        <ap-header margin="0111" style="border-bottom: 1px solid #e2e7ef;">
            <el-form ref="tSysJobStandardSearchForm" :model="tSysJobStandardSearchForm"
                     :rules="tSysJobStandardSearchFormRules"
                     :inline="true" class="layout-header-form" @submit.native.prevent="searchEvent">
                <el-form-item label="搜索:" class="layout-header-form-item" prop="likeStr">
                    <el-input v-model="tSysJobStandardSearchForm.likeStr" placeholder="名称/编码"></el-input>
                </el-form-item>
                <el-form-item label="是否有效:" class="layout-header-form-item" prop="fdDisabled">
                    <el-select v-model='tSysJobStandardSearchForm.fdDisabled'>
                        <el-option key="" label="全部" value=""></el-option>
                        <el-option :key="0" label="有效" :value="0"></el-option>
                        <el-option :key="1" label="失效" :value="1"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-button type="primary" @click="searchEvent">查询</el-button>
            <ap-button type="primary" :click="syncJobStandardInfo" style="display: none">更新EHR基准岗位</ap-button>
        </ap-header>
        <ap-main margin="0101">
            <ap-table ref="tSysJobStandardRef" :options.sync="tSysJobStandardOpt"></ap-table>
        </ap-main>
        <el-dialog :title="batchGroupDialogInfo.title" :visible.sync="batchGroupDialogInfo.isVisible"
                   @close="resetTSysJobStandardBatchGroupForm"
                   width="600px">
            <el-form ref="tSysJobStandardBatchGroupFormRef" :model="tSysJobStandardBatchGroupForm"
                     :rules="tSysJobStandardBatchGroupFormRules"
                     v-loading="batchGroupDialogInfo.formIsLoading"
                     label-width="80px">
                <el-form-item label='群组' prop='fdGroupId'>
                    <ap-search-tree :options.sync="tSysJobStandardFormStandardGroupOptions"></ap-search-tree>
                </el-form-item>
            </el-form>

            <span slot="footer">
                <el-button @click="batchGroupDialogInfo.isVisible = false">取 消</el-button>
                <el-button type="primary" @click="batchSaveTSysJobStandardGroup"
                           :loading="batchGroupDialogInfo.saveBtnIsLoading">确 定</el-button>
            </span>
        </el-dialog>
    </ap-container>
</template>

<script>
    export default {
        name: 'SysJobStandard',
        data() {
            let columns = [
                {
                    prop: 'fdName',
                    label: '名称',
                    type: 'input',
                    rules: [{required: true, message: "请输入基准岗位名称"},
                        {min: 1, max: 20, message: '长度在 1 到 20 个字符'}]
                }, {
                    prop: 'fdParentName',
                    label: '父岗位',
                    type: 'searchTree',
                    searchTreeUrl: "/apm/sys/jobStandard/getTSysJobStandardList",
                    searchTreeParams: {
                        fdDisabled: 0
                    },
                    searchTreeRenderKey: {idKey: "fdId"},
                    searchTreeLoadTime: "edit",
                    searchTreeConfirm: (row, checkedNodes) => {
                        row.fdParentId = checkedNodes[0].fdId;
                        row.fdParentName = checkedNodes[0].fdName;
                        return true;
                    },
                    searchTreeClear: (row) => {
                        row.fdParentId = "";
                        row.fdParentName = "";
                        return true;
                    },
                }, {
                    prop: 'fdCode',
                    label: '编码',
                    width: 300,
                    type: 'input',
                    rules: [{required: true, message: "请输入基准岗位编码"},
                        {min: 1, max: 50, message: '长度在 1 到 50 个字符'}]
                }, {
                    prop: 'fdLevel',
                    label: '层级',
                    width: 150,
                    type: 'input'
                }, {
                    prop: 'fdGroupName',
                    label: '所属群组',
                    type: 'searchTree',
                    searchTreeUrl: "/apm/sys/sysJobStandardGroup/getTSysJobStandardGroupList",
                    searchTreeParams: {
                        fdDisabled: 0
                    },
                    searchTreeRenderKey: {idKey: "fdId"},
                    searchTreeLoadTime: "edit",
                    searchTreeConfirm: (row, checkedNodes) => {
                        row.fdGroupId = checkedNodes[0].fdId;
                        row.fdGroupName = checkedNodes[0].fdName;
                        return true;
                    },
                    searchTreeClear: (row) => {
                        row.fdGroupId = "";
                        row.fdGroupName = "";
                        return true;
                    },
                }, {
                    prop: 'fdDisabled',
                    label: '是否有效',
                    type: 'select',
                    align: "center",
                    width: 80,
                    addDefaultValue: 0,
                    selectOptions: [{label: "有效", value: 0}, {label: "失效", value: 1}],
                    html: function (value, row) {
                        if (value == 1) {
                            return '<div class="ap-tag-danger">禁用</div>'
                        } else if (value == 0) {
                            return '<div class="ap-tag-success">启用</el-tag>';
                        }
                    },
                    rules: [{required: true, message: "请选择是否有效"}]
                },
            ];

            let toolbarBtn = [{
                btnType: "primary",
                text: "批量设置群组",
                icon: "el-icon-setting",
                onClick: this.doBatchSetGroupDialog
            }, "separator", "add", "edit", "del", "cancel", "refresh", "save"];

            let tableOpt = {
                title: "基准岗位表",
                columns: columns,
                editPk: "fdId", //默认fdId
                deletePk: "fdId", // 默认fdId
                dataUrl: "/apm/sys/jobStandard/getTSysJobStandardGridList",
                saveUrl: "/apm/sys/jobStandard/saveTSysJobStandardGridData",
                deleteUrl: "/apm/sys/jobStandard/deleteTSysJobStandardGridData",
                toolbarBtn: toolbarBtn,
                initData: true, // 默认false
                pagination: {pageSizes: [100, 1000, 3000], pageSize: 100},
                rowParentKey: "fdParentId",
                defaultExpandAll: true,
                indexWidth: 200,
                indexAlign: "left",
                params: () => {
                    return {
                        likeStr: this.tSysJobStandardSearchForm.likeStr,
                        fdDisabled: this.tSysJobStandardSearchForm.fdDisabled,
                    }
                }
            };
            let tSysJobStandardFormStandardGroupOptions = {
                searchTreeText: "",
                searchTreeValue: "",
                searchTreeUrl: "/apm/sys/sysJobStandardGroup/getTSysJobStandardGroupList",
                searchTreeParams: {
                    fdDisabled: 0
                },
                searchTreeConfirm: (checkedNodes) => {
                    // 修改组件显示值
                    this.tSysJobStandardFormStandardGroupOptions.searchTreeValue = checkedNodes[0].fdId;
                    this.tSysJobStandardFormStandardGroupOptions.searchTreeText = checkedNodes[0].fdName;
                    // 修改表单值
                    this.tSysJobStandardBatchGroupForm.fdGroupId = checkedNodes[0].fdId;
                    this.tSysJobStandardBatchGroupForm.fdGroupName = checkedNodes[0].fdName;
                    return true;
                },
                searchTreeClear: () => {
                    this.tSysJobStandardFormStandardGroupOptions.searchTreeValue = "";
                    this.tSysJobStandardFormStandardGroupOptions.searchTreeText = "";

                    this.tSysJobStandardBatchGroupForm.fdGroupId = "";
                    this.tSysJobStandardBatchGroupForm.fdGroupName = "";
                    return true;
                },
            };

            return {
                tSysJobStandardOpt: tableOpt,
                tSysJobStandardSearchForm: {
                    likeStr: "",
                    fdDisabled: 0
                },
                tSysJobStandardSearchFormRules: {

                    likeStr: [{min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}]
                },
                // 批量设置群组
                tSysJobStandardBatchGroupForm: {
                    standardIdList: [],
                    fdGroupId: "",
                    fdGroupName: ""
                },
                // 基准岗位选择
                tSysJobStandardFormStandardGroupOptions: tSysJobStandardFormStandardGroupOptions,
                tSysJobStandardBatchGroupFormRules: {},
                batchGroupDialogInfo: {
                    title: "",
                    isVisible: false,
                    saveBtnIsLoading: false,
                    formIsLoading: false
                },
            }
        },
        methods: {
            // 查询
            searchEvent() {
                // 表单rules认证
                this.$refs.tSysJobStandardSearchForm.validate((valid) => {
                    if (!valid) {
                        this.$message.error('搜索条件格式不正确！');
                        return false;
                    }
                    this.$refs.tSysJobStandardRef.refresh();
                });
            },
            syncJobStandardInfo() {
                return new Promise((resolve) => {
                    this.$request({
                        url: '/apm/sys/jobStandard/syncJobStandard',
                        method: 'post',
                    }).then(response => {
                        this.$message.success(`基准岗位资料更新完成！`);
                        this.$refs.tSysJobStandardRef.refresh();
                    }).finally(() => {
                        resolve();
                    });
                });
            },
            doBatchSetGroupDialog() {
                // 获取选择项目
                let selectRowList = this.$refs.tSysJobStandardRef.getSelection();
                if (selectRowList == undefined || selectRowList.length <= 0) {
                    this.$message.info(`至少选择一个标准岗位进行操作！`);
                    return;
                }
                let standardIdList = [];
                for (let i = 0; i < selectRowList.length; i++) {
                    if (selectRowList[i].fdId != undefined && selectRowList[i].fdId != null) {
                        standardIdList.push(selectRowList[i].fdId);
                    }
                }
                this.tSysJobStandardBatchGroupForm.standardIdList = standardIdList;

                this.batchGroupDialogInfo.title = "批量设置群组";
                this.batchGroupDialogInfo.isVisible = true;
            },
            resetTSysJobStandardBatchGroupForm() {
                this.tSysJobStandardFormStandardGroupOptions.searchTreeValue = "";
                this.tSysJobStandardFormStandardGroupOptions.searchTreeText = "";

                this.$refs.tSysJobStandardBatchGroupFormRef.resetFields();
            },
            batchSaveTSysJobStandardGroup() {
                // 表单rules认证
                this.$refs.tSysJobStandardBatchGroupFormRef.validate((valid) => {
                    if (!valid) {
                        this.$message.error('数据校验错误！');
                        return false;
                    }
                    this.batchGroupDialogInfo.saveBtnIsLoading = true;

                    // 添加数据
                    this.$request({
                        url: '/apm/sys/jobStandard/batchSetTSysJobStandardGroupData',
                        method: 'post',
                        data: {
                            standardIdList: JSON.stringify(this.tSysJobStandardBatchGroupForm.standardIdList),
                            fdGroupId: this.tSysJobStandardBatchGroupForm.fdGroupId
                        }
                    }).then(response => {
                        this.$message.success(`保存成功！`)
                        this.batchGroupDialogInfo.isVisible = false;
                        // 刷新表格
                        this.$refs.tSysJobStandardRef.refresh();
                    }).finally(() => {
                        this.batchGroupDialogInfo.saveBtnIsLoading = false;
                    })
                });
            }
        }
    }
</script>

<style scoped>
</style>
