
<script lang="ts">
import { Component } from 'vue-property-decorator';
import template from "./measuresReport.component.html"
import BaseRecordComponent from "hbt-common/components/common/baseRecord.component.vue"
import FormComponent from "hbt-common/components/common/form.component.vue"
import TableComponent from "hbt-common/components/common/table.component.vue"
import MeasureReportService from "@/service/measuresReport.service"
import FormOption from "hbt-common/models/formOptions"
import BtnOption from "hbt-common/models/btnOptions"
import DrawComponent from '@/components/draw.component.vue';
import UnitService from '@/service/unit.service';
import AreaService from '@/service/area.service';
import ButtonListComponent from "hbt-common/components/common/buttonList.component.vue";
import UnitTreeComponent from '@/components/tree.component.vue';

@Component({
    template,
    components: {
        FormComponent,
        TableComponent,
        DrawComponent,
        ButtonListComponent,
        UnitTreeComponent,
    },
})

export default class MeasuresReportManagerComponent extends BaseRecordComponent<any> {
    public tableService = new MeasureReportService();
    public unitService = new UnitService();
    public areaService = new AreaService();

    public params = {} as any;

    public treeData = [];

    public areaList = {} as any;
    public unitList = {} as any;

    //管控措施
    public measuresSelectData = {} as any;

    //排查任务
    public tasksSelectData = {} as any;

    public proTableColumn = [] as any;

    public showProtable = false;

    public currentProTableData = { datas: [] } as any;

    public filterStatusData: any;

    public subActions = [{
        name: "取消",
        value: "cancel"
    }];

    public filters = [{
        text: "未发布",
        value: 0
    }, {
        text: "已发布",
        value: 1
    }]

    public formActions = [{
        name: "查询",
        value: "search",
        icon: "el-icon-search",
        type: "primary"
    }, {
        name: "清空",
        icon: "el-icon-tickets",
        value: "reset"
    }];
    public tableActions = [{
        name: "批量删除",
        value: "delete",
        plain: true,
        icon: "el-icon-delete",
        type: "danger"
    }, {
        name: "发布/终止",
        value: "doUpdate",
        type: "primary"
    }];
    public footerActions = [{
        name: "选择全部",
        value: "selectAll",
        type: "primary"
    }, {
        name: "反向选择",
        value: "reverse"
    }];
    public formOptions: FormOption<BtnOption>[] = [{
        name: "管控对象",
        key: "controlName",
        type: "text",
    }, {
        name: "分析对象",
        key: "analName",
        type: "text",
    }];

    public showUpdate = false;
    public updateParams = {} as any;
    public selectData = [] as any;

    created() {
        this.$store.state.prevention_measures_sort.map((item) => {
            this.measuresSelectData[item.value] = this.treeSelectData(item.children)
        })
        this.$store.state.prevention_task_type.map((item) => {
            this.tasksSelectData[item.value] = this.treeSelectData(item.children)
        })
    }
    // 树点击
    public handleNodeClick(data) {
        this.params.unitId = "";
        this.params.areaId = "";
        if (data.areaId) {
            this.params.unitId = data.id
        } else {
            this.params.areaId = data.id
        }
        this.getTableData()
    }

