<template>
    <div>
        <div class="container structure-r" style="margin: 0;width: calc(50% - 10px);">
            <fullscreen :fullscreen.sync="fullscreen" class="container-box" style="padding: 0 20px 0 0;">
                <div class="titbox">
                    <h2>测试终端申请</h2>
                </div>
                <div class="search">
                    <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                </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%">
                        <el-table-column prop="terminalTypeName" label="类型">
                        </el-table-column>
                        <el-table-column prop="brandName" label="品牌">
                        </el-table-column>
                        <el-table-column prop="modelType" label="型号">
                        </el-table-column>
                        <el-table-column prop="terminalNo" label="终端编码">
                        </el-table-column>
                        <el-table-column label="操作" width="80px" align="center">
                            <template slot-scope="scope">
                                <el-button size="mini" type="primary" @click="dialogCheck(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>
            </fullscreen>
        </div>

        <div class="container structure-r" style="margin: 0;width: calc(50% - 10px);margin-left: 20px;">
            <div class="onetab">
                <el-table height="calc(100% - 10px)" class="com-table" ref="multipleTable" :data="infoApply" tooltip-effect="dark" size="small"
                    border style="width: 100%">
                    <el-table-column prop="terminalTypeName" label="类型">
                    </el-table-column>
                    <el-table-column prop="brandName" label="品牌">
                    </el-table-column>
                    <el-table-column prop="modelType" label="型号">
                    </el-table-column>
                    <el-table-column prop="terminalNo" label="终端编码">
                    </el-table-column>
                    <el-table-column label="操作" width="80px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="danger" @click="delone(scope.row)">x</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <el-form :model="infolist" ref="infolist" :rules="rules">
                <div class="info-line">
                    <el-form-item prop="terminalTypeName">
                        <span>申请标题</span>
                        <el-input v-model="infolist.tit" placeholder="申请标题" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item prop="modelType">
                        <span>归还时间</span>
                        <el-date-picker v-model="infolist.time" type="date" placeholder="归还时间">
                        </el-date-picker>
                    </el-form-item>
                </div>
                <div class="info-line">
                    <el-form-item>
                        <span>申请流程</span>
                        <el-select clearable v-model="terminal" placeholder="申请流程" disabled>
                            <el-option v-for="items in options" :key="items.procId" :label="items.procName"
                                :value="items.procId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <span>操作姓名</span>
                        <el-input v-model="infolist.opName" placeholder="操作姓名" disabled></el-input>
                    </el-form-item>
                </div>
            </el-form>
            <div class="t-footer">
                <el-button type="primary" @click="dialogCli(2)">确 定</el-button>
                <el-button @click="dialogCli(1)">取 消</el-button>
            </div>
        </div>
    </div>
</template>
<script>
    import mySearch from "../../../components/search.vue";

    export default {
        components: {
            mySearch
        },
        data() {
            const terminalTypeName = (rule, value, callback) => {
                if (!this.infolist.tit) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const modelType = (rule, value, callback) => {
                if (!this.infolist.time) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    terminalTypeName: [{
                        required: true,
                        trigger: 'blur',
                        validator: terminalTypeName
                    }],
                    modelType: [{
                        required: true,
                        trigger: 'blur',
                        validator: modelType
                    }],
                },
                searchList: [{
                        type: 'input',
                        tit: '类别',
                        value: '',
                        width: '48%',
                    },
                    {
                        type: 'input',
                        tit: '品牌',
                        value: '',
                        width: '48%',
                    },
                    // {
                    //     type: 'input',
                    //     tit: '型号',
                    //     value: '',
                    //     width: '32%',
                    // },
                ],
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                disableStatus: false,
                titname: '终端',
                infolist: {},
                typeOptions: [{
                    dataCode: 1,
                    dataName: 111,
                }],
                userInfo: {},
                params: {},
                infoApply: [],
                options: [],
                terminal:'686495855492730880',
            }
        },
        methods: {
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.terminalTypeName = v[0] : '';
                v[1] ? this.params.brandName = v[1] : '';
                v[2] ? this.params.modelType = v[2] : '';
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                v.sts = '0';
                this.pageSize = n;
                let _this = this;
                this.$http({
                    url: "/market/terminal/terminalrepo/queryPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: v,
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                    for (let i = 0; i < this.tableData.length; i++) {
                        _this.tableData[i].opTime =  _this.$formatDate(_this.tableData[i].opTime, "YYYY-MM-DD")
                    }
                });
            },
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //申请
            dialogCheck(n) {
                let _this = this;
                let sts = true;
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
                for(let i=0;i<this.infoApply.length;i++){
                    if(n.id === this.infoApply[i].id){
                        sts = false;
                    }
                }
                sts ? this.infoApply.push(Object.assign({}, n)) : '';
            },
            delone(n){
                for(let i=0;i<this.infoApply.length;i++){
                    if(n.id === this.infoApply[i].id){
                        this.infoApply.splice(i,1)
                    }
                }
            },
            //添加
            dialogCli(v) {
                if (v === 1) {
                    this.infolist = {};
                    this.infoApply = [];
                    this.getUser();
                    return
                }
                let _this = this;
                let info = {};
                info.opTime = _this.$formatDate(new Date(), "YYYY-MM-DD");
                info.opNo = this.userInfo.loginNo;
                info.opName = this.userInfo.loginName;
                info.params = {};
                info.params.reqBackTime = _this.infolist.time?_this.$formatDate(_this.infolist.time, "YYYY-MM-DD"):'';
                info.params.terminalRes = [];
                for (let i = 0; i < this.infoApply.length; i++) {
                    let terminal = {
                        terminalNo: this.infoApply[i].terminalNo,
                        terminalTypeName: this.infoApply[i].terminalTypeName,
                        brandName: this.infoApply[i].brandName,
                        modelType: this.infoApply[i].modelType,
                        id: this.infoApply[i].id,
                    }
                    info.params.terminalRes.push(terminal)
                }
                info.woTitle = this.infolist.tit;
                info.params.woTitle = this.infolist.tit;
                info.procId = this.terminal;
                for (let i = 0; i < this.options.length; i++) {
                    if(this.infolist.terminal === this.options[i].procId){
                        info.procName = this.options[i].procName;
                        info.procVersion = this.options[i].procVersion;
                    }
                }
                info.params.terminalRes = JSON.stringify(info.params.terminalRes)
                this.submitInfo("/bpm/api/startProc",info);
            },
            submitInfo(u,v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: v,
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.infolist = {};
                                _this.infoApply = [];
                                _this.getUser();
                            }

                        });
                    }
                })
            },
            //删除
            delLine(v) {
                let _this = this;
                this.$confirm('即将删除此条数据, 是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: "/market/terminal/terminaltype/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            id: v.id
                        },
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getList(this.params, this.pageSize);
                        }
                    });
                })
            },
            //流程
            getTermianl() {
                this.$http({
                    url: "/bpm/api/queryBpmProcList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + 1 + '","pageSize":"100"}'
                    },
                    data: {},
                }).then((res) => {
                    this.options = res.data.data;
                });
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                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;

            }
        },
        mounted() {
            this.getList({}, 1);
            this.getTermianl();
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .t-footer {
        text-align: right;
        padding-right: 20px;
    }

    .onetab {
        padding: 20px;
        height: calc(100% - 180px);
    }

    .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>
