<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>
            <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="sendNo" 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="sendTime" label="派发时间" width="140">
                    </el-table-column>
                 <!--   <el-table-column prop="auditName" label="审核人">
                        <template slot-scope="scope">
                            <span>{{$desensitization(scope.row.auditName,1)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="auditTime" label="审核时间" width="140">
                    </el-table-column> -->
                    <el-table-column prop="stsDesc" label="状态">
                    </el-table-column>
                    <el-table-column label="操作" width="180px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1,scope.row)">查看</el-button>
                            <el-button v-if="scope.row.sts == 2 && scope.row.auditNo == userInfo.loginNoStr" size="mini" type="primary"
                                @click="dialogCheck(3,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="工单任务" :visible.sync="dialogStatus" width="50%" :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>
                            <span>任务标题</span>
                            <el-input v-model="infolist.woTitle" placeholder="标题" :disabled='disableStatus'></el-input>
                        </el-form-item>
                    </div>
                    <el-form-item class="info-line online" v-if="activeName != '3'">
                        <span>接收人员</span>
                        <deptTreeOnly class="tree" @treeCheck="treeCheckonly" :defaultList="defaultList"
                            :type="depttype"></deptTreeOnly>
                        <div class="tree treeUser">
                            <p>{{treeListonly.leaderAuditName}}</p>
                        </div>
                    </el-form-item>

                    <el-form-item class="info-line online">
                        <span>工单备注</span>
                        <el-input v-model="infolist.dealRemark" placeholder="工单备注" type="textarea" :rows="3">
                        </el-input>
                    </el-form-item>
                    <div style="padding-left: 80px" v-if="activeName == '3'">
                        <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo" :fileList="fileInfo.fileList">
                        </myUpload>
                    </div>
                </el-form>

                <div slot="footer" class="dialog-footer myfooter">
                    <el-button type="primary" @click="dialogCli(2)">确 定</el-button>
                    <el-button @click="dialogCli(1)">取 消</el-button>
                </div>
            </div>

        </el-dialog>

        <el-dialog title="查看工单" :visible.sync="dialogInfo" width="70%" :close-on-press-escape="false" :show-close="true"
            :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 :historySts="historySts" :infolist="infolist" :datalist="datalist" :backlist="backlist" :dialogInfo="dialogInfo"></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 == '2' && infolist.auditNo == userInfo.loginNoStr">回 复</el-button>
                <el-button @click="dialoginfoCli(3)">取 消</el-button>
            </div>
        </el-dialog>
    </fullscreen>
</template>
<script>
    import mySearch from "../../../components/search.vue";
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown'
    import toolList from '../../../components/toolList'
    import deptTree from "../../../components/deptTreeUser.vue"
    import deptTreeOnly from "../../../components/deptTreeOnly.vue"
    import orderCheck from "./orderCheck.vue"

    export default {
        components: {
            mySearch,
            myUpload,
            uploadDown,
            toolList,
            deptTree,
            deptTreeOnly,
            orderCheck
        },
        data() {
            const terminalTypeName = (rule, value, callback) => {
                if (!this.infolist.tit) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const brandName = (rule, value, callback) => {
                if (!this.infolist.num) {
                    callback(new Error('不能为空'))
                } else {
                    if (this.infolist.num > 10) {
                        this.infolist.num = 10;
                    }
                    callback()
                }
            }
            const modelType = (rule, value, callback) => {
                if (!this.infolist.time) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    terminalTypeName: [{
                        required: true,
                        trigger: 'blur',
                        validator: terminalTypeName
                    }],
                    brandName: [{
                        required: true,
                        trigger: 'blur',
                        validator: brandName
                    }],
                    modelType: [{
                        required: true,
                        trigger: 'blur',
                        validator: modelType
                    }],
                },
                tooltit: '工单回复',
                searchList: [{
                        type: 'input',
                        tit: '工单标题',
                        value: '',
                        width: '32%',
                    },
                    {
                        type: 'date',
                        tit: '开始时间',
                        value: '',
                        width: '32%',
                    },
                    {
                        type: 'date',
                        tit: '结束时间',
                        value: '',
                        width: '32%',
                    },
                ],
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                disableStatus: false,
                titname: '终端',
                infolist: {},
                typeOptions: [{
                    dataCode: 1,
                    dataName: 111,
                }],
                userInfo: {},
                params: {},
                infoApply: [],
                fileInfo: {
                    limit: 10,
                    url: '/market/cwo/revertUpload',
                    fileList: []
                },
                datalist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 1
                },
                backlist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 1
                },
                datalistback: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 2
                },
                uploadstatus: false,
                visiblec: [],
                attList: [],
                btnctrlStatus: true,
                typelist: [],
                loading: false,
                loadinged: false,
                treeList: [],
                treeListonly: [],
                defaultList: [],
                depttype: 0,
                transfer: false,
                activeName: '3',
                routerList: [{
                    name: '其他人办理',
                    label: '1'
                }, {
                    name: '领导审批',
                    label: '2'
                }, {
                    name: '办理结束',
                    label: '3'
                }, ],
                dialogInfo:false,
                historySts:false,
            }
        },
        methods: {
            treeCheck(v) {
                this.treeList = v;
            },
            treeCheckonly(v) {
                this.treeListonly = v;
            },
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.woTitle = v[0] : '';
                v[1] ? this.params.sendTimeFrom = this.$formatDate(v[1], "YYYY-MM-DD") : '';
                v[2] ? this.params.sendTimeTo = this.$formatDate(v[2], "YYYY-MM-DD") : '';
                // v[3] ? this.params.modelType = v[3] : '';
                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/queryListByTraceDeal",
                    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);
            },
            //申请
            overcheck(v) {
                let _this = this;
                this.$http({
                    url: "/market/cwo/closeWoInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        woNo: v.woNo
                    },
                }).then((res) => {
                    _this.$message({
                        message: '成功',
                        type: 'success'
                    });
                    _this.getList({}, _this.pageSize);
                });
            },
            dialogCheck(v, n) {
                this.defaultList = [];
                if (v == 1) {
                    this.titname = '查看';
                } else if (v == 3) {
                    this.titname = '回复';
                    this.dialogStatus = true;
                    this.disableStatus = false;
                }
                this.$http({
                    url: "/market/cwo/queryInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        woNo: n.woNo,
                        id: n.id,
                    },
                }).then((res) => {
                    this.uploadstatus = true;
                    this.infolist = res.data;
                    this.infolist.sendName = this.$desensitization(res.data.sendName, 1);
                    this.infolist.auditName = this.$desensitization(res.data.auditName, 1);
                    this.visiblec = [];
                    this.treeList = res.data.deptList;
                    this.datalist.attList = res.data.attList;
                    this.backlist.attList = res.data.revertAttList;
                    this.infolist.attList = res.data.attList;
                    if (v == 1) {
                        this.dialogInfo = true;
                    }
                    this.attList = [];
                    this.fileInfo.fileList = [];
                    // if (res.data.revertAttList) {
                    //     res.data.revertAttList.forEach(item => {
                    //         this.fileInfo.fileList.push({
                    //             name: item.fileName,
                    //             url: '',
                    //             id: item.id,
                    //             fileName: item.fileName,
                    //             fileCode: item.fileCode,
                    //         });
                    //         this.attList.push({
                    //             name: item.fileName,
                    //             url: '',
                    //             id: item.id,
                    //             fileName: item.fileName,
                    //             fileCode: item.fileCode,
                    //         });
                    //     })
                    // }
                    this.depttype = 1;
                });
            },
            //添加
            dialogCli(v) {
                if (v === 1) {
                    this.infolist = {};
                    this.dialogStatus = false;
                    this.defaultList = [];
                    this.depttype = 0;
                    return
                } else if (v === 2) {
                    let params = {
                        woNo: this.infolist.woNo,
                        woTitle: this.infolist.woTitle,
                        dealRemark: this.infolist.dealRemark,
                        revertAttList: [],
                    }
                    if(this.activeName == '1'){
                        params.sts = "2";
                        params.stsDesc = "待处理";
                        params.auditName = this.treeListonly.leaderAuditName;
                        params.auditNo = this.treeListonly.leaderAuditNo;
                        if(!this.treeListonly.leaderAuditName){
                            this.$message({
                                message: '请选择接单人！',
                                type: 'error'
                            });
                            return
                        }
                    }else if(this.activeName == '2'){
                        params.sts = "0";
                        params.stsDesc = "待审核";
                        params.leaderAuditName = this.treeListonly.leaderAuditName;
                        params.leaderAuditNo = this.treeListonly.leaderAuditNo;
                        if(!this.treeListonly.leaderAuditName){
                            this.$message({
                                message: '请选择接单人！',
                                type: 'error'
                            });
                            return
                        }
                    }else if(this.activeName == '3'){
                        params.sts = "1";
                        params.stsDesc = "已处理";
                    }
                    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,
                            type: this.attList[i].type,
                            noticeId: this.infolist.noticeId
                        });
                    }
                    params.revertAttList = attList;
                    params.stepSts = this.activeName;
                    if (this.infolist.type == 1) {
                        params.msgType = this.infolist.msgType;
                        params.msgTypeName = this.infolist.msgTypeName;
                        params.msgInfo = this.infolist.dealTypeDesc;
                        params.msgTital = this.infolist.woTitle;
                        this.submitInfo("/market/cwo/recRptInfo", params);
                    } else {
                        this.submitInfo("/market/cwo/recWoInfo", params);
                    }
                }
            },
            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.loadinged = true;
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: v,
                        }).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.infolist = {};
                                _this.dialogStatus = false;
                                _this.getList({}, _this.pageSize);
                                _this.defaultList = [];
                                _this.depttype = 0;
                            }

                        })
                    }
                })
            },
            dialoginfoCli(v) {
                if (v == 1) {
                    this.historySts = !this.historySts;
                } else if (v == 2) {
                    this.dialogInfo = false;
                    this.dialogStatus = true;
                    this.historySts = false;
                    this.titname = '修改';
                } else if (v == 3) {
                    this.dialogInfo = false;
                    this.historySts = false;
                    this.infolist = {};
                }
            },
            //文件返回值
            uploadBack(v) {
                this.attList = v;
            },
            //功能栏
            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);
            },
            getType() {
                this.$http({
                    url: "/market/qatype/queryPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + 1 + '","pageSize":"100"}'
                    },
                    data: {},
                }).then((res) => {
                    this.typelist = res.data.data;
                });
            },
            changemsgType() {
                for (let i = 0; i < this.typelist.length; i++) {
                    if (this.infolist.msgType === this.typelist[i].id) {
                        this.infolist.msgTypeName = this.typelist[i].msgType;
                    }
                }
            },
        },
        mounted() {
            this.getList({}, 1);
            this.getUser();
            this.getType();
            if (this.$route.query.id) {
                let params = {
                    woNo: this.$route.query.id + ''
                }
                this.dialogCheck(this.$route.query.type, params);
            }
        },
        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);
        }

        .tree {
            width: calc(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;
        }

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