<template>
    <div class="PlanProjectRecommend">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                    label-width="70px"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="时间粒度">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.timeGranId"
                                    placeholder="时间粒度"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.timeGranId.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="计划项目">
                                <el-select
                                        filterable clearable
                                    v-model="filterForm.data.itemId"
                                    placeholder="计划项目"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.itemId.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="方案代码">
                                <el-select
                                        filterable clearable
                                    v-model="filterForm.data.caseId"
                                    placeholder="方案代码"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.caseId.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="box-top-gjl">
                    <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-plus"
                        v-privilege="activeMenu + 'ADD'"
                        @click="but_add"
                    >新增</el-button>
                    <div style="float:right; text-align: right;">
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-search"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="getTableData(1)"
                        >查询</el-button>
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-refresh"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="tableDataCancel();"
                        >重置</el-button>
                        <el-button
                            size="mini"
                            :icon="filterForm.show ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
                            :title="filterForm.show ? '收起搜索区' : '展开搜索区'"
                            @click="filterForm.show = !filterForm.show;
                                $nextTick(() => {
                                    singleTableHeight = getRoleHeight($refs['singleTable'].$el) - 45;
                                }
                            );"
                        >{{ filterForm.show ? '收起' : '展开' }}</el-button>
                    </div>
                </div>
            </div>
            <div class="box-bottom">
                <el-table stripe
                    id="singleTable"
                    ref="singleTable"
                    :data="tableData"
                    v-loading="tableLoading"
                    style="width: 100%;"
                    :height="singleTableHeight"
                    border
                    size="mini"
                    highlight-current-row
                >
                    <el-table-column
                    sortable
                        prop="itemId"
                        label="项目ID"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.itemId.obj[scope.row.itemId] ? nameObj.itemId.obj[scope.row.itemId] : scope.row.itemId }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    sortable
                        prop="timeGranId"
                        label="时间粒度"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.timeGranId.obj[scope.row.timeGranId] ? nameObj.timeGranId.obj[scope.row.timeGranId] : scope.row.timeGranId }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    sortable
                        prop="caseId"
                        label="方案代码"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.caseId.obj[scope.row.caseId] ? nameObj.caseId.obj[scope.row.caseId] : scope.row.caseId }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    sortable
                        prop="seqNo"
                        label="顺序号"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        class-name="gn-TableDownloadExcel-none"
                        label="操作"
                        align="center"
                        width="140px"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    type="primary"
                                    size="mini"
                                    v-privilege="activeMenu + 'PUT'"
                                    @click="but_edit(scope.row)"
                                >修改</el-button>
                                <el-button
                                    type="danger"
                                    size="mini"
                                    v-privilege="activeMenu + 'DELETE'"
                                    @click="but_del(scope.row)"
                                >删除</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    :page-sizes="[10, 20, 50, 100, 500, 1000]"
                    :page-size="pageSize"
                    :current-page.sync="pageNum"
                    @size-change="tableSizeChange"
                    @current-change="getTableData()"
                    style="text-align: right;margin-top: 10px;"
                ></el-pagination>
            </div>
        </div>
        <el-dialog
            :title="'计划项目推荐方案 - ' + (dialog.editorBox.type === 'add' ? '新增' : '修改')"
            :visible.sync="dialog.editorBox.show"
            width="600px"
            :close-on-click-modal="false"
            :show-close="!loading"
        >
            <div>
                <el-form
                    ref="dialog_form"
                    :model="dialog.editorBox.form.data"
                    :rules="dialog.editorBox.form.rules"
                    size="mini"
                    label-width="85px"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="项目ID">
                                <el-select
                                filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.itemId"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <el-option
                                        v-for="item of nameObj.itemId.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="时间粒度">
                                <el-select
                                filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.timeGranId"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <el-option
                                        v-for="item of nameObj.timeGranId.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="方案代码">
                                <el-select
                                filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.caseId"
                                >
                                    <el-option
                                        v-for="item of nameObj.caseId.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="顺序号">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.seqNo"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button @click="dialog.editorBox.show = false" :loading="loading">取 消</el-button>
                <el-button
                    type="primary"
                    @click="dataSave"
                    :loading="loading"
                >保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'PlanProjectRecommend',
    data () {
        return {
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    itemId: '',
                    timeGranId: '',
                    caseId: ''
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     itemId: 'plan1',
                //     timeGranId: '日',
                //     caseId: '方案1',
                //     seqNo: ''
                // }
            ],
            nameObj: {
                itemId: {
                    obj: {},
                    arr: []
                },
                caseId: {
                    obj: {},
                    arr: []
                },
                timeGranId: {
                    obj: {},
                    arr: []
                }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    form: {
                        data: {
                            itemId: '',
                            timeGranId: '',
                            caseId: '',
                            seqNo: ''
                        },
                        rules: {

                        }
                    }
                }
            }
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable'
                }
            ]
        };
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 45;
            }, 1);
        });
        that.getTimeGranData()
        that.getItemData();
        that.getCaseData();
        that.getTableData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 获取时间粒度(计划项目推荐方案维护)
        getTimeGranData () {
            let that = this;
            let url = 'pass/ems/v1/trmtimegrans/getIdAndName';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.timeGranId.arr = arr;
                        that.nameObj.timeGranId.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取计划项目
        getItemData () {
            let that = this;
            let url = 'pass/ems/v1/trmactitems/getIdAndName';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.itemid,
                                name: item.itemname
                            })
                            obj[item.itemid] = item.itemname;
                        }
                        that.nameObj.itemId.arr = arr;
                        that.nameObj.itemId.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取方案
        getCaseData () {
            let that = this
            let url = 'pass/ems/v1/tpmcase/getIdAndName';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.caseId,
                                name: item.caseDesc
                            })
                            obj[item.caseId] = item.caseDesc;
                        }
                        that.nameObj.caseId.arr = arr;
                        that.nameObj.caseId.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    caseid: that.filterForm.data.caseId,
                    itemid: that.filterForm.data.itemId,
                    timegranid: that.filterForm.data.timeGranId
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/tpmcaseacts/like/?pageNum=' + that.pageNum + '&pageSize=' + that.pageSize;
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        for (let item of res.data.list) {
                            arr.push({
                                itemId: item.itemid,
                                timeGranId: item.timegranid,
                                caseId: item.caseid,
                                seqNo: item.seqno
                            })
                        }
                        that.tableData = arr;
                        that.total = res.data.total;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                });
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.pageSize = val;
            that.getTableData(1);
        },
        // 重置搜索
        tableDataCancel () {
            let that = this;
            that.filterForm.data.itemId = '';
            that.filterForm.data.timeGranId = '';
            that.filterForm.data.caseId = '';
            that.getTableData(1);
        },
        but_add () {
            let that = this;
            that.dialog.editorBox.form.data.itemId = '';
            that.dialog.editorBox.form.data.timeGranId = '';
            that.dialog.editorBox.form.data.caseId = '';
            that.dialog.editorBox.form.data.seqNo = '';
            that.dialog.editorBox.type = 'add';
            that.dialog.editorBox.show = true;
            setTimeout(() => {
                that.$refs['dialog_form'].clearValidate();
            }, 100);
        },
        but_edit (row) {
            let that = this;
            that.dialog.editorBox.form.data.itemId = row.itemId;
            that.dialog.editorBox.form.data.timeGranId = row.timeGranId;
            that.dialog.editorBox.form.data.caseId = row.caseId;
            that.dialog.editorBox.form.data.seqNo = row.seqNo;
            that.dialog.editorBox.type = 'edit';
            that.dialog.editorBox.show = true;
        },
        but_del (row) {
            let that = this;
            that.$confirm('是否删除该条数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                state: 'warning'
            }).then(() => {
                that.axios.delete('pass/ems/v1/tpmcaseacts/' + row.timeGranId + ',' + row.itemId)
                    .then(function (res) {
                        if (res.code === '0') {
                            that.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            that.getTableData();
                        } else {
                            that.$message.error(res.message);
                        }
                    }).catch(function () {
                    });
            }).catch(() => {
            });
        },
        dataSave () {
            let that = this;
            let SubmitData = {
                itemid: that.dialog.editorBox.form.data.itemId,
                timegranid: that.dialog.editorBox.form.data.timeGranId,
                caseid: that.dialog.editorBox.form.data.caseId,
                seqno: that.dialog.editorBox.form.data.seqNo
            };
            that.$refs['dialog_form'].validate((valid) => {
                if (valid) {
                    if (that.dialog.editorBox.type === 'add') {
                        // let foorData = new FormData();
                        // for (let key in SubmitData) {
                        //     foorData.append(key, SubmitData[key]);
                        // }
                        that.loading = true;
                        that.axios.post('pass/ems/v1/tpmcaseacts/', SubmitData, {
                            contentType: 'application/json'
                        })
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    } else {
                        that.loading = true;
                        that.axios.put('pass/ems/v1/tpmcaseacts/' + SubmitData.timegranid + ',' + SubmitData.itemid, SubmitData)
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    }
                }
            });
        }
    }
}
</script>

<style lang="less">
.PlanProjectRecommend {
    min-width: 700px;
    height: 100%;
    .box {
        height: 100%;
        padding: 15px 15px 0 15px;
        .box-top {
            .el-form-item {
                margin-bottom: 7px;
            }
            .box-top-gjl {
                overflow: hidden;
                // margin-top: -7px;
                padding: 7px 0;
                border-top: 1px solid #ccc;
                // border-bottom: 1px solid #ccc;
                // margin-bottom: 6px;
            }
        }
    }
    .el-table {
        .el-form-item {
            margin-bottom: 0;
        }
    }
}
</style>
