<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container" style="margin: 0;width: 100%;">
        <div class="container-box" style="padding: 0 20px 0 0;">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div style="overflow: hidden;">
                <div class="search" style="float: left;">
                    <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                </div>
                <div style="margin-top: 20px;float: right;" class="bigbtns">
                        <el-button style="margin-left: 20px;" size="medium" type="success" @click="tempUpdate(1)">模板下载
                        </el-button>
                        <myUpload style="float: left;" @uploadBack="uploadBack" :fileInfo="fileInfot" :fileList="fileInfot.fileList"></myUpload>
                        <el-button style="margin-left: 20px;" class="btn-check" size="medium" type="primary" @click="dialogCheck(1)">添加
                        </el-button>
                        <myUpload style="float: right;margin-left: 20px;" @uploadBack="uploadBack" :fileInfo="fileInfo" :fileList="fileInfo.fileList"></myUpload>
                </div>
            </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="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="终端编码" width="145">
                    </el-table-column>
                    <!-- <el-table-column prop="modelType" 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="opTime" label="操做时间" width="135">
                    </el-table-column>
                    <el-table-column prop="stsDesc" label="状态">
                    </el-table-column>
                    <el-table-column prop="borrowName" label="借用人" width="100">
                    </el-table-column>
                    <el-table-column prop="borrowTime" label="借用时间" width="160">
                    </el-table-column>
                    <el-table-column prop="requireBackTime" label="预计归还时间" width="160">
                    </el-table-column>
                    <el-table-column prop="lastBackTime" label="实际归还时间" width="160">
                    </el-table-column>
                    <el-table-column label="操作" width="180px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">查看</el-button>
                            <el-button v-if="scope.row.sts == 0" size="mini" type="primary" @click="dialogCheck(3,scope.row)">修改</el-button>
                            <el-button v-if="scope.row.sts == 0" size="mini" type="danger" @click="delLine(scope.row)">删除</el-button>
                            <el-button v-if="scope.row.sts == 2" size="mini" type="primary" @click="backterminal(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>


            <el-dialog :title="titname+'终端管理'" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
                :modal-append-to-body="false" :close-on-click-modal="false">
                <el-form :model="infolist" ref="infolist" :rules="rules">
                    <div class="info-line">
                        <el-form-item prop="infovalue">
                            <span>终端类型</span>
                            <el-select :popper-append-to-body="false" v-model="infovalue" placeholder="终端类型" @change="verifcheck"
                                :disabled="disableStatus">
                                <el-option v-for="item in termOption" :key="item.id" :label="item.terminalTypeName+' - '+item.brandName+' - '+item.modelType"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="sts">
                            <span>终端状态</span>
                            <el-select :popper-append-to-body="false" v-model="infolist.sts" placeholder="终端状态"
                                :disabled="disableStatus">
                                <el-option v-for="items in stsOption" :key="items.dataCode" :label="items.dataName"
                                    :value="items.dataCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </div>
                    <div v-if="titname == '查看'">
                        <div class="info-line">
                            <el-form-item>
                                <span>当前借用人</span>
                                <el-input v-model="infolist.borrowName" placeholder="当前借用人" disabled></el-input>
                            </el-form-item>
                            <el-form-item>
                                <span>借用人工号</span>
                                <el-input v-model="infolist.borrowNo" placeholder="借用人工号" disabled></el-input>
                            </el-form-item>
                        </div>
                        <div class="info-line">
                            <el-form-item>
                                <span>借用时间</span>
                                <el-date-picker v-model="infolist.borrowTime" type="date" placeholder="借用时间" disabled>
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item>
                                <span>预计归还</span>
                                <el-date-picker v-model="infolist.requireBackTime" type="date" placeholder="归还时间"
                                    disabled>
                                </el-date-picker>
                            </el-form-item>
                        </div>
                        <div class="info-line">
                            <el-form-item>
                                <span>实际归还</span>
                                <el-date-picker v-model="infolist.lastBackTime" type="date" placeholder="实际归还" disabled>
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item>
                                <span>操作姓名</span>
                                <el-input v-model="infolist.opName" placeholder="操作姓名" disabled></el-input>
                            </el-form-item>
                        </div>
                        <div class="info-line">
                            <el-form-item>
                                <span>终端编码</span>
                                <el-input v-model="infolist.terminalNo" placeholder="终端编码" disabled></el-input>
                            </el-form-item>
                            <el-form-item>
                                <span>终端序列</span>
                                <el-input v-model="infolist.terminalNo" placeholder="终端序列" disabled></el-input>
                            </el-form-item>
                        </div>
                    </div>
                </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>
        </div>
        <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 toolList from "../../../components/toolList";
    import myMessage from "../../../components/myMessage.vue"

    export default {
        components: {
            myUpload,
            mySearch,
            toolList,
            myMessage
        },
        data() {
            const infovalue = (rule, value, callback) => {
                if (!this.infovalue) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const sts = (rule, value, callback) => {
                if (!this.infolist.sts) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const borrowTime = (rule, value, callback) => {
                if (!this.borrowTime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const requireBackTime = (rule, value, callback) => {
                if (!this.requireBackTime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const borrowName = (rule, value, callback) => {
                if (!this.infolist.borrowName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const borrowNo = (rule, value, callback) => {
                if (!this.infolist.borrowNo) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    infovalue: [{
                        required: true,
                        trigger: 'change',
                        validator: infovalue
                    }],
                    sts: [{
                        required: true,
                        trigger: 'change',
                        validator: sts
                    }],
                    borrowTime: [{
                        required: true,
                        trigger: 'change',
                        validator: borrowTime
                    }],
                    requireBackTime: [{
                        required: true,
                        trigger: 'change',
                        validator: requireBackTime
                    }],
                    borrowName: [{
                        required: true,
                        trigger: 'blur',
                        validator: borrowName
                    }],
                    borrowNo: [{
                        required: true,
                        trigger: 'blur',
                        validator: borrowNo
                    }],
                },
                tooltit: '测试终端管理',
                searchList: [{
                        type: 'input',
                        tit: '类别',
                        value: '',
                        width: '32%',
                    },
                    {
                        type: 'input',
                        tit: '品牌',
                        value: '',
                        width: '32%',
                    },
                    {
                        type: 'input',
                        tit: '型号',
                        value: '',
                        width: '32%',
                    },
                ],
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                disableStatus: false,
                titname: '终端',
                infolist: {},
                typeOptions: [{
                    dataCode: 1,
                    dataName: 111,
                }],
                userInfo: {},
                params: {},
                stsOption: [{
                        dataCode: '0',
                        dataName: '空闲',
                    },
                    {
                        dataCode: '1',
                        dataName: '预借',
                    },
                    {
                        dataCode: "2",
                        dataName: '借出',
                    }
                ],
                termOption: [],
                infovalue: '',
                borrowTime: '',
                requireBackTime: '',
                lastBackTime: '',
                fileInfo: {
                    type: 'btn',
                    typename: '导入',
                    btntype: 'primary',
                    limit: 1,
                    url: '/market/terminal/terminalrepos/import',
                    fileList: []
                },
                fileInfot: {
                    type: 'btn',
                    typename: '模板上传',
                    btntype: 'success',
                    limit: 1,
                    url: '/market/compatt/upload',
                    fileList: []
                },
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading: false
            }
        },
        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) {
                this.pageSize = n;
                this.loading = true;
                this.tableData = [];
                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;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //查看
            dialogCheck(v, n) {
                this.dialogStatus = true;
                this.disableStatus = false;
                if (v === 1) {
                    this.titname = '新建';
                    this.infovalue = '';
                    this.borrowTime = '';
                    this.requireBackTime = '';
                    this.lastBackTime = '';
                    this.getUser();
                    return
                } else if (v === 2) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 3) {
                    this.titname = '修改';
                }
                this.infolist = Object.assign({}, n); //拷贝
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
                this.borrowTime = this.infolist.borrowTime;
                this.requireBackTime = this.infolist.requireBackTime;
                this.lastBackTime = this.infolist.lastBackTime;
                for (let i = 0; i < this.termOption.length; i++) {
                    if (this.infolist.terminalType == this.termOption[i].terminalType && this.infolist.brandCode ==
                        this.termOption[i].brandCode && this.infolist.modelType == this.termOption[i].modelType) {
                        this.infovalue = this.termOption[i].id;
                    }
                }
            },
            //修改  添加
            dialogCli(v) {
                if (v === 1) {
                    this.infolist = {};
                    this.infovalue = '';
                    this.borrowTime = '';
                    this.requireBackTime = '';
                    this.lastBackTime = '';
                    this.dialogStatus = false;
                    return
                }
                let _this = this;
                this.infolist.opTime = _this.$formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss')
                this.infolist.borrowTime = _this.$formatDate(Date.parse(this.borrowTime), 'YYYY-MM-DD');
                this.infolist.requireBackTime = _this.$formatDate(Date.parse(this.requireBackTime),
                    'YYYY-MM-DD');
                this.lastBackTime ? this.infolist.lastBackTime = _this.$formatDate(Date.parse(this.lastBackTime),
                        'YYYY-MM-DD') :
                    '';
                this.infolist.sts === '0' ? this.infolist.stsDesc = '空闲' : this.infolist.sts === '1' ? this.infolist.stsDesc =
                    '预借' : this.infolist.stsDesc = '借出';
                if (this.titname === '新建') {
                    this.submitInfo("/market/terminal/terminalrepos/add", v);
                } else if (this.titname === '修改') {
                    this.submitInfo("/market/terminal/terminalrepo/update", v);
                }
            },
            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: 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 = {};
                                console.log(_this.infolist);
                                _this.dialogStatus = false;
                                _this.getList({}, _this.pageSize);
                            }

                        });
                    }
                })
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    this.$http({
                        url: "/market/terminal/terminalrepo/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            id: this.delid
                        },
                    }).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);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                this.centerDialogVisible = true;
                this.messTit = '即将删除此条数据, 是否删除?';
                this.delid = v.id;
            },
            backterminal(v) {
                let _this = this;
                this.$http({
                    url: "/market/terminal/terminalrepos/back",
                    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);
                    }
                });
            },
            tempUpdate(v) {
                let path = '';
                v === 1 ? path = "/market/terminal/terminalrepos/downloadTemp" : path =
                    "/market/terminal/terminalrepos/downloadTemp";
                this.$http({
                    url: path,
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    responseType: "blob",
                    data: {},
                }).then((response) => {
                    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                        let blob = new Blob([response.data], {
                            type: 'application/vnd.ms-excel'
                        })
                        window.navigator.msSaveOrOpenBlob(blob, '资源管理模板.xlsx')
                    } else {
                        /* 火狐谷歌的文件下载方式 */
                        var blob = new Blob([response.data])
                        var downloadElement = document.createElement('a')
                        var href = window.URL.createObjectURL(blob)
                        downloadElement.href = href
                        downloadElement.download = '资源管理模板.xlsx'
                        document.body.appendChild(downloadElement)
                        downloadElement.click()
                        document.body.removeChild(downloadElement)
                        window.URL.revokeObjectURL(href)
                    }
                });
            },
            verifcheck() {
                for (let i = 0; i < this.termOption.length; i++) {
                    if (this.termOption[i].id === this.infovalue) {
                        this.infolist.terminalTypeName = this.termOption[i].terminalTypeName;
                        this.infolist.terminalType = this.termOption[i].terminalType;
                        this.infolist.brandName = this.termOption[i].brandName;
                        this.infolist.brandCode = this.termOption[i].brandCode;
                        this.infolist.modelType = this.termOption[i].modelType;
                    }
                }
            },
            uploadBack(v) {
                this.getList({}, 1);
            },
            //获取列表
            getDepot() {
                this.$http({
                    url: "/market/terminal/terminaltype/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.termOption = res.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.getDepot();
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .titbox {
        div {
            float: right;

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

    .tabbox {
        margin-top: 16px;
    }

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