<template>
<!-- 计划方案维护 -->
    <div class="plan">
        <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-input clearable
                                    v-model="filterForm.data.caseId"
                                    placeholder="方案代码"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="方案描述">
                                <el-input clearable
                                    v-model="filterForm.data.caseDesc"
                                    placeholder="方案描述"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="顺序号">
                                <el-input clearable
                                    v-model="filterForm.data.seqNo"
                                    placeholder="顺序号"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </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>
                    <el-button
                        type="primary"
                        size="mini"
                        icon="el-icon-edit-outline"
                        v-privilege="activeMenu + 'PUT'"
                        @click="but_edit_plural()"
                        :loading="loading"
                    >修改</el-button>
                    <el-button
                        type="danger"
                        size="mini"
                        icon="el-icon-delete"
                        v-privilege="activeMenu + 'DELETE'"
                        @click="but_del_plural()"
                        :loading="loading"
                    >删除</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-form
                    size="mini"
                    ref="dialog_form_arr"
                    label-width="0px"
                    :model="tableFormDataObj"
                >
                    <el-table stripe
                        id="singleTable"
                        ref="singleTable"
                        :data="tableData"
                        v-loading="tableLoading"
                        style="width: 100%;"
                        :height="singleTableHeight"
                        border
                        size="mini"
                        highlight-current-row
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column
                            type="selection"
                            width="40"
                            align="center"
                            fixed="left"
                        ></el-table-column>
                        <el-table-column
                        sortable
                            prop="caseId"
                            label="方案代码"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                        sortable
                            prop="caseDesc"
                            label="方案描述"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.caseId + '.caseDesc'"
                                    :rules="dialog.editorBox.form.rules.caseDesc"
                                >
                                    <el-input clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.caseId].caseDesc"
                                    ></el-input>
                                    <template slot="error" slot-scope="scope">
                                        <p class="error" :title="scope.error">{{ scope.error }}</p>
                                    </template>
                                </el-form-item>
                                <span v-else>{{scope.row.caseDesc}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                        sortable
                            prop="isAp"
                            label="默认方案配置"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.caseId + '.isAp'"
                                    :rules="dialog.editorBox.form.rules.isAp"
                                >
                                    <el-select
                                        filterable clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.caseId].isAp"
                                    >
                                        <el-option
                                            value="DAY"
                                            label="日"
                                        ></el-option>
                                        <el-option
                                            value="MONTH"
                                            label="月"
                                        ></el-option>
                                        <el-option
                                            value="YEARS"
                                            label="年"
                                        ></el-option>
                                    </el-select>
                                    <template slot="error" slot-scope="scope">
                                        <p class="error" :title="scope.error">{{ scope.error }}</p>
                                    </template>
                                </el-form-item>
                                <span v-else>{{ nameObj.isAp[scope.row.isAp] ? nameObj.isAp[scope.row.isAp] : scope.row.isAp }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                        sortable
                            prop="seqNo"
                            label="顺序号"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    v-if="scope.row.isSelection"
                                    :prop="scope.row.caseId + '.seqNo'"
                                    :rules="dialog.editorBox.form.rules.seqNo"
                                >
                                    <el-input clearable
                                        style="width: 100%;"
                                        v-model="tableFormDataObj[scope.row.caseId].seqNo"
                                    ></el-input>
                                    <template slot="error" slot-scope="scope">
                                        <p class="error" :title="scope.error">{{ scope.error }}</p>
                                    </template>
                                </el-form-item>
                                <span v-else>{{scope.row.seqNo}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="crman"
                            label="创建人"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="crtime"
                            label="创建时间"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="upman"
                            label="修改人"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="uptime"
                            label="修改时间"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            class-name="gn-TableDownloadExcel-none"
                            label="操作"
                            align="center"
                            width="90px"
                            fixed="right"
                        >
                            <template slot-scope="scope">
                                <div>
                                    <el-button
                                        type="primary"
                                        size="mini"
                                        @click="but_edit_caseConfig(scope.row)"
                                    >方案内容</el-button>
                                    <!-- <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-form>
                <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="90px"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="方案代码" prop="caseId">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.caseId"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="方案描述" prop="caseDesc">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.caseDesc"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="默认方案配置" prop="isAp">
                                <el-select
                                filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.isAp"
                                >
                                    <el-option
                                        value="DAY"
                                        label="日"
                                    ></el-option>
                                    <el-option
                                        value="MONTH"
                                        label="月"
                                    ></el-option>
                                    <el-option
                                        value="YEARS"
                                        label="年"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="顺序号" prop="seqNo">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.seqNo"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="方案内容" prop="caseConfig">
                                <el-input clearable
                                    class="input-but"
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.caseConfig"
                                    readonly
                                >
                                    <el-button
                                        slot="append"
                                        type="primary"
                                        @click="configBoxParse()"
                                    >编辑</el-button>
                                </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>
        <el-dialog
            title="计划方案配置"
            :visible.sync="dialog.configBox.show"
            width="600px"
            :close-on-click-modal="false"
            :show-close="!loading"
        >
            <div>
                <el-form
                    size="mini"
                    label-width="65px"
                >
                    <div style="padding: 10px;border: 1px solid #ccc;">
                        <p
                            style="font-size: 14px;color: #000;line-height: 28px;margin-bottom: 5px;"
                        >配置区</p>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="数据来源">
                                    <el-select
                                        filterable clearable
                                        style="width: 100%;"
                                        v-model="dialog.configBox.form.data.dataOrigin"
                                    >
                                        <el-option
                                            v-for="(item, index) of dialog.configBox.dataSet.dataOrigin"
                                            :key="index"
                                            :value="item.value"
                                            :label="item.label"
                                        ></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.configBox.form.data.timeType"
                                    >
                                        <el-option
                                            v-for="(item, index) of dialog.configBox.dataSet.timeType"
                                            :key="index"
                                            :value="item.value"
                                            :label="item.label"
                                        ></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.configBox.form.data.timegranId"
                                    >
                                        <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="12">
                                <el-form-item label="算法">
                                    <el-select
                                        filterable clearable
                                        style="width: 100%;"
                                        v-model="dialog.configBox.form.data.algorithm"
                                    >
                                        <el-option
                                            v-for="(item, index) of dialog.configBox.dataSet.algorithm"
                                            :key="index"
                                            :value="item.value"
                                            :label="item.label"
                                        ></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.configBox.form.data.jq"
                                    >
                                        <el-option value="1" label="是"></el-option>
                                        <el-option value="0" label="否"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row> -->
                    </div>
                    <div style="padding: 10px;border: 1px solid #ccc;margin-top: 5px;">
                        <div style="line-height: 28px;margin-bottom: 5px;">
                            <span style="font-size: 14px;color: #000;">时间类型选择</span>
                            <el-button
                                v-if="dialog.configBox.form.data.timeType === '2'"
                                type="text"
                                size="mini"
                                style="margin-left: 5px;font-size: 14px;"
                                @click="configBoxTimeArrAdd()"
                            >新增</el-button>
                        </div>
                        <el-form-item
                            label="上"
                            v-if="dialog.configBox.form.data.timeType === '0'"
                        >
                            <el-input-number v-model="dialog.configBox.form.data.time" :step="1" step-strictly
                                :min="0" :max="999" style="width: 120px" controls-position="right"
                            ></el-input-number>
                            <span>{{ nameObj.timeGranId.obj[dialog.configBox.form.data.timegranId] ? nameObj.timeGranId.obj[dialog.configBox.form.data.timegranId] : dialog.configBox.form.data.timegranId }}</span>
                        </el-form-item>
                        <el-form-item
                            label="上"
                            v-if="dialog.configBox.form.data.timeType === '1'"
                        >
                            <el-select
                                filterable clearable
                                style="width: 100%;"
                                v-model="dialog.configBox.form.data.timegranId1"
                            >
                                <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-form-item
                            label="开始时间"
                            v-if="dialog.configBox.form.data.timeType === '1'"
                        >
                            <el-date-picker
                                type="date"
                                style="width: 100%;"
                                value-format="yyyy-MM-dd"
                                v-model="dialog.configBox.form.data.time1"
                            ></el-date-picker>
                        </el-form-item>
                        <el-form-item
                            label="结束时间"
                            v-if="dialog.configBox.form.data.timeType === '1'"
                        >
                            <el-date-picker
                                type="date"
                                style="width: 100%;"
                                value-format="yyyy-MM-dd"
                                v-model="dialog.configBox.form.data.time2"
                            ></el-date-picker>
                        </el-form-item>
                        <el-table stripe
                            v-if="dialog.configBox.form.data.timeType === '2'"
                            :data="dialog.configBox.form.data.timeArr"
                            height="200px"
                            border
                            size="mini"
                        >
                            <el-table-column
                    sortable label="时间">
                                <template slot-scope="scope">
                                    <el-form-item label-width="0px">
                                        <el-date-picker
                                            v-model="scope.row.date"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            style="width: 100%;"
                                        ></el-date-picker>
                                    </el-form-item>
                                </template>
                            </el-table-column>
                            <el-table-column
                    sortable
                                label="操作"
                                width="75px"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    <el-button
                                        size="mini"
                                        type="danger"
                                        @click="configBoxTimeArrDel(scope.row)"
                                    >删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </el-form>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button @click="dialog.configBox.show = false">取 消</el-button>
                <el-button
                    v-if="!dialog.editorBox.show && dialog.configBox.show"
                    type="primary"
                    @click="but_edit_caseConfig_save()"
                >保 存</el-button>
                <el-button
                    v-else
                    type="primary"
                    @click="configBoxStringify()"
                >确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { zCheckNumber2 } from '@/utils/validator.js'
export default {
    name: 'plan',
    data () {
        return {
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    caseId: '',
                    caseDesc: '',
                    seqNo: ''
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     caseId: 'plan1',
                //     caseDesc: '方案1',
                //     isAp: 'DAY',
                //     seqNo: '',
                //     caseConfig: ''
                // }
            ],
            multipleSelection: [],
            tableFormDataObj: {},
            nameObj: {
                isAp: {
                    DAY: '日',
                    MONTH: '月',
                    YEARS: '年'
                },
                timeGranId: {
                    obj: {
                        'DAY': '日',
                        'MONTH': '月',
                        'YEAR': '年'
                    },
                    arr: [{id: 'DAY', name: '日'}, {id: 'MONTH', name: '月'}, {id: 'YEAR', name: '年'}]
                }
                // timeGranId: {
                //     obj: {},
                //     arr: []
                // }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    form: {
                        data: {
                            caseId: '',
                            caseDesc: '',
                            isAp: '',
                            seqNo: '',
                            caseConfig: ''
                        },
                        rules: {
                            caseId: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            caseDesc: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            isAp: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            seqNo: [
                                { required: true, message: '该项不能为空', trigger: 'change' },
                                { validator: zCheckNumber2, trigger: 'change' }
                            ],
                            caseConfig: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ]
                        }
                    }
                },
                configBox: {
                    show: false,
                    form: {
                        row: null,
                        data: {
                            dataOrigin: '',
                            timeType: '0',
                            timegranId: '0',
                            algorithm: '',
                            jq: '',
                            time: '',
                            timegranId1: '',
                            time1: '',
                            time2: '',
                            timeArr: [
                                {
                                    date: '2019-08-10'
                                }
                            ]
                        },
                        rules: {

                        }
                    },
                    dataSet: {
                        dataOrigin: [
                            {
                                label: '实绩表',
                                value: 'T_RM_ACT_VALUE'
                            },
                            {
                                label: '计划数据表',
                                value: 'T_RM_PLAN_VALUE'
                            },
                            {
                                label: '平衡表数据',
                                value: 'T_RM_BALANCE_VALUE'
                            }
                        ],
                        timeType: [
                            {
                                label: '上一周期',
                                value: '0'
                            }
                            // {
                            //     label: '固定一段时间',
                            //     value: '1'
                            // },
                            // {
                            //     label: '离散时间',
                            //     value: '2'
                            // }
                        ],
                        algorithm: [
                            {
                                label: '按月分摊(忽略其他所有参数)',
                                value: 'FunctionFenTan'
                            },
                            {
                                label: '实绩产量与计划产量最接近的一个月',
                                value: 'FunctionAct'
                            },
                            {
                                label: '年权重推荐单耗',
                                value: 'FunctionJC'
                            }
                        ]
                    }
                }
            }
        }
    },
    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.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);
                    }
                });
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    caseId: that.filterForm.data.caseId,
                    caseDesc: that.filterForm.data.caseDesc,
                    seqNo: that.filterForm.data.seqNo
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/tpmcase/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({
                                caseId: item.caseId,
                                caseDesc: item.caseDesc,
                                isAp: item.isAp,
                                seqNo: item.seqNo,
                                caseConfig: item.caseConfig,
                                crman: item.crman,
                                crtime: item.crtime,
                                upman: item.upman,
                                uptime: item.uptime
                            })
                        }
                        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.caseId = '';
            that.filterForm.data.caseDesc = '';
            that.filterForm.data.seqNo = '';
            that.getTableData(1);
        },
        but_add () {
            let that = this;
            that.dialog.editorBox.form.data.caseId = '';
            that.dialog.editorBox.form.data.caseDesc = '';
            that.dialog.editorBox.form.data.isAp = '';
            that.dialog.editorBox.form.data.seqNo = '';
            that.dialog.editorBox.form.data.caseConfig = '';
            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.caseId = row.caseId;
            that.dialog.editorBox.form.data.caseDesc = row.caseDesc;
            that.dialog.editorBox.form.data.isAp = row.isAp;
            that.dialog.editorBox.form.data.seqNo = row.seqNo;
            that.dialog.editorBox.form.data.caseConfig = row.caseConfig;
            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/tpmcase/' + row.caseId)
                    .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 = {
                caseId: that.dialog.editorBox.form.data.caseId,
                caseDesc: that.dialog.editorBox.form.data.caseDesc,
                isAp: that.dialog.editorBox.form.data.isAp,
                seqNo: that.dialog.editorBox.form.data.seqNo,
                caseConfig: that.dialog.editorBox.form.data.caseConfig
            };
            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/tpmcase/', 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/tpmcase/' + SubmitData.caseId, 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;
                            });
                    }
                }
            });
        },
        configBoxParse () {
            let that = this;
            that.dialog.configBox.form.data.dataOrigin = '';
            that.dialog.configBox.form.data.timeType = '0';
            that.dialog.configBox.form.data.timegranId = '';
            that.dialog.configBox.form.data.algorithm = '';
            that.dialog.configBox.form.data.jq = '';
            that.dialog.configBox.form.data.time = '';
            that.dialog.configBox.form.data.timegranId1 = '';
            that.dialog.configBox.form.data.time1 = '';
            that.dialog.configBox.form.data.time2 = '';
            that.dialog.configBox.form.data.timeArr = [];
            if (that.dialog.editorBox.form.data.caseConfig) {
                let arr = that.dialog.editorBox.form.data.caseConfig.split('|');
                that.dialog.configBox.form.data.dataOrigin = arr[0] || '';
                that.dialog.configBox.form.data.timeType = arr[3] || '0';
                that.dialog.configBox.form.data.timegranId = arr[1] || '';
                that.dialog.configBox.form.data.algorithm = arr[4] || '';
                that.dialog.configBox.form.data.jq = arr[2] || '';
                switch (arr[3]) {
                    case '0':
                        that.dialog.configBox.form.data.time = arr[5] || '';
                        break;
                    case '1':
                        that.dialog.configBox.form.data.time1 = arr[5] || '';
                        that.dialog.configBox.form.data.time2 = arr[6] || '';
                        that.dialog.configBox.form.data.timegranId1 = arr[7] || '';
                        break;
                    case '2':
                        for (let i = 5; i < arr.length; i++) {
                            that.dialog.configBox.form.data.timeArr.push(
                                {
                                    date: arr[i]
                                }
                            );
                        }
                        break;
                }
            }
            that.dialog.configBox.show = true;
        },
        configBoxStringify () {
            let that = this;
            let caseConfig = that.dialog.configBox.form.data.dataOrigin + '|' +
                that.dialog.configBox.form.data.timegranId + '|' +
                that.dialog.configBox.form.data.jq + '|' +
                that.dialog.configBox.form.data.timeType + '|' +
                that.dialog.configBox.form.data.algorithm;
            switch (that.dialog.configBox.form.data.timeType) {
                case '0':
                    caseConfig += '|' + that.dialog.configBox.form.data.time;
                    break;
                case '1':
                    caseConfig += '|' + that.dialog.configBox.form.data.time1 + '|' + that.dialog.configBox.form.data.time2 + '|' + that.dialog.configBox.form.data.timegranId1;
                    break;
                case '2':
                    for (let item of that.dialog.configBox.form.data.timeArr) {
                        console.log(item)
                        caseConfig += '|' + item.date;
                    }
                    break;
            }
            that.dialog.editorBox.form.data.caseConfig = caseConfig;
            that.dialog.configBox.show = false;
        },
        configBoxTimeArrAdd () {
            let that = this;
            that.dialog.configBox.form.data.timeArr.unshift({date: ''});
        },
        configBoxTimeArrDel (row) {
            let that = this;
            for (let i = 0; i < that.dialog.configBox.form.data.timeArr.length; i++) {
                if (that.dialog.configBox.form.data.timeArr[i] === row) {
                    that.dialog.configBox.form.data.timeArr.splice(i, 1);
                    break;
                }
            }
        },
        but_edit_caseConfig (row) {
            let that = this;
            that.dialog.configBox.form.row = row;
            that.dialog.configBox.form.data.dataOrigin = '';
            that.dialog.configBox.form.data.timeType = '0';
            that.dialog.configBox.form.data.timegranId = '';
            that.dialog.configBox.form.data.algorithm = '';
            that.dialog.configBox.form.data.jq = '';
            that.dialog.configBox.form.data.time = '';
            that.dialog.configBox.form.data.timegranId1 = '';
            that.dialog.configBox.form.data.time1 = '';
            that.dialog.configBox.form.data.time2 = '';
            that.dialog.configBox.form.data.timeArr = [];
            if (row.caseConfig) {
                let arr = row.caseConfig.split('|');
                that.dialog.configBox.form.data.dataOrigin = arr[0] || '';
                that.dialog.configBox.form.data.timeType = arr[3] || '0';
                that.dialog.configBox.form.data.timegranId = arr[1] || '';
                that.dialog.configBox.form.data.algorithm = arr[4] || '';
                that.dialog.configBox.form.data.jq = arr[2] || '';
                switch (arr[3]) {
                    case '0':
                        that.dialog.configBox.form.data.time = arr[5] || '';
                        break;
                    case '1':
                        that.dialog.configBox.form.data.time1 = arr[5] || '';
                        that.dialog.configBox.form.data.time2 = arr[6] || '';
                        that.dialog.configBox.form.data.timegranId1 = arr[7] || '';
                        break;
                    case '2':
                        for (let i = 5; i < arr.length; i++) {
                            that.dialog.configBox.form.data.timeArr.push(
                                {
                                    date: arr[i]
                                }
                            );
                        }
                        break;
                }
            }
            that.dialog.editorBox.show = false;
            that.dialog.configBox.show = true;
        },
        but_edit_caseConfig_save () {
            let that = this;
            let caseConfig = that.dialog.configBox.form.data.dataOrigin + '|' +
                that.dialog.configBox.form.data.timegranId + '|' +
                that.dialog.configBox.form.data.jq + '|' +
                that.dialog.configBox.form.data.timeType + '|' +
                that.dialog.configBox.form.data.algorithm;
            switch (that.dialog.configBox.form.data.timeType) {
                case '0':
                    caseConfig += '|' + that.dialog.configBox.form.data.time;
                    break;
                case '1':
                    caseConfig += '|' + that.dialog.configBox.form.data.time1 + '|' + that.dialog.configBox.form.data.time2 + '|' + that.dialog.configBox.form.data.timegranId1;
                    break;
                case '2':
                    for (let item of that.dialog.configBox.form.data.timeArr) {
                        console.log(item)
                        caseConfig += '|' + item.date;
                    }
                    break;
            }
            that.loading = true;
            that.axios.put('pass/ems/v1/tpmcase/' + that.dialog.configBox.form.row.caseId, {
                caseId: that.dialog.configBox.form.row.caseId,
                caseConfig: caseConfig
            })
                .then(function (res) {
                    if (res.code === '0') {
                        that.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        that.dialog.configBox.form.row.caseConfig = caseConfig;
                        that.dialog.configBox.show = false;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.loading = false;
                }).catch(function () {
                    that.loading = false;
                });
        },
        // 表格中多选按钮的相关方法
        handleSelectionChange (val) {
            let that = this;
            let tableFormDataObj = {};
            for (let item of that.tableData) {
                item.isSelection = false;
            }
            for (let item of val) {
                let obj = {};
                let xId = item.caseId;
                if (that.tableFormDataObj[xId]) {
                    tableFormDataObj[xId] = that.tableFormDataObj[xId];
                } else {
                    for (let key in item) {
                        obj[key] = item[key];
                    }
                    tableFormDataObj[xId] = obj;
                }
                item.isSelection = true;
            }
            that.tableFormDataObj = tableFormDataObj;
            that.multipleSelection = val;
        },
        // 批量修改
        but_edit_plural () {
            let that = this;
            if (that.multipleSelection.length > 0) {
                that.$refs['dialog_form_arr'].validate((valid) => {
                    if (valid) {
                        let SubmitData = [];
                        for (let key in that.tableFormDataObj) {
                            SubmitData.push({
                                caseId: that.tableFormDataObj[key].caseId,
                                caseDesc: that.tableFormDataObj[key].caseDesc,
                                isAp: that.tableFormDataObj[key].isAp,
                                seqNo: that.tableFormDataObj[key].seqNo
                            });
                        }
                        that.loading = true;
                        that.axios.put('pass/ems/v1/tpmcase/batchUpdate', 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;
                            });
                        }
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        },
        // 批量删除
        but_del_plural () {
            let that = this;
            if (that.multipleSelection.length > 0) {
                let SubmitData = [];
                for (let item of that.multipleSelection) {
                    SubmitData.push({
                        caseId: item.caseId
                    });
                }
                that.$confirm('是否删除已选数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    state: 'warning'
                }).then(() => {
                    that.axios.delete('pass/ems/v1/tpmcase/batchDel', {
                        data: SubmitData,
                        contentType: 'application/json'
                    })
                        .then(function (res) {
                            if (res.code === '0') {
                                that.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                that.getTableData();
                            } else {
                                that.$message.error(res.message);
                            }
                        }).catch(function () {
                        });
                }).catch(() => {
                });
            } else {
                this.$message.error('至少要选择一条数据');
            }
        }
    }
}
</script>

<style lang="less">
.plan {
    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-table__row {
            height: 35px;
        }
        .el-table__body .el-form-item--mini.el-form-item {
            margin: 0px;
            .error {
                overflow: hidden;
                color: #f56c6c;
                font-size: 12px;
                line-height: 1;
            }
        }
    }
    .el-table {
        .el-form-item {
            margin-bottom: 0;
        }
    }
    .input-but {
        .el-input-group__append {
            background-color: #409eff;
            color: #fff;
        }
    }
    .is-success {
        .input-but {
            .el-input-group__append {
                border-color: #67c23a;
            }
        }
    }
    .is-error {
        .input-but {
            .el-input-group__append {
                border-color: #f56c6c;
            }
        }
    }
}
</style>
