<!--
 * @Descripttion:待数据输入列表-重新分配
 * @version: 1.0
 * @Author: Liu_jiaYin
-->
<!-- 样品接收确认 -->
<template>
    <div class="sample-scan">
        <el-dialog
            width="20%"
            top="25vh"
            :modal-append-to-body="false"
            title="检测员重新分配"
            :before-close="handleClose"
            :visible.sync="visible"
            @close="closeDialog"
        >
            <!--自定义对话框-->
            <ibps-custom-dialog
                v-model="jianCeYuan"
                size="mini"
                :disabled="false"
                template-key="xzjcy"
                style="width: 150px;margin:auto;"
                :multiple="false"
                type="dialog"
            />
            
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button type="primary" @click="submitData('确认')">确认</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import IbpsLinkData from '@/business/platform/data/templaterender/link-data'
import IbpsCustomDialog from '@/business/platform/data/templaterender/custom-dialog'
import { formatDate } from '@/utils/date'
import ActionUtils from '@/utils/action'

export default {
    components: {
        IbpsLinkData,
        IbpsCustomDialog

    },
    props: {
        scanVisible: Boolean,
        obj: {
            // eslint-disable-next-line vue/require-valid-default-prop
            default: [],
            type: Array
        }},
    data () {
        return {
            visible: false,
            jianCeYuan: ''
        }
    },
    watch: {
        scanVisible: {
            handler (val, oldName) {
                if (val) {
                    this.visible = true
                }
            },
            deep: true,
            immediate: true
        }
    },
    created () {
    },
    methods: {

        submitData (v) {
            this.handleUp()
        },
        handleClose () {
            this.$confirm('确认要取消重新分配吗').then((t) => {
                this.closeDialog()
            }).catch((e) => {
            })
        },
        closeDialog () {
            this.visible = false
            this.$emit('scanOff', false)
        },
        handleUp () {
            const { projectIds, template, this_ } = this.obj[0]
            const updLists = []
            for (const i of projectIds) {
                const dataWhere = {}
                const where = {
                    id_: i
                }
                const param = {
                    jian_ce_yuan_: this.jianCeYuan
                }
                dataWhere['where'] = where
                dataWhere['param'] = param
                updLists.push(dataWhere)
            }
            const param = { tableName: 't_lhjczb', updList: updLists }
            this.reassign(this_, template, projectIds, this.jianCeYuan)
            this_.$curdPost('updatesByWhere', JSON.stringify(param)).then(() => {
                template.$message.success('重新分配成功。')
                template.search()
                this.closeDialog()
            }).catch(() => {
                template.$message.error('分配失败，请重新再试。')
                this.closeDialog()
            })
        },
        getTaskData (this_, businessKey) {
            const order = businessKey.replace(/,/g, "','")
            const sql = `select a.businesskey_ as businessKey, b.task_id_ as taskId,b.proc_inst_id_ as procInstId, b.node_id_ as nodeId from ibps_bpm_bus_rel a left join ibps_bpm_tasks b on a.proc_inst_id_ = b.proc_inst_id_ where find_in_set(a.businesskey_, '${businessKey}') order by field(a.businesskey_, '${order}')`
            return new Promise((resolve, reject) => {
                this_.$curdPost('sql', sql).then(res => {
                    const { data = [] } = res.variables || {}
                    const taskIds = data.filter(i => i && i.taskId)
                    resolve(taskIds)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        async reassign (this_, template, idList, targetId) {
            const { name, userList } = template.$store.getters
            const ids = idList.join(',')
            const taskIds = await this.getTaskData(this_, ids)
            const currentDate = new Date()
            // updateAssignParams = []
            // updateProjectParams = []
            // 转派人，写死金通
            // const targetId = '702117247933480960'
            if (taskIds.length) {
                for (const i of taskIds) {
                    const executorName = userList.find(item => item.userId === targetId)?.userName
                    this_.$request({
                        url: '/business/v3/bpm/task/change/save',
                        method: 'post',
                        data: {
                            pk: '',
                            name: null,
                            ip: null,
                            createBy: null,
                            createTime: formatDate(currentDate, 'yyyy-MM-dd HH:mm:ss'),
                            updateBy: null,
                            updateTime: null,
                            tenantId: null,
                            dataStatus: null,
                            deleted: null,
                            dbType: null,
                            dsAlias: null,
                            id: null,
                            taskId: i.taskId,
                            taskSubject: `用户：${name}于${formatDate(currentDate, 'yyyy-MM-dd')}重新分配检测员为：${executorName}`,
                            taskName: '发起人',
                            procInstId: i.procInstId,
                            nodeId: i.nodeId,
                            changeType: 'shift',
                            status: 'running',
                            ownerId: '1',
                            executorId: null,
                            comment: `用户：${name}于${formatDate(currentDate, 'yyyy-MM-dd')}重新分配检测员为：${executorName}`,
                            completeTime: null,
                            cancelTime: null,
                            delBeforeSave: true,
                            bpmTaskChangeAssignPoList: [
                                {
                                    'type': 'employee',
                                    'executor': targetId,
                                    'executorName': executorName
                                }
                            ],
                            ownerName: '管理员',
                            executorName: null,
                            icon: null,
                            iconBgColor: null
                        }
                    })
                }
            }

            // taskIds.forEach(item => {
            //     updateAssignParams.push({
            //         where: {
            //             task_id_: item.taskId
            //         },
            //         param: {
            //             executor_: targetId
            //         }
            //     })
            //     updateProjectParams.push({
            //         where: {
            //             id_: item.businessKey
            //         },
            //         param: {
            //             jian_ce_yuan_: targetId
            //         }
            //     })
            // })
            // const updateAssign = {
            //     tableName: 'ibps_bpm_task_assign',
            //     updList: updateAssignParams
            // }
            // const updateProject = {
            //     tableName: 't_lhjczb',
            //     updList: updateProjectParams
            // }
            // // 更新assign表执行人和检测表检测员，刷新列表数据
            // this.$curdPost('updatesByWhere', updateAssign).then(() => {
            //     this.$curdPost('updatesByWhere', updateProject).then(() => {
            //         this.$template.handleAction('search', {}, [], [], 0, {button_type: 'search'})
            //     })
            // })
        }

    }
}
</script>

<style lang="less" scoped>
/deep/ .el-form-item__label,
/deep/ .el-input__inner {
    color: none;
}
.dialog-body{

}
.sample-scan .popContainer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999999;
    background: rgba(0, 0, 0, 0.7);
}

// .dynamic-form-table__label:before {
//   content: '*';
//   color: #F56C6C;
// }
.dynamic-form-table__label {
    color: #F56C6C;
    font-size: 18px;
    line-height: 40px;
    font-weight: bold;
}

/**必须设置.el-form-item__label为none */
/deep/.zzj .el-form-item__content .el-input__inner {
    color: #e60c14;
}

/deep/.el-form-item__content {
    display: inline-block;
}

/deep/.el-form-item__label {
    padding: 0;
    margin-right: 5px;
    width: 120px;
    // width: 101px;
    // margin-left:25px;
}

/deep/.el-input__inner {
    border-top: none !important;
    border-left: none !important;
    border-right: none !important;
    border-radius: 0;
}

/deep/.jiaji {
    // color: #F56C6C;
    // font-size: 18px;
    // line-height: 40px;
    // font-weight: bold;
}

/deep/.el-dialog__title {
    line-height: 24px;
    color: #e60c14;
    font-weight: 600;
}
</style>