    public buildTable() {
        this.tableColumn.push({
            name: '状态', key: "status", width: "80px", render: (data) => {
                if (data.status == 0) {
                    return "<span class='noDraw'>未发布</span>"
                } else if (data.status == 1) {
                    return "<span class='color_1' >已发布</span>"
                }
            },
            filters: this.filters.map(item => {
                return {
                    text: item.text,
                    value: item.value,
                }
            }), filterMethod: (data, row) => {
                this.filterStatusData = data
                return row.status === data;
            }
        });
        this.tableColumn.push({
            name: '区域名称', key: "areaName", width: "130px",
        });
        this.tableColumn.push({
            name: '单元名称', key: "unitName", width: "130px",
        });
        this.tableColumn.push({ name: '管控对象', key: "analControlName", width: "200px" });
        this.tableColumn.push({ name: '分析对象', key: "itemName", width: "100px" });
        this.tableColumn.push({ name: '风险源', key: "itemRiskSource", width: "120px" });
        this.tableColumn.push({
            name: '事故后果', key: "itemSeriousResult", width: "100px",
            render: (data) => {
                if (data.itemSeriousResult) {
                    return data.itemSeriousResult.split(";").map(item => this.$store.getters.prevention_serious_result_map[item]).join(";")
                }
            }
        });
        this.tableColumn.push({
            name: '风险等级', key: "itemRiskLevel", width: "100px", render: (data) => {
                return "<span class='color_" + data.itemRiskLevel + "'>" + (data.itemRiskLevel ? (this.$store.getters.prevention_risk_level_map[data.itemRiskLevel]) : '') + "</span>"
            },
            filters: this.$store.state.prevention_risk_level.map(item => {
                return {
                    text: item.name,
                    value: item.value,
                }
            }), filterMethod: (data, row) => {
                return row.riskLevel === data;
            }
        });
        this.tableColumn.push({
            name: '管控措施分类', key: "measureFirstType", width: "120px", render: (data) => {
                if (data.measureFirstType) {
                    return this.$store.getters.prevention_measures_sort_map[data.measureFirstType]
                }
            }
        });
        this.tableColumn.push({
            name: '管控措施分类', width: "120px", key: "measureSecondType",
            render: (data) => {
                if (data.measureFirstType) {
                    const secondTypeItem = this.measuresSelectData[data.measureFirstType]
                    return this.selectName(secondTypeItem, data.measureSecondType)
                }
            }
        });
        this.tableColumn.push({ name: '管控措施分类', width: "120px", key: "measureThirdType" });
        this.tableColumn.push({ name: '管控措施', key: "measureDescription", width: "250px" });

        this.tableColumn.push({
            name: '是否包保责任人任务', width: "150px", key: "insuranceDutyFlag", render: (data) => {
                if (data.taskItem) {
                    return '是'
                } else {
                    return '否'
                }
            }
        });
        this.tableColumn.push({
            name: '任务类型', key: "taskType", width: "130px", render: (data) => {
                if (data.taskType || data.taskType === 0) {
                    return this.$store.getters.prevention_task_type_map[data.taskType]
                }
            },
            filters: this.$store.state.prevention_task_type.map(item => {
                return {
                    text: item.name,
                    value: item.value,
                }
            }), filterMethod: (data, row) => {
                return row.taskType === data;
            }
        });
        this.tableColumn.push({
            name: '包保任务对应项', key: "taskItem", width: "250px", render: (data) => {
                if (data.taskItem) {
                    if (!this.tasksSelectData[data.taskType] || this.tasksSelectData[data.taskType].length === 0) {
                        this.$store.state.prevention_task_type.map((item) => {
                            this.tasksSelectData[item.value] = this.treeSelectData(item.children)
                        })
                    }
                    const taskItemName = {} as any;
                    this.tasksSelectData[data.taskType].map((item) => {
                        taskItemName[item.id] = item.label
                    })
                    return taskItemName[data.taskItem]
                }
            }
        });
        this.tableColumn.push({ name: '隐患排查任务', key: "taskName", width: "250px" });
        this.tableColumn.push({ name: '隐患责任人', key: "taskChargeUserName", width: "100px" });
        this.tableColumn.push({ name: '岗位', key: "taskExecutePostName" });
        this.tableColumn.push({ name: '排查周期', key: "taskReviewCycleValue" });
        this.tableColumn.push({
            name: '单位', key: "taskReviewCycleUnit", render: (data) => {
                return this.$store.getters.prevention_cycle_unit_map[data.taskReviewCycleUnit]
            }
        });

        //查看执行记录
        this.proTableColumn.push({ name: '序号', key: "index", width: "80px" });
        this.proTableColumn.push({ name: '排查时间', key: "checkTime", width: "150px" });
        this.proTableColumn.push({
            name: '排查人', key: "checkUserId", render: (data) => {
                if (data.checkUserId) {
                    return this.$store.getters.user_map[data.checkUserId]
                }
            }
        });
        this.proTableColumn.push({ name: '排查内容', key: "taskName" });
        this.proTableColumn.push({
            name: '排查结果', key: "result", render: (data) => {
                if (data.status == 0) {
                    return "<span class='noDraw'>异常</span>"
                } else if (data.status == 1) {
                    return "<span>正常</span>"
                }
            }
        });
        this.proTableColumn.push({
            name: '是否为隐患', key: "dangerFlag", render: (data) => {
                if (data.status == 1) {
                    return "<span class='noDraw'>是</span>"
                } else if (data.status == 0) {
                    return "<span>否</span>"
                }
            }
        });
        this.proTableColumn.push({ name: '隐患描述', key: "description" });
        this.proTableColumn.push({ name: '隐患归属人', key: "ownerName" });
    }

