<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>
                <el-button class="btn-check" size="medium" type="primary" @click="dialogCheck(1)">添加
                </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="woTitle" label="工单标题">
                    </el-table-column>
                    <el-table-column prop="sendName" label="派发姓名">
                        <template slot-scope="scope">
                            <span>{{$desensitization(scope.row.sendName,1)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="sendNo" label="派发工号">
                    </el-table-column>
                    <el-table-column prop="sendTime" label="派发时间">
                    </el-table-column>
                    <el-table-column prop="stsDesc" label="状态">
                    </el-table-column>
                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">

                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">查看</el-button>
                            <el-button v-if="scope.row.sts == '4' && scope.row.leaderAuditNo == userInfo.loginNoStr" size="mini" type="primary"
                                @click="dialogCheck(3,scope.row)">处理</el-button>
                            <!-- <el-button v-if="scope.row.stsDesc == '打回' && scope.row.leaderAuditNo == userInfo.loginNoStr" size="mini" type="danger"
                                @click="delLine(scope.row)">删除</el-button> -->
                            <el-button v-if="scope.row.sts == 1" size="mini" type="primary"
                                @click="overcheck(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="70%" :close-on-press-escape="false"
                :show-close="true" :before-close="closedia" :destroy-on-close="true" :modal-append-to-body="false"
                :close-on-click-modal="false" :fullscreen="true">

                <div class="dialog-l">
                    <el-tabs tab-position="left" v-model="activeName">
                        <el-tab-pane v-for="item in routerList" :key="item.label" :name="item.label" :label="item.name">
                        </el-tab-pane>
                    </el-tabs>
                </div>

                <div v-loading="loadinged" class="dialog-r">
                    <el-form :model="infolist" ref="infolist" :rules="rules"
                        style="height: calc(100vh - 180px);overflow-y: scroll;">
                        <div class="info-line">
                            <el-form-item prop="woTitle">
                                <span>工单标题</span>
                                <el-input v-model="infolist.woTitle" placeholder="工单标题" :disabled="disableStatus">
                                </el-input>
                            </el-form-item>
                            <el-form-item prop="requireTime">
                                <span>反馈时间</span>
                                <el-date-picker :append-to-body='false' v-model="infolist.requireTime" type="date"
                                    placeholder="要求反馈时间" :disabled="disableStatus">
                                </el-date-picker>
                            </el-form-item>
                        </div>
                        <el-form-item prop="visiblec" class="info-line online">
                            <span>接收人员</span>

                            <deptTreeOnly v-if="!disableStatus" class="tree" @treeCheck="treeCheckonly"
                                :defaultList="defaultList" :type="depttype" :closeList="closeList"></deptTreeOnly>
                            <div :class="!disableStatus?'tree treeUser':'tree treeUserb'">
                                <p>{{treeListonly.leaderAuditName}}
                                <i v-if="treeListonly.leaderAuditName" @click="deletes(item,index)"
                                    class="el-icon-error"></i></p>
                            </div>
                        </el-form-item>

                        <el-form-item class="info-line online">
                            <span>任务描述</span>
                            <el-input v-model="infolist.arcDesc" :disabled="disableStatus" placeholder="任务描述"
                                type="textarea" :rows="3"></el-input>
                        </el-form-item>
                        <div style="padding-left: 80px" v-if="!disableStatus">
                            <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo" :fileList="fileInfo.fileList">
                            </myUpload>
                        </div>
                    </el-form>
                    <div slot="footer" class="dialog-footer myfooter">
                        <el-button @click="dialogCli(1)" type="primary">确 定</el-button>
                        <el-button @click="dialogCli(2)">取 消</el-button>
                    </div>
                </div>
            </el-dialog>

            <el-dialog title="查看工单" :visible.sync="dialogInfo" width="70%" :close-on-press-escape="false"
                :show-close="true" :before-close="closedia" :destroy-on-close="true" :modal-append-to-body="false"
                :close-on-click-modal="false" :fullscreen="true">
                <div style="height: calc(100vh - 180px);overflow-y: scroll;">
                    <orderCheck :infolist="infolist" :datalist="datalist" :backlist="backlist" :dialogInfo="dialogInfo" :historySts="historySts"></orderCheck>

                </div>
                <div class="dialog-footer myfooter">
                    <el-button @click="dialoginfoCli(1)" type="primary">轨 迹</el-button>
                    <el-button @click="dialoginfoCli(2)" type="primary" v-if="infolist.sts == '4'&& infolist.leaderAuditNo == userInfo.loginNoStr">处 理</el-button>
                    <el-button @click="dialoginfoCli(3)">取 消</el-button>
                </div>
            </el-dialog>
        </div>
        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible"
            v-if="centerDialogVisible"></myMessage>
    </fullscreen>
</template>
<script>
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown'
    import mySearch from '../../../components/search'
    import toolList from '../../../components/toolList'
    import myMessage from "../../../components/myMessage.vue"
    import deptTree from "../../../components/deptTreeUser.vue"
    import deptTreeOnly from "../../../components/deptTreeOnly.vue"
    import orderCheck from "./orderCheck.vue"

    export default {
        components: {
            myUpload,
            uploadDown,
            mySearch,
            toolList,
            myMessage,
            deptTree,
            deptTreeOnly,
            orderCheck
        },
        data() {
            const woTitle = (rule, value, callback) => {
                if (!this.infolist.woTitle) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const requireTime = (rule, value, callback) => {
                if (!this.infolist.requireTime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    woTitle: [{
                        required: true,
                        trigger: 'blur',
                        validator: woTitle
                    }],
                    requireTime: [{
                        required: true,
                        trigger: 'change',
                        validator: requireTime
                    }]
                },
                tooltit: '工单管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                infolist: {},
                typeOptions: [],
                titname: '',
                disableStatus: false,
                fileList: [],
                visiblec: [],
                visibleList: [],
                attList: [],
                fileInfo: {
                    limit: 10,
                    url: '/market/cwo/upload',
                    fileList: []
                },
                datalist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 1,
                    attList:[]
                },
                backlist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 1,
                    attList:[]
                },
                uploadstatus: false,
                userInfo: {},
                searchList: [{
                        type: 'input',
                        tit: '请输入标题',
                        value: '',
                        width: '98%'
                    },
                    // {
                    //     type: 'input',
                    //     tit: '发起人',
                    //     value: '',
                    //     width: '48%',
                    // },
                ],
                params: {},
                btnsts: 1,
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                delno: '',
                loading: false,
                loadinged: false,
                gdstatus: false,
                treeList: [],
                treeListonly: [],
                defaultList: [],
                depttype: 0,
                activeName: '1',
                routerList: [{
                    name: '其他人办理',
                    label: '1'
                }, {
                    name: '领导审批',
                    label: '2'
                }, {
                    name: '办理结束',
                    label: '3'
                }, ],
                dialogInfo: false,
                historySts: false,
                closeList: false,
            }
        },
        methods: {
            deletes(val, index) {
                this.treeListonly = {};
                this.closeList = !this.closeList;
            },
            treeCheck(v) {
                this.treeList = v;
            },
            treeCheckonly(v) {
                this.treeListonly = v;
            },
            //申请
            overcheck(v) {
                let _this = this;
                this.$http({
                    url: "/market/cwo/chkWoInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        woNo: v.woNo,
                        sts : "3",
                        stsDesc : "已归档"
                    },
                }).then((res) => {
                    _this.$message({
                        message: '成功',
                        type: 'success'
                    });
                    _this.getList({}, _this.pageSize);
                    _this.dialogStatus = false;
                });
            },
            closedia() {
                this.datalist.attList = [];
                this.backlist.attList = [];
                this.uploadstatus = false;
                this.fileInfo.fileList = [];
                this.infolist = {};
                this.infolist.deptCode = '';
                this.dialogStatus = false;
                this.defaultList = [];
                this.depttype = 0;
                this.loadinged = false;
                this.dialogInfo = false;
                this.treeListonly = [];
                this.treeList = [];
                this.attList = [];
                this.historySts = false;
            },
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.woTitle = v[0] : '';
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.tableData = [];
                this.loading = true;
                this.$http({
                    url: "/market/cwo/queryAllPage ",
                    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.disableStatus = false;
                this.dialogInfo = false;
                this.btnsts = v;
                this.visiblec = [];
                this.infolist = {};
                this.defaultList = [];
                this.attList = [];
                this.datalist.attList = [];
                this.backlist.attList = [];
                if (v === 1) {
                    this.titname = '新建';
                    this.getUser();
                    this.depttype = 1;
                    this.treeListonly = [];
                    this.treeList = [];
                    this.dialogStatus = true;
                    return
                } else if (v === 2) {
                    this.titname = '查看';
                } else if (v === 3) {
                    this.titname = '修改';
                    this.loadinged = true;
                    this.dialogStatus = true;
                }
                this.$http({
                    url: "/market/cwo/queryInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        woNo: n.woNo,
                        id: n.id
                    },
                }).then((res) => {
                    res.data.sendName = this.$desensitization(res.data.sendName, 1);
                    this.datalist.attList = res.data.attList;
                    this.backlist.attList = res.data.revertAttList;
                    if (v === 2) {
                        this.dialogInfo = true;
                    }
                    // this.treeListonly.leaderAuditName = res.data.leaderAuditName;
                    // this.treeListonly.leaderAuditNo = res.data.leaderAuditNo;
                    this.uploadstatus = true;
                    this.infolist = res.data;
                    this.fileInfo.fileList = [];
                    this.treeList = res.data.deptList;
                    if (res.data.attList) {
                        res.data.attList.forEach(item => {
                            this.fileInfo.fileList.push({
                                name: item.fileName,
                                url: '',
                                id: item.id,
                                fileName: item.fileName,
                                fileCode: item.fileCode,
                                type: item.type,
                            });
                            this.attList.push({
                                name: item.fileName,
                                url: '',
                                id: item.id,
                                fileName: item.fileName,
                                fileCode: item.fileCode,
                                type: item.type,
                            });
                        })
                    }
                    this.loadinged = false;
                });
            },
            dialogCli(v) {
                this.datalist.attList = [];
                this.backlist.attList = [];
                this.uploadstatus = false;
                if (v === 2) {
                    this.fileInfo.fileList = [];
                    this.infolist = {};
                    this.infolist.deptCode = '';
                    this.dialogStatus = false;
                    this.defaultList = [];
                    this.depttype = 0;
                    return
                }
                if (this.titname === '新建') {
                    this.submitInfo("/market/cwo/add", v);
                } else if (this.titname === '修改') {
                    this.submitInfo("/market/cwo/chkWoInfo", v);
                }
            },
            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        if (this.activeName == '1') {
                            this.infolist.sts = "2";
                            this.infolist.stsDesc = "待处理";
                            this.infolist.auditName = this.treeListonly.leaderAuditName;
                            this.infolist.auditNo = this.treeListonly.leaderAuditNo;
                            this.infolist.leaderAuditName = this.treeListonly.leaderAuditName;
                            this.infolist.leaderAuditNo = this.treeListonly.leaderAuditNo;
                            if(!this.treeListonly.leaderAuditName){
                                this.$message({
                                    message: '请选择接单人！',
                                    type: 'error'
                                });
                                return
                            }
                        } else if (this.activeName == '2') {
                            this.infolist.sts = "0";
                            this.infolist.stsDesc = "待审核";
                            this.infolist.auditName = this.userInfo.loginName;
                            this.infolist.auditNo = this.userInfo.loginNo;
                            this.infolist.leaderAuditName = this.treeListonly.leaderAuditName;
                            this.infolist.leaderAuditNo = this.treeListonly.leaderAuditNo;
                            if(!this.treeListonly.leaderAuditName){
                                this.$message({
                                    message: '请选择接单人！',
                                    type: 'error'
                                });
                                return
                            }
                        } else if (this.activeName == '3') {
                            this.infolist.sts = "1";
                            this.infolist.stsDesc = "已处理";
                        }
                        this.infolist.stepSts = this.activeName;
                        this.infolist.sendNo = this.userInfo.loginNo;
                        this.infolist.sendName = this.userInfo.loginName;
                        this.infolist.sendTime = this.$formatDate(new Date(), "YYYY-MM-DD");
                        this.infolist.requireTime = this.infolist.requireTime?this.$formatDate(this.infolist.requireTime,"YYYY-MM-DD"):'';
                        let attList = [];
                        for (let i = 0; i < this.attList.length; i++) {
                            attList.push({
                                id: this.attList[i].id,
                                fileCode: this.attList[i].fileCode,
                                fileName: this.attList[i].fileName,
                                opName: this.attList[i].opName,
                                opNo: this.attList[i].opNo,
                                opTime: this.attList[i].opTime,
                                woNo: this.infolist.woNo,
                                type: this.attList[i].type,
                            });
                        }
                        this.infolist.attList = attList;
                        this.loadinged = true;
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: this.infolist
                        }).then((res) => {
                            this.loadinged = false;
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.fileInfo.fileList = [];
                                _this.infolist = {};
                                _this.infolist.deptCode = '';
                                _this.dialogStatus = false;
                                _this.getList(this.params, this.pageSize);
                                _this.defaultList = [];
                                _this.depttype = 0;
                                _this.treeListonly = [];
                                _this.treeList = [];
                            }

                        });
                    }
                })
            },
            dialoginfoCli(v) {
                if (v == 1) {
                    this.historySts = !this.historySts;
                } else if (v == 2) {
                    this.dialogInfo = false;
                    this.dialogStatus = true;
                    this.titname = '修改';
                    this.historySts = false;
                } else if (v == 3) {
                    this.dialogInfo = false;
                    this.infolist = {};
                    this.fileInfo.fileList = [];
                    this.historySts = false;
                }
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    _this.$http({
                        url: "/market/cwo/delWoInfo",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            id: this.delid,
                            woNo: this.delno
                        }
                    }).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;
                this.delno = v.woNo;
            },
            //文件返回值
            uploadBack(v) {
                this.attList = v;
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            //获取部门
            getDepot() {
                this.$http({
                    url: "/sysmgr/sysdept/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.typeOptions = res.data;
                });
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
            }
        },
        mounted() {
            this.getList(this.params, this.pageSize);
            this.getDepot();
            this.getUser();
            if (this.$route.query.id) {
                let params = {
                    woNo: this.$route.query.id + ''
                }
                if (this.$route.query.type == 2) {
                    this.gdstatus = true;
                    this.dialogCheck(2, params);
                } else if (this.$route.query.type == 3) {
                    this.dialogCheck(3, params);
                } else {
                    this.dialogCheck(2, params);
                }
            }
        },
        created() {

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

    .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);
        }

        .tree {
            width: calc(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;
            .el-icon-error {
                float: right;
                font-size: 20px;
                margin-top: 9px;
                cursor: pointer;
            }
        }

        .treeUser {
            margin: 0;
            border: 1px solid #ddd;

            p {
                background: #f4f4f4;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }

        .treeUserb {
            width: calc(100% - 100px);
            border: 1px solid #ddd;
            background: #f4f4f4;
            border-radius: 3px;
            height: auto;
            overflow: hidden;

            p {
                display: inline-block;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }
    }
</style>
