<template>
    <div>
        <el-dialog title="部门长确认调解员" :visible="departmentVisable" v-if="departmentVisable" @close="cancel" center
            :distroy-on-close="true">
            <div style="margin-bottom: 20px;">
                <el-radio-group v-model="confirmFlag">
                    <el-radio :label="1">同意</el-radio>
                    <el-radio :label="2">拒绝</el-radio>
                </el-radio-group>
            </div>
            <div>
                <div>
                    <div style="margin-bottom: 20px;">调解员</div>
                </div>
                <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
                    @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" v-if="!confirmShow">
                    </el-table-column>
                    <el-table-column prop="mediatorName" label="调解员名称">
                    </el-table-column>
                    <el-table-column prop="specialty" label="专业">
                    </el-table-column>
                    <el-table-column prop="todoAmount" label="待办数量">
                    </el-table-column>
                    <el-table-column prop="completeAmount" label="已办数量">
                    </el-table-column>
                </el-table>
                <!-- <div>
                    <div style="margin-top: 20px;margin-bottom: 20px;">时间</div>
                    <el-form label-position="right" label-width="80px" :model="formLabelAlign" :disabled="confirmShow">
                        <el-form-item label="时间">
                            <el-date-picker v-model="formLabelAlign.time[0]" type="datetime" placeholder="选择日期时间">
                            </el-date-picker>
                        </el-form-item> -->
                        <!-- 后期需要三个时间 -->
                        <!-- <el-form-item label="时间2">
                            <el-date-picker v-model="formLabelAlign.time[1]" type="datetime" placeholder="选择日期时间">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="时间3">
                            <el-date-picker v-model="formLabelAlign.time[2]" type="datetime" placeholder="选择日期时间">
                            </el-date-picker>
                        </el-form-item> -->
                    <!-- </el-form>
                </div> -->
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel" class="endbutton1"><span>取 消</span></el-button>
                <el-button @click="submitMediator" class="endbutton1"><span>确 认</span></el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { Message } from 'element-ui'
import { listMediator, selectReservation, verifyMediator } from '@/api/caseManagement/caseManagement.js'
import moment from "moment";
export default {
    props: ["departmentVisable", "departmentData", "queryParams"],
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            formLabelAlign: {
                time: []
            },
            formTimeArr: [],
            mediatorArr: [],
            confirmFlag: 1,
            confirmShow: true,
        };
    },
    watch: {
        departmentVisable(val) {
            if (val) {
                this.confirmFlag = 1;
                this.mediatorArr = [];
                this.formTimeArr = [];
                this.selectReservationFn({ id: this.departmentData.id })
            }
        },
        confirmFlag(val) {
            if (val == 1) {
                this.confirmShow = true;
                console.log(this.departmentData.id);
                this.selectReservationFn({ id: this.departmentData.id })
            } else {
                this.confirmShow = false;
                listMediator().then(res => {
                    this.tableData = res.data;
                })
            }
        }
    },
    methods: {
        cancel() {
            this.$emit("cancelDepartment");
        },
        selectReservationFn(data) {
            selectReservation(data).then(res => {
                this.tableData = res.data.mediatorList;
                this.formLabelAlign.time[0] = res.data.herDates[0];
            })
        },
        /**核实调解员 */
        verifyMediatorFn(data) {
            verifyMediator(data).then(res => {
                this.$modal.msgSuccess("成功");
                this.$emit("cancelDepartment");
                this.$emit('getList', this.queryParams);
            })
        },
        /**提交选择结果*/
        async submitMediator() {
            if (this.confirmFlag == 1) {
                let userArr = [];
                this.tableData.forEach(item => {
                    userArr.push({
                        userId: item.mediatorId,
                        userName: item.mediatorName
                    })
                })
                this.verifyMediatorFn({
                    id: this.departmentData.id,
                    caseFlowId: this.departmentData.caseFlowId,
                    userList: userArr,
                    // herDates: this.formLabelAlign.time
                })
            } else {
                if (this.multipleSelection.length > 1) {
                    Message.error('最多选择一名调解员');
                    return
                } else if (this.multipleSelection.length < 1) {
                    Message.error('至少选择一名调解员');
                    return
                }
                if (this.formLabelAlign.time.length < 1) {
                    Message.error('至少选择一个时间');
                    return
                }
                this.formLabelAlign.time.forEach(item => {
                    item = moment(
                        item
                    ).format("YYYY-MM-DD HH:mm:ss");
                    this.formTimeArr.push(item)
                })
                this.multipleSelection.forEach(item => {
                    this.mediatorArr.push({
                        userId: item.mediatorId,
                        userName: item.mediatorName
                    })
                })
                this.verifyMediatorFn({
                    id: this.departmentData.id,
                    caseFlowId: this.departmentData.caseFlowId,
                    userList: this.mediatorArr,
                    // herDates: this.formLabelAlign.time
                })
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        }
    },
};
</script>
  
<style lang="scss" scoped>
.steps {
    display: flex;
    flex-wrap: wrap;
}

::v-deep .el-step {
    // width: 150px;
    flex-basis: 25% !important;
    margin-right: 20px;
    margin-bottom: 20px;
}

::v-deep .el-dialog__body {
    height: 500px !important;
    overflow: auto !important;
}

::v-deep .el-dialog {
    width: 800px;
    background: #ffffff;
    border-radius: 20px;
}

.timeTitle {
    width: 1000%;
    text-align: center;
}
</style>