    // 加载区域列表
    public loadAreaData() {
        this.areaService.selectByPage({ pageSize: 1000 }).then((res: any) => {
            res.data.datas.map(item => {
                this.areaList[item.id] = item.name
            })
        })
    }
    // 加载单元列表
    public loadUnitData() {
        this.unitService.selectByPage({ pageSize: 1000 }, false).then((res: any) => {
            res.data.datas.map(item => {
                this.unitList[item.id] = item.name
            })
        })
    }

    public callback(data) {
        // 查询
        if (data.value === "search") {
            this.getTableData()
            // 重置
        } else if (data.value === "reset") {
            this.reset()
            // 反选
        } else if (data.value === "reverse") {
            this.toggleAll()
            // 全选
        } else if (data.value === "selectAll") {
            this.selectAll()
        } else if (data.value === "delete") {
            this.deleteData(this.selectData.map((item: any) => item.id))
        } else if (data.value === "doUpdate") {
            //发布--终止
            this.doUpdate()
        }
    }
    //发布--终止
    public doUpdate() {
        if (this.selectData && this.selectData.length > 0) {
            this.filterStatusData = this.selectData[0]['status']
            const tmpSelectData = this.selectData.filter((item) => item.status !== this.filterStatusData)
            if (tmpSelectData.length === 0) {
                const statusName = this.filterStatusData === 1 ? '终止' : '发布'
                this.$confirm('确认' + statusName + '所选数据', '确认数据', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const updateParams = JSON.parse(JSON.stringify(this.selectData))
                    updateParams.map((item) => {
                        item.status = this.filterStatusData === 0 ? 1 : 0
                        return item
                    })
                    this.tableService.addOrUpdate(updateParams).then((res) => {
                        this.$message.success(statusName + "成功！");
                        this.getTableData();
                    })
                }).catch(() => {
                    //
                });
            }
        }

    }
    // 重置数据
    public reset() {
        this.params = {
            pageNum: 1,
            pageSize: 20,
        } as any;
    }
    // 删除数据



    public showUpdateModel(id) {
        this.showUpdate = true
    }

    public handleClose() {
        this.showUpdate = false;
    }


    public toggleAll() {
        (this.$refs.multipleTable as any).toggleAllSelection();
    }
    public selectAll() {
        if (!this.selectData.length) {
            this.toggleAll()
        } else {
            this.tableData.datas.forEach((item, index) => {
                const find = this.selectData.find((data: any) => data.userId === item.userId);
                if (!find) {
                    (this.$refs.multipleTable as any).toggleRowSelection(item);
                }
            })
        }
    }

    public handleSelectionChange(data) {
        this.selectData = data;
    }

    public treeSelectData(data) {
        return data.map((item) => {
            return {
                label: item.dictLabel,
                id: isNaN(+item.dictValue) ? item.dictValue : +item.dictValue,
            }
        })

    }

    public selectName(selectGroup, data) {
        if (selectGroup && selectGroup.length > 0) {
            const map = {};
            selectGroup.forEach((item: any) => {
                if (item.value) {
                    map[item.value] = item.name
                } else if (item.id) {
                    map[item.id] = item.label
                }

            })
            return map[data]
        }
    }

    //查看执行记录
    public showSubModal(id) {
        this.showProtable = true
        this.currentProTableData.datas = [] as any;
        this.tableService.selectById({ controlId: id }).then((res: any) => {
            this.currentProTableData.datas = res.data.datas
            this.currentProTableData.datas.forEach((item, index) => {
                item.index = index + 1
                return item
            })
        })
    }

    //执行记录--关闭
    public subCallback() {
        this.showProtable = false;
        this.currentProTableData.datas = [] as any;
    }

}
</script>
<style lang="scss" scoped src="../../common.component.scss"></style>
