<template>
    <div class="mark-setting-wrap light-iview-form">
        <div v-show="!isSetting" class="publish-status-box">
            <Icon type="ios-information-circle-outline" class="tips-icon" />
            <p class="status-text">
                <span style="font-size:16px;">{{$t('learnActivity.mark.noPublish')}}</span>
            </p>
            <span class="setting-btn" @click="isSetting = !isSetting">{{$t('learnActivity.mark.publish')}}</span>
        </div>
        <vuescroll v-show="isSetting">
            <!-- 基础设置 -->
            <div class="setting-block">
                <div class="setting-content">
                    <Form ref="baseSetting" :model="setting" label-colon :label-width="100" :rules="ruleValidate">
                        <Row>
                            <!-- 开始时间 -->
                            <Col :md="24" :lg="24" :xl="12" :xxl="8">
                            <FormItem :label="$t('learnActivity.mark.startTime')" class="setting-item-wrap" prop="startTime">
                                <DatePicker :options="dateOpt" class="attr-value-wrap" type="datetime" format="yyyy/MM/dd HH:mm" v-model="startTime" split-panels :placeholder="$t('learnActivity.createEv.sTimeHolder')" style="width:100%" @on-change="getDate($event,0)"></DatePicker>
                            </FormItem>
                            </Col>
                            <!-- 结束时间 -->
                            <Col :md="24" :lg="24" :xl="12" :xxl="8">
                            <FormItem :label="$t('learnActivity.mark.endTime')" class="setting-item-wrap" prop="endTime">
                                <DatePicker :options="dateOpt1" class="attr-value-wrap" type="datetime" format="yyyy/MM/dd HH:mm" v-model="endTime" split-panels :placeholder="$t('learnActivity.createEv.eTimeHolder')" style="width:100%" @on-change="getDate($event,1)"></DatePicker>
                            </FormItem>
                            </Col>
                            <!-- 分配方式 -->
                            <Col :md="24" :lg="24" :xl="12" :xxl="8">
                            <FormItem :label="$t('learnActivity.mark.allocation')" class="setting-item-wrap" prop="mode">
                                <RadioGroup v-model="setting.mode">
                                    <Radio label="stu">
                                        {{$t('learnActivity.mark.allocationByStu')}}
                                        <Tooltip :content="$t('learnActivity.mark.byStuTips')">
                                            <Icon type="md-help-circle" color="#a5a5a5" />
                                        </Tooltip>
                                    </Radio>
                                    <Radio label="qu" style="margin-left:20px">
                                        {{$t('learnActivity.mark.allocationByQu')}}
                                        <Tooltip :content="$t('learnActivity.mark.byQuTips')">
                                            <Icon type="md-help-circle" color="#a5a5a5" />
                                        </Tooltip>
                                    </Radio>
                                </RadioGroup>
                            </FormItem>
                            </Col>
                            <!-- 阅卷次数 -->
                            <Col :md="24" :lg="24" :xl="12" :xxl="8">
                            <FormItem :label="$t('learnActivity.mark.markTimes')" class="setting-item-wrap" prop="num">
                                <!-- <InputNumber :max="2" class="attr-value-wrap" :min="1" v-model="setting.point"></InputNumber> -->
                                <RadioGroup v-model="setting.num">
                                    <Radio :label="1">{{$t('learnActivity.mark.markNum1')}}</Radio>
                                    <Radio :label="2" style="margin-left:0px">{{$t('learnActivity.mark.markNum2')}}</Radio>
                                    <Radio :label="3" style="margin-left:0px">{{$t('learnActivity.mark.markNum3')}}</Radio>
                                </RadioGroup>
                            </FormItem>
                            </Col>
                            <!-- 启用仲裁 -->
                            <!-- <Col :md="24" :lg="24" :xl="12" :xxl="8" v-show="setting.num > 1">
                            <FormItem :label="$t('learnActivity.mark.openArb')" class="setting-item-wrap" prop="isArb">
                                <RadioGroup v-model="setting.isArb" style="color:white">
                                    <Radio :label="1">{{$t('learnActivity.mark.yes')}}</Radio>
                                    <Radio :label="0" style="margin-left:20px">{{$t('learnActivity.mark.no')}}</Radio>
                                </RadioGroup>
                            </FormItem>
                            </Col> -->

                        </Row>
                    </Form>
                </div>
            </div>
            <div class="subject-wrap">
                <span v-for="(item,index) in subjects" :key="index" :class="index == curSubIndex ? 'filter-tab-item filter-tab-item-active':'filter-tab-item'" @click="curSubIndex = index">
                    {{item.name}}
                </span>
            </div>

            <!-- 阅卷老师设置 -->
            <div class="setting-block" id="teacher-wrap">
                <div class="setting-content" id="teacher-wrap-content">
                    <Form ref="teacherSet" :model="subsInfo" label-colon :label-width="100" :rules="ruleValidate" style="margin-top:15px">
                        <Row>
                            <Col :md="24" :lg="24" :xl="24" :xxl="24">
                            <!-- 异常处理老师 -->
                            <FormItem :label="$t('learnActivity.mark.errRole')" class="setting-item-wrap" prop="err">
                                <Select v-model="subsInfo.err" style="width:400px" multiple filterable>
                                    <Option v-for="(item,index) in teacherList" :value="item.id" :key="index">{{item.name}}</Option>
                                </Select>
                            </FormItem>
                            </Col>
                            <Col :md="24" :lg="24" :xl="24" :xxl="24" v-if="setting.num > 1">
                            <!-- 仲裁老师 -->
                            <FormItem :label="$t('learnActivity.mark.arb')" class="setting-item-wrap" prop="arb">
                                <Select v-model="subsInfo.arb" style="width:400px" multiple filterable>
                                    <Option v-for="(item,index) in teacherList" :value="item.id" :key="index">{{item.name}}</Option>
                                </Select>
                            </FormItem>
                            </Col>
                            <Col :md="24" :lg="24" :xl="24" :xxl="24" v-if="setting.mode == 'stu'">
                            <!-- 阅卷老师 -->
                            <FormItem :label="$t('learnActivity.mark.markRole')" class="setting-item-wrap" prop="markers">
                                <Table :columns="teaCol" :data="markers" border :no-data-text="$t('learnActivity.mark.noTea')">
                                    <template slot-scope="{ row }" slot="header">
                                        <PersonalPhoto :name="row.name" :picture="row.picture" />
                                    </template>
                                    <template slot-scope="{ row,index }" slot="action">
                                        <Button v-show="setting.mode == 'qu'" type="info" size="small" style="margin-right:10px" @click="setQuStatus = true">
                                            {{$t('learnActivity.mark.quIndex')}}
                                        </Button>
                                        <Button type="error" size="small" @click="removeMarker(row,index)">{{$t('learnActivity.mark.remove')}}</Button>
                                    </template>
                                    <template slot-scope="{ row }" slot="count">
                                        <span>{{`${row.count}/${evInfo.stuCount * setting.num}`}}</span>
                                    </template>
                                    <template slot-scope="{ row }" slot="qu">
                                        <span :style="{color:setting.mode == 'stu' ? '#2db7f5' : row.qu.length ? '#FFFFFF' : '#ed4014'}">
                                            {{setting.mode == 'stu' ? $t('learnActivity.mark.allQu') : row.qu.length ? row.qu.join(',') : $t('learnActivity.mark.noSet')}}
                                        </span>
                                    </template>
                                </Table>
                                <span class="add-tea-btn" v-if="$access.can('admin.*|schoolAc-upd')" @click="addTeaStatus = true">
                                    <Icon type="md-add" />
                                    {{$t('learnActivity.mark.addTea')}}
                                </span>
                            </FormItem>
                            </Col>
                            <Col :md="24" :lg="24" :xl="24" :xxl="24" v-else>
                            <!-- 题目划块 -->
                            <FormItem :label="$t('learnActivity.mark.quBlock')" class="setting-item-wrap">
                                <Table :columns="blockCol" :data="quBlockData" border>
                                    <template slot-scope="{ row,index }" slot="action">
                                        <Button type="info" size="small" style="margin-right:10px" @click="editBlock(row)">
                                            {{$t('learnActivity.mgtScEv.edit')}}
                                        </Button>
                                        <Button type="error" size="small" @click="removeBlock(row,index)">{{$t('learnActivity.mark.remove')}}</Button>
                                    </template>
                                    <template slot-scope="{ row }" slot="num">
                                        <span>{{evInfo.stuCount * setting.num}}</span>
                                    </template>
                                    <template slot-scope="{ row }" slot="quLabel">
                                        <span style="word-break: break-word;">
                                            {{getQuLabels(row.quNo).join(', ')}}
                                        </span>
                                    </template>
                                    <template slot-scope="{ row }" slot="teaCount">
                                        <span style="word-break: break-word;">
                                            {{row.teachers ? row.teachers.length : 0}}
                                        </span>
                                    </template>
                                    <template slot-scope="{ row }" slot="teachers">
                                        <span style="word-break: break-word;">
                                            {{row.teachers.map(item=>item.name).join(', ')}}
                                        </span>
                                    </template>
                                </Table>
                                <span class="add-tea-btn" v-if="$access.can('admin.*|schoolAc-upd')" @click="setQuStatus = true">
                                    <Icon type="md-add" />
                                    {{$t('learnActivity.mark.addBlock')}}
                                </span>
                            </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </div>
            </div>

            <!-- 阅卷数据统计 -->
            <MarkData v-show="isShowData"></MarkData>
        </vuescroll>
        <!-- 按人分配设置阅卷老师 -->
        <Modal v-model="addTeaStatus" :title="$t('learnActivity.mark.addTeaTitle')" @on-ok="okAddTea" :width="1000">
            <Table ref="sltTea" :columns="teacherCol" :data="teacherList" style="margin-top:10px" @on-selection-change="(selection)=>{sltTeachers = selection}" height="600">
                <template slot-scope="{ row }" slot="picture">
                    <PersonalPhoto :name="row.name" :picture="row.picture" />
                </template>
                <template slot-scope="{ row }" slot="job">
                    <span>{{row.job || '--'}}</span>
                </template>
            </Table>
        </Modal>
        <!-- 按题分配设置题块和老师 -->
        <Modal v-model="setQuStatus" @on-ok="okSetQu" :width="1000" :loading="mLoading">
            <Tabs value="name1" style="color:white">
                <TabPane :label="$t('learnActivity.mark.quLabel')" name="name1">
                    <CheckboxGroup v-model="quIds" style="margin-left:10px">
                        <Checkbox :disabled="item.disabled" v-for="(item,index) in quNoList" :key="index" :label="item.value">
                            <span class="qu-no-label">
                                {{item.label}}
                                <span v-show="item.disabled" style="font-size:12px;color:#ff9900">
                                    {{$t('learnActivity.mark.objectiveLabel')}}
                                </span>
                                <span v-show="assignQus.includes(item.value)" style="font-size:12px;color:#19be6b">
                                    {{$t('learnActivity.mark.alreadyLabel')}}
                                </span>
                            </span>
                        </Checkbox>
                    </CheckboxGroup>
                    <p class="objective-tips">{{$t('learnActivity.mark.objcTips')}}</p>
                </TabPane>
                <TabPane :label="$t('learnActivity.mark.teaLabel')" name="name2">
                    <Table ref="quTea" :columns="teacherCol" :data="teacherList" @on-selection-change="(selection)=>{quTeachers = selection}" height="600">
                        <template slot-scope="{ row }" slot="picture">
                            <PersonalPhoto :name="row.name" :picture="row.picture" />
                        </template>
                        <template slot-scope="{ row }" slot="job">
                            <span>{{row.job || '--'}}</span>
                        </template>
                    </Table>
                </TabPane>
            </Tabs>
        </Modal>
    </div>
