<template>

    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <!-- <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch> -->
                <div>
                    <el-select :popper-append-to-body="false"  v-model="searchList[0].value" @change="searchInfo(searchList[0].value)">
                        <el-option v-for="item in searchList[0].options" :key="item.dataCode" :label="item.dataName" :value="item.dataCode">
                        </el-option>
                    </el-select>
                </div>
                <el-button class="btn-check" size="medium" type="primary" @click="dialogCheck(3)">添加{{titinfo}}
                </el-button>
            </div>
            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="lineName" label="线条名称">
                    </el-table-column>
                    <el-table-column prop="lineId" label="线条编码">
                    </el-table-column>
                    <el-table-column prop="unitName" label="单元名称">
                    </el-table-column>
                    <el-table-column prop="unitId" label="单元编码">
                    </el-table-column>
                    <el-table-column prop="moduleName" label="模块名称">
                    </el-table-column>
                    <el-table-column prop="moduleId" label="模块编码">
                    </el-table-column>
                    <el-table-column prop="funcName" label="功能名称">
                    </el-table-column>
                    <el-table-column prop="funcCode" label="功能编码">
                    </el-table-column>
                    <el-table-column prop="opNo" label="添加工号">
                    </el-table-column>
                    <el-table-column prop="opName" label="添加姓名">
                    </el-table-column>
                    <el-table-column prop="sortNum" label="排序">
                    </el-table-column>
                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1,scope.row)">查看</el-button>
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">修改</el-button>
                            <el-button size="mini" type="danger" @click="delLine(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                    :total="total">
                </el-pagination>
            </div>
        </div>

        <el-dialog :title="titname + titinfo" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-press-escape="false" :close-on-click-modal="false" :before-close="closedia">

            <el-form :model="infolist" ref="infolist" :rules="rules">
                <div class="info-line">
                    <el-form-item v-if="type == '1'" prop="lineName">
                        <span>线条名称</span>
                        <el-input v-model="infolist.lineName" placeholder="线条名称" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type != '1'" prop="lineId">
                        <span>线条名称</span>
                        <el-select :popper-append-to-body="false" :disabled="disableStatus" v-model="lineId"
                            placeholder="线条名称" @change="changetype(1)">
                            <el-option v-for="item in lineopt" :key="item.lineId" :label="item.lineName" :value="item.lineId">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item v-if="type == '2'" prop="unitName">
                        <span>单元名称</span>
                        <el-input v-model="infolist.unitName" placeholder="单元名称" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type > '2'" prop="unitId">
                        <span>单元名称</span>
                        <el-select :popper-append-to-body="false" :disabled="disableStatus" v-model="unitId"
                            placeholder="单元名称" @change="changetype(2)">
                            <el-option v-for="item in unitopt" :key="item.unitId" :label="item.unitName" :value="item.unitId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type == '3'" prop="moduleName">
                        <span>模块名称</span>
                        <el-input v-model="infolist.moduleName" placeholder="模块名称" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type > '3'" prop="moduleId">
                        <span>模块名称</span>
                        <el-select :popper-append-to-body="false" :disabled="disableStatus" v-model="moduleId"
                            placeholder="模块名称" @change="changetype(3)">
                            <el-option v-for="item in moudelopt" :key="item.moduleId" :label="item.moduleName" :value="item.moduleId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="type == '3'" prop="moduleUrl">
                        <span>模块地址</span>
                        <el-input v-model="infolist.moduleUrl" placeholder="模块地址" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type == '3'" prop="sts">
                        <span>模块状态</span>
                        <el-select :popper-append-to-body="false" v-model="infolist.sts" placeholder="模块状态" :disabled="disableStatus">
                            <el-option v-for="item in stsopt" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item v-if="type == '4'" prop="funcName">
                        <span>功能名称</span>
                        <el-input v-model="infolist.funcName" placeholder="功能名称" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type == '4'" prop="funcCode">
                        <span>功能编码</span>
                        <el-input v-model="infolist.funcCode" placeholder="功能编码" :disabled="titname != '添加'"></el-input>
                    </el-form-item>
                    <el-form-item v-if="type == '4'" prop="funcUrl">
                        <span>功能地址</span>
                        <el-input v-model="infolist.funcUrl" placeholder="功能地址" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item prop="sortNum">
                        <span>排序</span>
                        <el-input v-model="infolist.sortNum" placeholder="排序" :disabled="disableStatus"></el-input>
                    </el-form-item>
                </div>
                <el-form-item class="info-line online">
                    <span>图标</span>
                    <el-input v-model="infolist.icon" placeholder="图标" :disabled="disableStatus" type="textarea"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogCli(2)" v-if="titname !== '查看'">确 定</el-button>
                <el-button @click="dialogCli(1)" v-if="titname === '查看'">确 定</el-button>
                <el-button @click="dialogCli(1)">取 消</el-button>
            </div>

        </el-dialog>

        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible" v-if="centerDialogVisible"></myMessage>
    </fullscreen>