</template>
<script>
import MarkProgress from '../echarts/MarkProgress.vue'
import ScanProgress from '../echarts/ScanProgress.vue'
import CptCount from '../echarts/CptCount.vue'
import PersonalPhoto from "@/components/public/personalPhoto/Index.vue"
import MarkData from './MarkData.vue'
import { mapGetters } from 'vuex'
export default {
    model: {
        prop: 'isShow',
        event: 'statusChange'
    },
    components: {
        MarkProgress, CptCount, ScanProgress, PersonalPhoto, MarkData
    },
    props: {
        evInfo: {
            type: Object,
            default: () => {
                return {
                    id: '',
                    name: ''
                }
            }
        },
        isShow: Boolean //父组件是否显示保存按钮 对应isSetting的值
    },
    data() {
        let _this = this
        return {
            quIds: [],
            mLoading: true,
            isSetting: false,
            isShowData: false,
            ruleValidate: {
                startTime: [
                    { required: true, type: 'number', message: this.$t('learnActivity.mark.startErr'), trigger: 'blur' }
                ],
                endTime: [
                    { required: true, type: 'number', message: this.$t('learnActivity.mark.endErr'), trigger: 'blur' }
                ],
                mode: [
                    { required: true, message: this.$t('learnActivity.mark.typeErr'), trigger: 'change' }
                ],
                num: [
                    { required: true, type: 'number', message: this.$t('learnActivity.mark.numErr'), trigger: 'change' }
                ],
                isArb: [
                    { required: true, type: 'number', message: this.$t('learnActivity.mark.isArbTips'), trigger: 'change' }
                ],
                arb: [
                    { required: true, type: 'array', message: this.$t('learnActivity.mark.arbTeaTips'), trigger: 'change' }
                ],
                err: [
                    { required: true, type: 'array', message: this.$t('learnActivity.mark.errTeaTips'), trigger: 'change' }
                ],
                markers: [
                    { required: true, type: 'array', message: this.$t('learnActivity.mark.markerErr'), trigger: 'change' }
                ],
            },
            setQuStatus: false,
            curSubIndex: 0,
            setting: {
                id: '',
                code: '',
                name: '',
                startTime: '',
                createTime: '',
                endTime: '',
            },
            quBlockes: [], //按题分配数据结构
            paperQuList: [],//各科试卷题目序号数据
            addTeaStatus: false,
            sltTeachers: [], //按人分配时，设置老师
            quTeachers: [], //按题分配时，设置老师
            teacherCol: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: ' ',
                    slot: 'picture',
                    align: 'center ',
                    width: '120'
                },
                {
                    title: this.$t('cusMgt.teaName'),
                    key: 'name',
                    align: 'center '
                },
                {
                    title: 'id',
                    key: 'id',
                    align: 'center '
                },
                {
                    title: this.$t('cusMgt.job'),
                    slot: 'job',
                    align: 'center '
                }
            ],
            teaCol: [
                {
                    title: ' ',
                    slot: 'header',
                    align: 'center'
                },
                {
                    title: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: this.$t('learnActivity.mark.name'),
                    key: 'name',
                    align: 'center'
                },
                {
                    title: this.$t('learnActivity.mark.markNum'),
                    slot: 'count',
                    align: 'center'
                },
                {
                    title: this.$t('learnActivity.mark.markQuNo'),
                    slot: 'qu',
                    align: 'center'
                },
                {
                    title: '',
                    slot: 'action',
                    align: 'center'
                },
            ],
            blockCol: [
                {
                    title: this.$t('learnActivity.mark.orderIndex'),
                    type: 'index',
                    align: 'center',
                    width: 80
                },
                {
                    title: this.$t('learnActivity.mark.quNo'),
                    slot: 'quLabel',
                    align: 'center',
                    width: 180
                },
                {
                    title: this.$t('learnActivity.mark.markTea'),
                    slot: 'teachers',
                    align: 'center'
                },
                {
                    title: this.$t('learnActivity.mark.teaCount'),
                    slot: 'teaCount',
                    align: 'center',
                    width: 120
                },
                {
                    title: this.$t('learnActivity.mark.markCount'),
                    slot: 'num',
                    align: 'center',
                    width: 120
                },
                {
                    title: '',
                    slot: 'action',
                    align: 'center',
                    width: 150
                },
            ],
            dateOpt: {
                disabledDate(date) {
                    return date && date.valueOf() < Date.now() - 86400000
                }
            },
            dateOpt1: {
                disabledDate(date) {
                    let d = _this.setting.startTime ? _this.setting.startTime : Date.now()
                    return d && d > date.valueOf() + 86400000
                }
            },
            startTime: '',
            endTime: '',
            editId: ''
        }
    },
    methods: {
        getQuLabels(quNo) {
            return this.quNoList.filter(item => {
                return quNo.includes(item.value)
            }).map(item => {
                return item.label
            })
        },
        /**
         * index 题目index 必传
         * childIndex 小题index 非必传 
         */
        getScoreIndex(index, childIndex) {
            let realIndex = 0
            this.curPaper.item.forEach((item, itemIndex) => {
                if (itemIndex <= index) {
                    //综合题
                    if (item.children.length) {
                        item.children.forEach((childItem, cIndex) => {
                            if (itemIndex < index) {
                                realIndex++
                            } else if (cIndex <= childIndex) {
                                realIndex++
                            }
                        })
                    }
                    else {
                        realIndex++
                    }
                }
            })
            return --realIndex
        },
        //移除阅卷老师
        removeMarker(row, index) {
            if (this.$access.can('admin.*|schoolAc-upd')) {
                this.$Modal.confirm({
                    title: this.$t('learnActivity.mark.rmvTitle'),
                    content: `${this.$t('learnActivity.mark.rmvContent')}${row.name}?`,
                    okText: this.$t('learnActivity.mark.yes'),
                    cancelText: this.$t('learnActivity.mark.no'),
                    onOk: () => {
                        let subject = this.setting.subs.find(item => {
                            return this.subjects[this.curSubIndex].id == item.id
                        })
                        for (let index in subject.markers) {
                            if (row.id == subject.markers[index].id) {
                                subject.markers.splice(index, 1)
                                break
                            }
                        }
                        //重新计算阅卷任务量
                        let count = this.evInfo.stuCount * this.setting.num
                        let more = count % subject.markers.length
                        subject.markers.map((item, index) => {
                            if (index < more) {
                                item.count = Math.ceil(count / subject.markers.length)
                            } else {
                                item.count = Math.floor(count / subject.markers.length)
                            }

                        })
                    }
                })
            } else {
                this.$Message.error(this.$t('system.authErr'))
            }
        },
        //移除题目分块
        removeBlock(row, index) {
            this.$Modal.confirm({
                title: this.$t('learnActivity.mark.delBlockTitle'),
                content: this.$t('learnActivity.mark.delBlockCont'),
                onOk: () => {
                    let blockData = this.quBlockes.find(item => {
                        return item.id == this.subjects[this.curSubIndex].id
                    })
                    if (blockData) {
                        blockData.quBlock.splice(index, 1)
                    }
                }
            })
        },
        // 编辑题目分块
        editBlock(row) {
            this.editId = row.id
            this.quIds = row.quNo
            this.quTeachers = row.teachers
            //初始化教师选中状态
            if (this.quTeachers && this.quTeachers.length) {
                let ids = this.quTeachers.map(item => {
                    return item.id
                })
                for (let i in this.$refs.quTea.$refs.tbody.objData) {
                    this.$refs.quTea.$refs.tbody.objData[i]._isChecked = ids.includes(this.$refs.quTea.$refs.tbody.objData[i].id)
                }
            }
            this.setQuStatus = true
        },
        //确认设置题目划块
        okSetQu() {
            if (!this.quIds.length) {
                this.$Message.warning(this.$t('learnActivity.mark.selectQuTips'))
                this.mLoading = false
                setTimeout(() => {
                    this.mLoading = true
                }, 0)
                return
            }
            if (!this.quTeachers.length) {
                this.$Message.warning(this.$t('learnActivity.mark.selectTeaTips'))
                this.mLoading = false
                setTimeout(() => {
                    this.mLoading = true
                }, 0)
                return
            }
            this.setQuStatus = false
            //计算任务量
            if (this.quTeachers.length) {
                let count = this.evInfo.stuCount * this.setting.num
                let more = count % this.quTeachers.length
                this.quTeachers.map((item, index) => {
                    if (index < more) {
                        item.count = Math.ceil(count / this.quTeachers.length)
                    } else {
                        item.count = Math.floor(count / this.quTeachers.length)
                    }
                })
            }
            let blockData = this.quBlockes.find(item => {
                return item.id == this.subjects[this.curSubIndex].id
            })
            if (!blockData) {
                // this.$Message.error('数据错误')
                return
            }
            // 编辑
            if (this.editId) {
                let block = blockData.quBlock.find(item => {
                    return item.id == this.editId
                })
                if (block) {
                    block.quNo = [...this.quIds]
                    block.teachers = [...this.quTeachers]
                }
            }
            // 新增
            else {
                blockData.quBlock.push({
                    id: this.$jsFn.uuid(),
                    quNo: [...this.quIds],
                    teachers: [...this.quTeachers]
                })
            }
            //初始化状态
            this.editId = ''
            this.quIds = []
            this.quTeachers = []
            this.$refs.quTea.selectAll(false)
        },
        /**
         * 将日期控件时间格式转成时间戳
         * @param value
         * @param flag 0:开始时间 1：结束时间
         */
        getDate(value, flag) {
            if (value.indexOf('00:00') > 0) {
                value = value.replace('00:00', '23:59:59')
            }
            if (flag == 0) {
                this.startTime = value
                this.setting.startTime = new Date(value).getTime()
                if (this.setting.startTime >= this.setting.endTime) {
                    this.endTime = undefined
                    this.setting.endTime = undefined
                }
            } else if (flag == 1) {
                this.endTime = value
                this.setting.endTime = new Date(value).getTime()
            }
        },
        saveSetting() {
            let full = true
            //验证基础信息设置
            this.$refs['baseSetting'].validate((valid) => {
                if (!valid) {
                    full = false
                    this.$Message.error(this.$t('learnActivity.mark.baseErr'))
                }
            })
            if (!full) return
            // 验证学科阅卷相关老师设置 待完善，目前验证了当前学科
            this.$refs['teacherSet'].validate((valid) => {
                if (!valid) {
                    full = false
                    this.$Message.error(this.$t('learnActivity.mark.teacherErr'))
                }
            })
            if (!full) return

            if (this.setting.mode == 'qu') {
                //验证按题分配模式 题目分块数据
                let res = this.quBlockes.some(dataItem => {
                    let allNo = []
                    dataItem.quBlock.forEach(item => {
                        allNo.push(...item.quNo)
                    })
                    let quListData = this.paperQuList.find(pQu => {
                        return pQu.id == dataItem.id
                    })
                    //1、验证题目是否完全分配 some方法返回true的时候就会break循环， forEach方法不能break
                    let last = quListData.quList.some(item => {
                        return !allNo.includes(item.value) && !item.disabled
                    })
                    if (last) {
                        this.$Message.error(this.$t('learnActivity.mark.lastQu'))
                        return true
                    }

                    //2、验证题目是否重复分配
                    let rep = allNo.some((item, index) => {
                        return allNo.indexOf(item) != index
                    })
                    if (rep) {
                        this.$Message.error(this.$t('learnActivity.mark.reapQu'))
                        return true
                    }
                })
                if (res) return
                //通过验证，将按题分配数据结构转成标准API结构

                this.quBlockes.forEach(subItem => {
                    let curSubData = this.setting.subs.find(item => {
                        return item.id == subItem.id
                    })
                    if (curSubData) {
                        curSubData.markers = []
                        curSubData.model = subItem.quBlock.length
                        subItem.quBlock.forEach(block => {
                            let count = this.evInfo.stuCount * this.setting.num
                            let more = count % block.teachers.length
                            block.teachers.forEach((teacher, index) => {
                                let c
                                if (index < more) {
                                    c = Math.ceil(count / block.teachers.length)
                                } else {
                                    c = Math.floor(count / block.teachers.length)
                                }
                                curSubData.markers.push({
                                    id: teacher.id,
                                    name: teacher.name,
                                    qu: block.quNo.sort((a, b) => {
                                        return a - b
                                    }),
                                    count: c
                                })
                            })
                        })
                    }
                })
            } else {
                //如果先设置按题分配，然后切换成按人分配需要清空qu
                this.setting.subs.forEach(subItem => {
                    subItem.markers.forEach(t => {
                        t.qu = []
                    })
                })
            }
            console.log('完整数据', this.setting)

            // 通过验证，保存设置
            this.setting.code = this.$store.state.userInfo.schoolCode
            this.$api.mark.UpsertMarkSet(this.setting).then(
                res => {
                    this.$Message.success(this.$t('learnActivity.mark.saveOk'))
                },
                err => {
                    this.$Message.error(this.$t('learnActivity.mark.saveErr'))
                }
            )
        },
        //确认添加老师
        okAddTea() {
            let subject = this.setting.subs.find(item => {
                return this.subjects[this.curSubIndex].id == item.id
            })
            if (subject) {
                let ids = subject.markers.map(item => {
                    return item.id
                })
                this.sltTeachers.forEach(item => {
                    if (ids.indexOf(item.id) == -1) {
                        subject.markers.push({
                            id: item.id,
                            name: item.name,
                            qu: []
                        })
                    }
                })
                let count = this.evInfo.stuCount * this.setting.num
                let more = count % subject.markers.length
                subject.markers.map((item, index) => {
                    if (index < more) {
                        item.count = Math.ceil(count / subject.markers.length)
                    } else {
                        item.count = Math.floor(count / subject.markers.length)
                    }

                })
            }
            this.cancel()
        },
        cancel() {
            this.sltTeachers = []
            this.$refs.sltTea.selectAll(false)
        },
        //查询评测阅卷配置信息
        findSettingInfo() {
            let requestData = {
                id: this.evInfo.id,
                code: this.$store.state.userInfo.schoolCode
            }
            this.$api.mark.FindById(requestData).then(
                res => {
                    if (res.correct) {
                        this.setting = res.correct
                        this.isSetting = true
                        this.isShowData = true
                        //如果是按题分配模式需要转换数据格式
                        if (this.setting.mode == 'qu') {
                            this.quBlockes = []
                            this.setting.subs.forEach(subItem => {
                                let item = {
                                    id: subItem.id,
                                    name: subItem.name,
                                    quBlock: []
                                }
                                //根据题块进行分组
                                let group = this.$jsFn.groupBy(subItem.markers, 'qu')
                                group.forEach(gItem => {
                                    let qItem = {
                                        id: this.$jsFn.uuid(),
                                        quNo: gItem[0].qu,
                                        teachers: []
                                    }
                                    gItem.forEach(tItem => {
                                        qItem.teachers.push({
                                            id: tItem.id,
                                            name: tItem.name
                                        })
                                    })
                                    item.quBlock.push(qItem)
                                })
                                this.quBlockes.push(item)
                            })
                            console.log('处理结果', this.quBlockes)
                        }
                    } else {
                        this.isSetting = false
                        if (this.evInfo.subjects) {
                            this.setting = {
                                id: this.evInfo.id,
                                source: this.evInfo.source,
                                name: this.evInfo.name,
                                school: this.$store.state.userInfo.schoolCode,
                                code: this.$store.state.userInfo.schoolCode,
                                scode: this.evInfo.code,
                                creatorId: this.$store.state.userInfo.TEAMModelId,
                                mode: 'stu',
                                num: 1,
                                isArb: 0, //是否有仲裁处理流程 0/1
                                createTime: new Date().getTime(),
                                startTime: new Date().getTime(),
                                endTime: new Date().getTime() + 86400000 * 2,
                                subs: []
                            }

                            //根据科目信息初始化各科基础数据
                            this.quBlockes = []
                            this.evInfo.subjects.forEach((item) => {
                                //初始化标准按人分配数据结构
                                this.setting.subs.push({
                                    id: item.id,
                                    name: item.name,
                                    markers: [],
                                    model: 1
                                })
                                //初始化按题分配的数据结构
                                this.quBlockes.push({
                                    id: item.id,
                                    name: item.name,
                                    quBlock: []
                                })
                            })

                            // 根据试卷信息初始化数据
                            this.paperQuList = []
                            this.evInfo.papers.forEach(paper => {
                                //初始化各科试卷题号数据
                                if (paper.item.length) {
                                    let objectiveQu = ['single', 'multiple', 'judge']
                                    let data = []
                                    let realIndex = 0
                                    paper.item.forEach((item, index) => {
                                        if (item.children.length) {
                                            item.children.forEach((childItem, childIndex) => {
                                                data.push({
                                                    label: (index + 1) + '-' + (childIndex + 1),
                                                    value: realIndex++,
                                                    disabled: objectiveQu.includes(childItem.type)
                                                })
                                            })
                                        } else {
                                            data.push({
                                                label: index + 1,
                                                value: realIndex++,
                                                disabled: objectiveQu.includes(item.type)
                                            })
                                        }
                                    })
                                    this.paperQuList.push({
                                        id: paper.subjectId,
                                        name: paper.subjectName,
                                        quList: data
                                    })
                                }
                            })
                        }
                    }
                },
                err => {

                }
            ).finally(() => {

            })

        }
    },
    created() {
        //由DB取得該校所有使用者並放入state.schoolUserList
        this.$store.dispatch('user/getSchoolTeacher').then(
            res => {
                if (res.code == 0) {
                    this.$Message.error('無法取得使用者資料')
                }
            },
            err => {
                this.$Message.error('user/setSchoolTeacher API error!')
            }
        )
        //初始化起止时间
        this.startTime = new Date()
        this.setting.startTime = new Date().getTime()
        this.endTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 3 * 24 * 60 * 60 * 1000 - 1)
        this.setting.endTime = new Date().getTime() + 3 * 24 * 60 * 60 * 1000 - 1
    },
    watch: {
        'evInfo.id': {
            handler(n, o) {
                this.curSubIndex = 0
                if (this.evInfo.id) {
                    this.findSettingInfo()
                } else {
                    this.$Message.warning('no exam id!')
                }

            },
            deep: true
        },
        isSetting: {
            handler(n, o) {
                this.$emit('statusChange', this.isSetting)
            },
            immediate: true
        },
        'setting.num': {
            handler(n, o) {
                if (this.setting.subs) {
                    let count = this.evInfo.stuCount * this.setting.num
                    this.setting.subs.forEach((item, subIndex) => {
                        if (item.markers && item.markers.length) {
                            let more = count % item.markers.length
                            item.markers.map((m, index) => {
                                if (index < more) {
                                    this.$set(this.setting.subs[subIndex].markers[index], 'count', Math.ceil(count / item.markers.length))
                                } else {
                                    this.$set(this.setting.subs[subIndex].markers[index], 'count', Math.floor(count / item.markers.length))
                                    // m.count = Math.floor(count / item.markers.length)
                                }
                            })
                        }
                    })
                }
            }
        }
    },
    computed: {
        ...mapGetters({
            teacherList: 'user/getSchoolUserJoined', // 取得已加入此學校的使用者
        }),
        //当前科目按题分配数据
        quBlockData() {
            let blockData = this.quBlockes.find(item => {
                return item.id == this.subjects[this.curSubIndex].id
            })
            return blockData ? blockData.quBlock : []
        },
        // 当前科目已分配的题号
        assignQus() {
            if (this.quBlockData.length) {
                let data = []
                this.quBlockData.forEach(item => {
                    data.push(...item.quNo)
                })
                return data
            } else {
                return []
            }
        },
        //当前科目的配置信息
        subsInfo() {
            let info = {
                arb: [],
                err: [],
                markers: []
            }
            if (this.setting.subs && this.subjects && this.subjects[this.curSubIndex]) {
                let subject = this.setting.subs.find(item => {
                    return item.id == this.subjects[this.curSubIndex].id
                })
                if (subject) {
                    info = subject
                }
            }
            return info
        },
        //从基础信息获取当前学科阅卷老师
        markers() {
            let arr = []
            if (this.setting.num && this.setting.subs && this.subjects && this.subjects[this.curSubIndex]) {
                let subject = this.setting.subs.find(item => {
                    return item.id == this.subjects[this.curSubIndex].id
                })
                if (subject) {
                    arr = [...subject.markers]
                }
            }
            return arr
        },
        //当前评测科目
        subjects() {
            if (this.evInfo && this.evInfo.subjects) {
                return this.evInfo.subjects
            } else {
                return []
            }
        },
        //当前科目试卷信息
        curPaper() {
            let defData = {
                item: []
            }
            if (this.subjects.length) {
                let curSub = this.subjects[this.curSubIndex]
                if (curSub) {
                    let paper = this.evInfo.papers.find(item => {
                        return item.subjectId == curSub.id
                    })
                    if (paper) {
                        return paper
                    }
                }
            }
            return defData
        },
        //试卷题号列表
        quNoList() {
            if (this.curPaper.item.length) {
                let objectiveQu = ['single', 'multiple', 'judge']
                let data = []
                let realIndex = 0
                this.curPaper.item.forEach((item, index) => {
                    if (item.children.length) {
                        item.children.forEach((childItem, childIndex) => {
                            data.push({
                                label: (index + 1) + '-' + (childIndex + 1),
                                value: realIndex++,
                                disabled: objectiveQu.includes(childItem.type)
                            })
                        })
                    } else {
                        data.push({
                            label: index + 1,
                            value: realIndex++,
                            disabled: objectiveQu.includes(item.type)
                        })
                    }
                })
                return data
            }
            return []
        }
    }
}
</script>
<style scoped lang="less">
.objective-tips {
    color: #ff9900;
    margin-bottom: 15px;
    padding-left: 10px;
    font-size: 12px;
    margin-top: 20px;
}
.qu-no-label {
    color: white;
    margin-right: 15px;
    margin-bottom: 10px;
    width: 60px;
    display: inline-block;
}
.publish-status-box {
    width: 360px;
    height: 315px;
    // background: #37363a;
    margin: 60px auto;
    padding: 20px;
    border-radius: 20px;
    position: relative;
    box-shadow: 0 0px 10px 0px #d5d5d5;

    &:hover {
        // background: #3c3c41;
    }
    .tips-icon {
        display: block;
        margin: auto;
        margin-top: 6%;
        font-size: 80px;
        margin-right: 5px;
        // color: #eeeeee;
    }
    .status-text {
        // color: #ccc;
        text-align: center;
        margin-top: 40px;
        font-size: 14px;
    }
    .setting-btn {
        display: block;
        color: white;
        background: #2db7f5;
        width: 120px;
        padding: 4px 0px;
        border-radius: 4px;
        position: absolute;
        text-align: center;
        bottom: 50px;
        left: 50%;
        font-size: 16px;
        margin-left: -60px;
        cursor: pointer;
    }
}
.subject-wrap {
    margin-top: 30px;
    color: #a5a5a5;
}
#teacher-wrap {
    margin-top: 0px;
}
#teacher-wrap-content {
    margin-top: 0px;
    border-top-left-radius: 0px;
}
.filter-tab-item {
    display: inline-block;
    width: 80px;
    text-align: center;
    background: #1a191c;
    color: #a5a5a5;
    margin-right: 1px;
    border-top-right-radius: 15px;
    padding: 2px 0px;
    font-size: 12px;
    cursor: pointer;
}
.filter-tab-item-active {
    background: #70B1E7;
    color: white;
}
.mark-setting-wrap {
    height: calc(100% - 45px);
    padding-bottom: 5px;
}
.setting-block:first-child {
    margin-top: 15px;
}