</template>
<script>
    import mySearch from "../../../components/search.vue";
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown'
    import myMessage from "../../../components/myMessage.vue"
    import toolList from '../../../components/toolList'

    export default {
        components: {
            mySearch,
            myUpload,
            uploadDown,
            myMessage,
            toolList
        },
        data() {
            const lineName = (rule, value, callback) => {
                if (!this.infolist.lineName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const lineId = (rule, value, callback) => {
                if (!this.lineId && this.type > 1) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }

            const unitName = (rule, value, callback) => {
                if (!this.infolist.unitName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const unitId = (rule, value, callback) => {
                if (!this.unitId && this.type > 2) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }

            const moduleName = (rule, value, callback) => {
                if (!this.infolist.moduleName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const moduleUrl = (rule, value, callback) => {
                if (!this.infolist.moduleUrl) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const sts = (rule, value, callback) => {
                if (!this.infolist.sts) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const moduleId = (rule, value, callback) => {
                if (!this.moduleId && this.type > 3) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }

            const funcCode = (rule, value, callback) => {
                if (!this.infolist.funcCode) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const funcName = (rule, value, callback) => {
                if (!this.infolist.funcName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const funcUrl = (rule, value, callback) => {
                if (!this.infolist.funcUrl) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const sortNum = (rule, value, callback) => {
                if (!this.infolist.sortNum) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    lineName: [{
                        required: true,
                        trigger: 'blur',
                        validator: lineName
                    }],
                    lineId: [{
                        required: true,
                        trigger: 'change',
                        validator: lineId
                    }],
                    unitName: [{
                        required: true,
                        trigger: 'blur',
                        validator: unitName
                    }],
                    unitId: [{
                        required: true,
                        trigger: 'change',
                        validator: unitId
                    }],
                    moduleName: [{
                        required: true,
                        trigger: 'blur',
                        validator: moduleName
                    }],
                    moduleUrl: [{
                        required: true,
                        trigger: 'blur',
                        validator: moduleUrl
                    }],
                    sts: [{
                        required: true,
                        trigger: 'change',
                        validator: sts
                    }],
                    moduleId: [{
                        required: true,
                        trigger: 'change',
                        validator: moduleId
                    }],
                    funcName: [{
                        required: true,
                        trigger: 'blur',
                        validator: funcName
                    }],
                    funcCode: [{
                        required: true,
                        trigger: 'blur',
                        validator: funcCode
                    }],
                    funcUrl: [{
                        required: true,
                        trigger: 'blur',
                        validator: funcUrl
                    }],
                    sortNum: [{
                        required: true,
                        trigger: 'blur',
                        validator: sortNum
                    }],
                },
                searchList: [{
                    type: 'sel',
                    tit: '状态',
                    value: '1',
                    width: '98%',
                    options: [{
                            dataCode: '1',
                            dataName: '线条',
                        },
                        {
                            dataCode: '2',
                            dataName: '单元',
                        }, {
                            dataCode: '3',
                            dataName: '模块',
                        },
                        {
                            dataCode: '4',
                            dataName: '功能',
                        },
                    ],
                }],
                tooltit: '线条管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                disableStatus: false,
                titname: '',
                infolist: {

                },
                lineId: '',
                unitId: '',
                moduleId: '',
                typeOptions: [{
                    dataCode: 1,
                    dataName: 111,
                }],
                userInfo: {},
                params: {},
                infoApply: [],
                uploadstatus: false,
                visiblec: [],
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading: false,
                lineopt: [],
                unitopt: [],
                moudelopt: [],
                type: "1",
                titinfo: '线条',
                stsopt: [{
                        id: "0",
                        name: '完成'
                    },
                    {
                        id: "1",
                        name: '在建'
                    }
                ]
            }
        },
        methods: {
            closedia() {
                this.infolist = {};
                this.lineId = '';
                this.unitId = '';
                this.moduleId = '';
                this.dialogStatus = false;
            },
            //搜索数据
            searchInfo(v) {
                let url = '';
                if (v == '1') {
                    url = '/market/mkline/queryPage';
                    this.titinfo = '线条';
                } else if (v == '2') {
                    url = '/market/mkunit/queryPage';
                    this.titinfo = '单元';
                } else if (v == '3') {
                    url = '/market/mkmodule/queryPage';
                    this.titinfo = '模块';
                } else if (v == '4') {
                    url = '/market/mkfunc/queryPage';
                    this.titinfo = '功能';
                } else {
                    return
                }
                this.type = v;
                this.getList(url, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.loading = true;
                this.tableData = [];
                this.$http({
                    url: v,
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"100"}'
                    },
                    data: {},
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //申请
            dialogCheck(v, n) {
                this.getLine();
                this.getUnit();
                this.getMoudel();
                this.dialogStatus = true;
                if (v === 1) {
                    this.titname = '查看';
                    this.disableStatus = true;
                    this.infolist = Object.assign({}, n);
                    this.lineId = this.infolist.lineId;
                    this.unitId = this.infolist.unitId;
                    this.moduleId = this.infolist.moduleId;
                    return
                } else if (v === 2) {
                    this.titname = '修改';
                    this.disableStatus = false;
                    this.infolist = Object.assign({}, n);
                    this.lineId = this.infolist.lineId;
                    this.unitId = this.infolist.unitId;
                    this.moduleId = this.infolist.moduleId;
                } else if (v === 3) {
                    this.titname = '添加';
                    this.disableStatus = false;
                }
                this.infolist.createNo = this.userInfo.loginNo;
                this.infolist.createName = this.userInfo.loginName;
            },
            //添加
            dialogCli(v) {
                if (v === 1) {
                    this.infolist = {};
                    this.lineId = '';
                    this.unitId = '';
                    this.moduleId = '';
                    this.dialogStatus = false;
                    return
                } else {
                    this.infolist.opTime = this.$formatDate(new Date(), "YYYY-MM-DD");
                    this.infolist.opNo = this.userInfo.loginNo;
                    this.infolist.opName = this.userInfo.loginName;
                    this.infolist.lineId = this.lineId;
                    this.infolist.unitId = this.unitId;
                    this.infolist.moduleId = this.moduleId;
                    if (this.titname === '添加') {
                        let url = '';
                        if (this.type == '1') {
                            url = '/market/cmkline/addLine';
                        } else if (this.type == '2') {
                            url = '/market/cmkunit/addUnit';
                        } else if (this.type == '3') {
                            url = '/market/cmkmodule/addModule';
                        } else if (this.type == '4') {
                            url = '/market/cmkfunc/addFunc';
                        }
                        this.submitInfo(url);
                    } else if (this.titname === '修改') {
                        let url = '';
                        if (this.type == '1') {
                            url = '/market/cmkline/update';
                        } else if (this.type == '2') {
                            url = '/market/cmkunit/update';
                        } else if (this.type == '3') {
                            url = '/market/cmkmodule/update';
                        } else if (this.type == '4') {
                            url = '/market/cmkfunc/update';
                        }
                        this.submitInfo(url);
                    }

                }
            },
            submitInfo(u) {
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        let _this = this;
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: this.infolist,
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.infolist = {};
                                _this.searchInfo(_this.type);
                                _this.lineId = '';
                                _this.unitId = '';
                                _this.moduleId = '';
                                _this.dialogStatus = false;
                            }
                        });
                    }
                })
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    let url = '';
                    let params = {};
                    if (_this.type == '1') {
                        url = '/market/cmkline/del';
                        params.lineId = this.delid;
                    } else if (_this.type == '2') {
                        url = '/market/cmkunit/del';
                        params.unitId = this.delid;
                    } else if (_this.type == '3') {
                        url = '/market/cmkmodule/del';
                        params.moduleId = this.delid;
                    } else if (_this.type == '4') {
                        url = '/market/cmkfunc/del';
                        params.funcCode = this.delid;
                    }
                    _this.$http({
                        url: url,
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: params
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.searchInfo(_this.type);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                if (this.type == '1') {
                    this.messTit = '即将删除此条线条数据, 是否删除?';
                    this.delid = v.lineId;
                } else if (this.type == '2') {
                    this.messTit = '即将删除此条单元数据, 是否删除?';
                    this.delid = v.unitId;
                } else if (this.type == '3') {
                    this.messTit = '即将删除此条模块数据, 是否删除?';
                    this.delid = v.moduleId;
                } else if (this.type == '4') {
                    this.messTit = '即将删除此条功能数据, 是否删除?';
                    this.delid = v.funcCode;
                }
                this.centerDialogVisible = true;
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.searchInfo(this.type);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                this.infolist = {
                    opNo: '',
                    opName: '',
                };
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
            },
            getLine() {
                let _this = this;
                this.$http({
                    url: '/market/mkline/queryList',
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.lineopt = res.data;
                });
            },
            getUnit(v) {
                let _this = this;
                this.$http({
                    url: '/market/mkunit/queryList',
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        lineId: v
                    },
                }).then((res) => {
                    this.unitopt = res.data;
                });
            },
            getMoudel(v) {
                let _this = this;
                this.$http({
                    url: '/market/mkmodule/queryList',
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        unitId: v
                    },
                }).then((res) => {
                    this.moudelopt = res.data;
                });
            },
            changetype(v) {
                if (v == "1") {
                    for (let i = 0; i < this.lineopt.length; i++) {
                        if (this.lineId == this.lineopt[i].lineId) {
                            this.infolist.lineName = this.lineopt[i].lineName;
                        }
                    }
                    this.getUnit(this.lineId);
                    this.unitId = '';
                    this.moduleId = '';
                } else if (v == "2") {
                    for (let i = 0; i < this.unitopt.length; i++) {
                        if (this.unitId == this.unitopt[i].unitId) {
                            this.infolist.unitName = this.unitopt[i].unitName;
                        }
                    }
                    this.getMoudel(this.unitId);
                    this.moduleId = '';
                } else if (v == "3") {
                    for (let i = 0; i < this.moudelopt.length; i++) {
                        if (this.moduleId == this.moudelopt[i].moduleId) {
                            this.infolist.moduleName = this.moudelopt[i].moduleName;
                        }
                    }
                }
            }
        },
        mounted() {
            this.getList('/market/mkline/queryPage', 1);
            this.getLine();
            this.getUnit();
            this.getMoudel();
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .onetab {
        margin-bottom: 20px;
        padding: 0 20px;
    }

    .titbox {
        div {
            float: right;

            i {
                font-size: 22px;
                margin-left: 20px;
                cursor: pointer;
            }
        }
    }

    .tabbox {
        margin-top: 15px;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 80px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }
    }
</style>