.setting-content {
    // background: #2b2a2f;
    // box-shadow: 1px 1px 13px #141414;
    border-bottom: 1px solid var(--border-color);
    padding: 10px;
    border-radius: 5px;
    margin-right: 12px;
    margin-top: 5px;
}
.setting-item-wrap {
    padding-right: 15px;
}
.attr-value-wrap {
    width: 90%;
    max-width: 180px;
}
.block-title {
    color: white;
    border-left: 2px solid #1cc0f3;
    line-height: 14px;
    padding-left: 5px;
    user-select: none;
}
.setting-block {
    margin-top: 30px;
}
.count-wrap {
    flex: 1;
    text-align: center;
    color: white;
    width: 200px;
    position: relative;
    .count-icon {
        color: #a5a5a5;
    }
    .count-num {
        font-size: 40px;
    }
    .count-label {
        color: #a5a5a5;
    }
}
.count-wrap::after {
    content: "";
    width: 2px;
    height: 30px;
    background: #a5a5a5;
    position: absolute;
    right: 0px;
    top: 50%;
    margin-top: -15px;
}
.count-wrap:last-child::after {
    display: none;
}
.count-content {
    display: flex;
}
.chart-wrap {
    flex: 1;
    color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.role-item-wrap {
    margin: 5px 0px;
    .role-label {
        color: #a5a5a5;
        display: inline-block;
        // width: 120px;
        text-align: right;
        padding-right: 10px;
    }
}
.scan-tea-table {
    width: calc(100% - 120px);
    display: inline-block;
}
.add-tea-btn {
    color: white;
    margin-top: 8px;
    display: inline-block;
    cursor: pointer;
    user-select: none;
}
</style>