<template>
    <div v-if="visible">
        <div class="sub-title">基本信息</div>
        <FormComponent :options="subBasicOptions" labelWidth="110px" labelAlign="right"
            :data.sync="analysisUpdateParams" @actionCallback="callback" :isReadonly="analyIsReadonly"
            @change="handleChange" ref="basicForm">
        </FormComponent>
        <div class="sub-title">风险评估</div>
        <FormComponent :options="subRiskOptions" labelWidth="110px" labelAlign="right" :data.sync="analysisUpdateParams"
            @actionCallback="callback" @change="handleRiskChange" :isReadonly="analyIsReadonly" ref="riskForm">
            <span class="measure-tips">*注：当评估结果为最高级时，必须由公司级管控</span>
        </FormComponent>

        <div class="sub-title">管控措施</div>
        <FormComponent labelWidth="110px" labelAlign="right" :data.sync="analysisUpdateParams"
            @actionCallback="measureCallback" :isReadonly="analyIsReadonly">
            <TableComponent :tableData="analysisUpdateParams.measures" :tableColumn="triTableColumn"
                @actionCallback="measureCallback($event)" :actions="analyIsReadonly ? [] : triTableActions"
                actionPosition="flex-start" :showFooter="false" style="margin-bottom: 20px;">
                <el-table ref="multipleTable" :data="analysisUpdateParams.measures" tooltip-effect="dark"
                    max-height="500" border row-key="checked" @selection-change="handleSelectionChange"
                    style="width: 100%">
                    <el-table-column v-if="!analyIsReadonly" type="selection" fixed label="全选" width="40">
                    </el-table-column>
                    <template v-for="item in triTableColumn">
                        <el-table-column v-if="item.render" :show-overflow-tooltip="item.showTip" :label="item.name"
                            :width="item.width" :key="item.key">
                            <div slot-scope="scope" v-html="item.render(scope.row)"></div>
                        </el-table-column>
                        <el-table-column v-else :prop="item.key" :show-overflow-tooltip="item.showTip"
                            :label="item.name" :width="item.width" :key="item.key">
                        </el-table-column>
                    </template>
                    <el-table-column label="操作" fixed="right" width="150">
                        <template slot-scope="scope">
                            <el-button type="text" @click="showMeasureModel(scope.row, true)">查看</el-button>
                            <el-button v-if="!analyIsReadonly" type="text"
                                @click="showMeasureModel(scope.row)">编辑</el-button>
                            <el-button v-if="!analyIsReadonly" type="text"
                                @click="deleteMeasure(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </TableComponent>
        </FormComponent>
        <div class="sub-title">残余风险评估</div>
        <FormComponent :options="subRemnantsOptions" labelWidth="110px" labelAlign="right"
            :data.sync="analysisUpdateParams" @actionCallback="analyCallback"
            :actions="analyIsReadonly ? [] : measureActions" :full-btn="true" btnPosition="center"
            @change="handleSubRiskChange" :isReadonly="analyIsReadonly" ref="remantRiskForm">
        </FormComponent>

        <!-- 风险评估方法 -->
        <el-dialog :close-on-click-modal="false" :show-close="false" :visible.sync="showRiskUpdate" width="952px"
            :before-close="handleClose">
            <FormComponent :options="riskType === 'ls' ? riskLSUpdateOptions : riskLECUpdateOptions" labelWidth="110px"
                labelAlign="right" :data.sync="methodUpdateParams" @actionCallback="methodCallback"
                :actions="triActions" :full-btn="true" btnPosition="center">
            </FormComponent>
            <div v-if="riskType === 'ls'" style="text-align: center;">
                <img style="width:70%" src="../assets/images/3.png" alt="">
                <img style="width:60%" src="../assets/images/2.png" alt="">
            </div>
            <div v-if="riskType === 'lec'" style="text-align: center;">
                <img style="width:70%" src="../assets/images/6.png" alt="">
            </div>
        </el-dialog>
        <!-- 风险评估取值 -->
        <div v-if="showRiskValueUpdate">
            <el-dialog :close-on-click-modal="false" :title="riskTitle" :visible.sync="showRiskValueUpdate"
                width="952px" :before-close="handleClose" :show-close="false">
                <TableComponent :tableData="riskTableData" :tableColumn="riskTableColumn"
                    @tabCallback="popCallback($event)" actionPosition="flex-start" :showFooter="false"
                    style="margin-bottom: 20px;" :data.sync="methodUpdateParams">
                    <el-table ref="singleTable" :data="riskTableData" tooltip-effect="dark" border row-key="checked"
                        @current-change="handleCurrentChange" style="width: 100%" highlight-current-row>
                        <template v-for="item in riskTableColumn">
                            <el-table-column v-if="item.render" :label="item.name" :width="item.width" :key="item.key">
                                <div slot-scope="scope" style="pointer-events: none;" v-html="item.render(scope.row)">
                                </div>
                            </el-table-column>
                            <el-table-column v-else :prop="item.key" :label="item.name" :width="item.width"
                                :key="item.key">
                            </el-table-column>
                        </template>
                    </el-table>
                </TableComponent>
                <ButtonListComponent :actions="triActions" @callback="popCallback" btn-position="center"
                    :full-btn="true">
                </ButtonListComponent>
            </el-dialog>
        </div>

        <el-dialog :close-on-click-modal="false" :title="'管控措施制定'" :visible.sync="subUpdate" width="952px"
            :before-close="handleMeasureClose" destroy-on-close>
            <MeasureComponent @actionCallback="dataCallback" :data.sync="measuresData" :show.sync="subUpdate"
                :tabledata.sync="analysisUpdateParams" :isReadonly="measureIsReadonly" :outermostData="updateParams">
            </MeasureComponent>
        </el-dialog>

    </div>
</template>
<script lang="ts">
import FormComponent from 'hbt-common/components/common/form.component.vue';
import { Component, Emit, Prop, PropSync, Vue, Watch } from 'vue-property-decorator';
import BaseRecordComponent from "hbt-common/components/common/baseRecord.component.vue"
import TableComponent from "hbt-common/components/common/table.component.vue"
import FormOption from "hbt-common/models/formOptions"
import BtnOption from "hbt-common/models/btnOptions"
import DrawComponent from '@/components/draw.component.vue';
import WorkService from '@/service/work.service';
import UnitService from '@/service/unit.service';
import AreaService from '@/service/area.service';
import SystemService from "hbt-common/service/system.service"
import JobHazardService from '@/service/jobHazard.service';
import ButtonListComponent from "hbt-common/components/common/buttonList.component.vue";
import { L_COLUMN, L_VALUE, S_COLUMN, S_VALUE } from '@/mock/lsRisk';
import { L_LECCOLUMN, L_LECVALUE, E_COLUMN, E_VALUE, C_COLUMN, C_VALUE } from '@/mock/lecRisk';
import MeasureComponent from '@/components/measure.component.vue'
import moment from 'moment';
import { Message } from 'element-ui';

@Component({
    components: {
        FormComponent,
        TableComponent,
        DrawComponent,
        ButtonListComponent,
        MeasureComponent,
    }
})
export default class AnalEvaluationComponent extends Vue {
    public systemService = new SystemService();
    // public analysisUpdateParams = {
    //     riskChoose: [],
    //     remainChoose: [],
    //     remainLevel: null,
    //     riskLevel: null,
    //     datas: []
    // } as any;



    public measuresData = {} as any;

    public dictData = {
        riskControlLevel: [],
        identifier: [],
        seriousResult: []
    } as any;
    public triTableColumn = [] as any;
    public showSubUpdate = false;
    public analyIsReadonly = false;

    public measureIsReadonly = false;

    public showRiskUpdate = false;//风险评估方法
    public selectData = [];

    public riskType = 'ls';

    public showRiskValueUpdate = false;

    public showRiskValueModal: any = 'lslvalue';

    public riskTitle: any = '选择L值';

    public riskTableData = [] as any;

    public riskTableColumn = [] as any;

    public currentRow = {} as any;

    public selectedArrData = [];


    //评估方法
    public methodUpdateParams = {
        lslvalue: null,
        lssvalue: null,
        lsrvalue: null,
        lsgrade: null,
        leclvalue: null,
        lecevalue: null,
        leccvalue: null,
        lecdvalue: null,
        lecgrade: null,

    } as any;

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

    @Prop()
    isReadonly: boolean;

    @Prop()
    type: any;

    @Watch("isReadonly", { immediate: true, deep: true })
    onCountValueChange() {
        this.analyIsReadonly = this.isReadonly
    }

    @PropSync("data", {
        required: true,
    })
    analysisUpdateParams!: any;
    @Watch("analysisUpdateParams", { immediate: true })
    onChangeData(newValue, oldVale) {
        if (!oldVale || (newValue.id !== oldVale.id)) {
            this.buildInitData()
        }
    }

    @PropSync("show", {
        required: true,
        default: true
    })
    visible: boolean;
    @PropSync("tabledata", {
        required: true,
    })
    updateParams: any;

    @Prop()
    areaList: any;

    @Prop()
    //评估方法
    checkRiskType: any;

    //基本信息
    public subBasicOptions: FormOption<BtnOption>[] = [];
    public subBasicForm() {
        if (this.type === "work") {
            this.subBasicOptions = [{
                name: "步骤名称",
                key: "name",
                type: "text",
                require: true,
                width: "100%",
                disable: true,
                showError: false,
            }, {
                name: "作业地点",
                key: "location",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            },
            {
                name: "责任人",
                key: "chargeUserName",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            }, {
                name: "作业人",
                key: "workerName",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            },
            {
                name: "作业步骤描述",
                key: "description",
                type: "textarea",
                require: true,
                width: "100%",
                disable: true,
                showError: false,
            },
            {
                name: "风险源",
                key: "riskSource",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                showError: false,
            },
            {
                name: "最严重后果",
                key: "seriousResult",
                format: "seriousResultName",
                type: "select",
                require: true,
                multiple: true,
                width: "calc(50% - 20px)",
                datas: this.$store.state.prevention.prevention_serious_result,
                filterable: true,
                showError: false,
                clearable: true,
            },
            {
                name: "危害分析",
                key: "hazardAnalysis",
                type: "textarea",
                require: true,
                width: "calc(100% - 20px)",
                showError: false,
            },
            {
                name: "安全警示标识",
                key: "safetySign",
                format: "safetySignName",
                type: "select",
                require: true,
                multiple: true,
                width: "100%",
                datas: this.$store.state.prevention.prevention_security_identifier,
                filterable: true,
                showError: false,
                clearable: true,
            },
            {
                name: "设置复评时间",
                key: "reviewStartTime",
                type: "date",
                subType: "date",
                width: "calc(50% - 20px)",
                require: true,
                showError: false,
                format: "yyyy-MM-dd",
                valueFormat: "yyyy-MM-dd",
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < moment(new Date()).add(1, 'month').subtract(1, 'day').toDate().getTime();
                    }
                }
            },
            ];
        } else {
            this.subBasicOptions = [{
                name: "设备名称",
                key: "deviceName",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            }, {
                name: "设备类型",
                key: "deviceType",
                format: "typeName",
                type: "select",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                datas: this.$store.state.prevention.prevention_device_type,
                showError: false,
            },
            {
                name: "责任人",
                key: "chargeUserName",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            },
            {
                name: "检查项目",
                key: "itemName",
                type: "textarea",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            },
            {
                name: "检查标准",
                key: "itemStandard",
                type: "textarea",
                require: true,
                width: "calc(50% - 20px)",
                disable: true,
                showError: false,
            },
            {
                name: "风险源",
                key: "riskSource",
                type: "text",
                require: true,
                width: "calc(50% - 20px)",
                showError: false,
            },
            {
                name: "最严重后果",
                key: "seriousResult",
                format: "seriousResultName",
                type: "select",
                require: true,
                multiple: true,
                width: "calc(50% - 20px)",
                datas: this.$store.state.prevention.prevention_serious_result,
                filterable: true,
                showError: false,
                clearable: true,
            },
            {
                name: "危害分析",
                key: "hazardAnalysis",
                type: "text",
                require: true,
                width: "calc(100% - 20px)",
                showError: false,
            },
            {
                name: "安全警示标识",
                key: "safetySign",
                format: "safetySignName",
                type: "select",
                require: true,
                multiple: true,
                width: "100%",
                datas: this.$store.state.prevention.prevention_security_identifier,
                filterable: true,
                showError: false,
                clearable: true,
            },
            {
                name: "设置复评时间",
                key: "reviewStartTime",
                type: "date",
                subType: "date",
                width: "calc(50% - 20px)",
                require: true,
                showError: false,
                format: "yyyy-MM-dd",
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < moment(new Date()).add(1, 'month').subtract(1, 'day').toDate().getTime();
                    }
                }
            },
            ];
        }

    }
    //风险评估
    public subRiskOptions: FormOption<BtnOption>[] = [];
    public riskUpdateForm() {
        this.subRiskOptions = [{
            name: "选择评估方法",
            key: "riskChoose",
            type: "checkbox",
            width: "calc(50% - 20px)",
            require: true,
            showError: false,
            datas: [{
                name: "LS法",
                value: 'LS',
                disable: false,
            }, {
                name: "LEC法",
                value: 'LEC',
                disable: false,
            }]
        }, {
            name: "风险等级",
            key: "riskLevel",
            format: "riskLevelName",
            type: "select",
            require: true,
            width: "calc(50% - 20px)",
            disable: true,
            datas: this.$store.state.prevention.prevention_risk_level,
            showError: false,
        }, {
            name: "管控层级",
            key: "riskControlLevel",
            format: "riskControlLevelName",
            type: "select",
            require: true,
            width: "calc(50% - 20px)",
            datas: this.$store.state.prevention.prevention_control_level,
            showError: false,
            disable: this.analysisUpdateParams.riskLevel === 4
        }];
    }
    public triTableActions = [{
        name: "添加",
        value: "triSubAdd",
        icon: "el-icon-plus",
        type: "primary"
    }];
    public triActions = [{
        name: "取消",
        value: "cancel"
    }, {
        name: "保存",
        value: "save",
        type: "primary"
    }];
    public measureActions = [
        {
            name: "取消",
            value: "cancel"
        }, {
            name: "保存",
            value: "store",
            type: "primary"
        }
    ];
    //残余风险评估
    public subRemnantsOptions: FormOption<BtnOption>[] = [];
    public remainUpdateForm() {
        this.subRemnantsOptions = [{
            name: "选择评估方法",
            key: "remainChoose",
            type: "checkbox",
            width: "calc(50% - 20px)",
            require: true,
            showError: false,
            datas: [{
                name: "LS法",
                value: 'LS'
            }, {
                name: "LEC法",
                value: 'LEC'
            }]
        }, {
            name: "风险等级",
            key: "remainLevel",
            format: "remainLevelName",
            type: "select",
            require: true,
            showError: false,
            width: "calc(50% - 20px)",
            disable: true,
            datas: this.$store.state.prevention.prevention_risk_level
        }];
    }
    //LS
    public riskLSUpdateOptions: FormOption<BtnOption>[] = [{
        name: "L取值",
        key: "lslvalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        unit: {
            type: "btn",
            name: "选择",
            value: "lslvalue",
            btnType: "primary",
            width: "100%",
        },
    }, {
        name: "S取值",
        key: "lssvalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        unit: {
            type: "btn",
            name: "选择",
            value: "lssvalue",
            btnType: "primary",
            width: "100%",
        },
    }, {
        name: "R值",
        key: "lsrvalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
    }, {
        name: "风险等级",
        key: "lsgrade",
        type: "select",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        datas: this.$store.state.prevention.prevention_risk_level
    },];
    //LEC
    public riskLECUpdateOptions: FormOption<BtnOption>[] = [{
        name: "L取值",
        key: "leclvalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        unit: {
            type: "btn",
            name: "选择",
            value: "leclvalue",
            btnType: "primary",
            width: "100%",
        },
    }, {
        name: "E取值",
        key: "lecevalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        unit: {
            type: "btn",
            name: "选择",
            value: "lecevalue",
            btnType: "primary",
            width: "100%",
        },
    }, {
        name: "C取值",
        key: "leccvalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        unit: {
            type: "btn",
            name: "选择",
            value: "leccvalue",
            btnType: "primary",
            width: "100%",
        },
    }, {
        name: "D值",
        key: "lecdvalue",
        type: "text",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
    }, {
        name: "风险等级",
        key: "lecgrade",
        type: "select",
        require: true,
        width: "calc(50% - 20px)",
        disable: true,
        showError: false,
        datas: this.$store.state.prevention.prevention_risk_level
    },];

    // created () {
    //     this.buildInitData()
    // }
    /**
     * 初始化数据
     */
    public buildInitData() {
        this.$store.state.prevention.prevention_measures_sort.map((item) => {
            this.measuresSelectData[item.value] = this.treeSelectData(item.children)
        })
        this.subBasicForm()
        this.riskUpdateForm()
        this.remainUpdateForm()
        this.triTableColumn = []
        this.buildTable()
    }

    //评价
    public handleAnalyClose() {
        this.showSubUpdate = false;
        this.analyIsReadonly = true;
    }

    public callback(data) {
        //

    }

    public buildTable() {
        this.triTableColumn.push({ name: '序号', key: "index", width: "80px" });
        this.triTableColumn.push({
            name: '管控方法', key: "controlWayName", render: (data) => {
                if (data.controlWay) {
                    return this.$store.getters.prevention_measure_type_map[data.controlWay]
                }
            }

        });
        this.triTableColumn.push({
            name: '措施类型一', key: "firstTypeName", render: (data) => {
                if (data.firstType) {
                    return this.$store.getters.prevention_measures_sort_map[data.firstType]
                }
            }
        });
        this.triTableColumn.push({
            name: '措施类型二', key: "secondTypeName",
            render: (data) => {
                if (data.firstType) {
                    const secondTypeItem = this.measuresSelectData[data.firstType]
                    return this.selectName(secondTypeItem, data.secondType)
                }
            }
        });
        this.triTableColumn.push({ name: '措施类型三', key: "thirdType" });
        this.triTableColumn.push({ name: '管控措施', key: "description" });
        this.triTableColumn.push({
            name: '排查任务', key: "tasksNum", render: (data) => {
                if (data.tasksNum) {
                    return data.tasksNum
                } else if (data.tasks && data.tasks.length > 0) {
                    return data.tasks.length
                }
            }
        });
    }


    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 measureCallback(data) {
        if (data && data.value === "triSubAdd") {
            this.subUpdate = true;
            this.measureIsReadonly = false;
            this.measuresData = {
                tasks: []
            };
        } else if (data && data.value === 'cancel') {
            this.visible = false;
        } else if (data && data.value.indexOf("save") >= 0) {
            // this.doMeasureSave(data.value !== "save")
        }
        this.measureIsReadonly = false
    }

    public dataCallback(data) {
        //console.log('返回数据', data);

    }


    public handleMeasureClose() {
        this.subUpdate = false;
    }

    //风险评估
    public handleRiskChange(data) {
        const datalen = data.length
        const risklen = this.checkRiskType.risk.length
        if (datalen > risklen) {
            this.showRiskUpdate = true
            let filterdata = data.filter(subItem => !this.checkRiskType.risk.includes(subItem))
            if (filterdata.includes('LS')) {
                this.riskType = 'ls'
                if (this.analysisUpdateParams.riskLsValue) {
                    this.methodUpdateParams = JSON.parse(this.analysisUpdateParams.riskLsValue)
                }

            } else if (filterdata.includes('LEC')) {
                this.riskType = 'lec'
                if (this.analysisUpdateParams.riskLecValue) {
                    this.methodUpdateParams = JSON.parse(this.analysisUpdateParams.riskLecValue)
                }
            }
        } else {
            let filterdata = this.checkRiskType.risk.filter(subItem => !data.includes(subItem))
            if (filterdata.includes('LS')) {
                this.riskType = 'ls'
                if (this.analysisUpdateParams.riskLsValue) {
                    this.analysisUpdateParams.riskLsValue = null
                    this.analysisUpdateParams.riskLsLevel = null
                }
                if (this.analysisUpdateParams.riskLecValue) {
                    this.analysisUpdateParams.riskLevel = JSON.parse(this.analysisUpdateParams.riskLecValue).lecgrade
                }

            } else if (filterdata.includes('LEC')) {
                this.riskType = 'lec'
                if (this.analysisUpdateParams.riskLecValue) {
                    this.analysisUpdateParams.riskLecValue = null
                    this.analysisUpdateParams.riskLecLevel = null
                }

                if (this.analysisUpdateParams.riskLsValue) {
                    this.analysisUpdateParams.riskLevel = JSON.parse(this.analysisUpdateParams.riskLsValue).lsgrade
                }
            }
            this.compareRiskLevel()

        }
        this.checkRiskType.risk = data
        this.checkRiskType.type = 'risk'

    }


    //残余风险评估
    public handleSubRiskChange(data) {
        const datalen = data.length
        const risklen = this.checkRiskType.remain.length
        if (datalen > risklen) {
            this.showRiskUpdate = true
            let filterdata = data.filter(subItem => !this.checkRiskType.remain.includes(subItem))
            if (filterdata.includes('LS')) {
                this.riskType = 'ls'
                if (this.analysisUpdateParams.remainLsValue) {
                    this.methodUpdateParams = JSON.parse(this.analysisUpdateParams.remainLsValue)
                }
            } else if (filterdata.includes('LEC')) {
                this.riskType = 'lec'
                if (this.analysisUpdateParams.remainLecValue) {
                    this.methodUpdateParams = JSON.parse(this.analysisUpdateParams.remainLecValue)
                }
            }
        } else {
            let filterdata = this.checkRiskType.remain.filter(subItem => !data.includes(subItem))
            if (filterdata.includes('LS')) {
                this.riskType = 'ls'
                if (this.analysisUpdateParams.remainLsValue) {
                    this.analysisUpdateParams.remainLsValue = null
                    this.analysisUpdateParams.remainLsLevel = null
                }
                if (this.analysisUpdateParams.remainLecValue) {
                    this.analysisUpdateParams.remainLevel = JSON.parse(this.analysisUpdateParams.remainLecValue).lecgrade
                }

            } else if (filterdata.includes('LEC')) {
                this.riskType = 'lec'
                if (this.analysisUpdateParams.riskLecValue) {
                    this.analysisUpdateParams.riskLecValue = null
                    this.analysisUpdateParams.riskLecLevel = null
                }

                if (this.analysisUpdateParams.remainLsValue) {
                    this.analysisUpdateParams.remainLevel = JSON.parse(this.analysisUpdateParams.remainLsValue).lsgrade
                }
            }
            this.compareRiskLevel()
        }
        this.checkRiskType.remain = data
        this.checkRiskType.type = 'subRisk'
    }

    //评估方法 -- 风险等级确定
    public compareRiskLevel() {
        const tmpUpdateParams = JSON.parse(JSON.stringify(this.analysisUpdateParams))
        const riskChoose = tmpUpdateParams.riskChoose
        const remainChoose = tmpUpdateParams.remainChoose
        if (riskChoose.length === 0) {
            this.analysisUpdateParams.riskLevel = null
        } else {
            if (riskChoose.includes('LS')) {
                if (tmpUpdateParams.riskLsLevel) {
                    this.analysisUpdateParams.riskLevel = tmpUpdateParams.riskLevel > tmpUpdateParams.riskLsLevel ? tmpUpdateParams.riskLevel : tmpUpdateParams.riskLsLevel
                }

            }
            if (riskChoose.includes('LEC')) {
                if (tmpUpdateParams.riskLecLevel) {
                    this.analysisUpdateParams.riskLevel = tmpUpdateParams.riskLevel > tmpUpdateParams.riskLecLevel ? tmpUpdateParams.riskLevel : tmpUpdateParams.riskLecLevel
                }
            }
        }

        if (remainChoose.length === 0) {
            this.analysisUpdateParams.remainLevel = null
        } else {
            if (remainChoose.includes('LS')) {
                if (tmpUpdateParams.remainLsLevel) {
                    this.analysisUpdateParams.remainLevel = tmpUpdateParams.remainLevel > tmpUpdateParams.remainLsLevel ? tmpUpdateParams.remainLevel : tmpUpdateParams.remainLsLevel
                }

            }
            if (remainChoose.includes('LEC')) {
                if (tmpUpdateParams.remainLecLevel) {
                    this.analysisUpdateParams.remainLevel = tmpUpdateParams.remainLevel > tmpUpdateParams.remainLecLevel ? tmpUpdateParams.remainLevel : tmpUpdateParams.remainLecLevel
                }
            }
        }
        if (this.analysisUpdateParams.riskLevel === 4) {
            this.analysisUpdateParams.riskControlLevel = 1
        }
        if (!this.analysisUpdateParams.riskLevel) {
            this.analysisUpdateParams.riskControlLevel = null
        }
        this.riskUpdateForm()
        this.remainUpdateForm()
    }

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

    public showMeasureModel(row, isReadonly) {
        if (isReadonly) {
            this.measureIsReadonly = true
            this.measuresData = row
            this.measuresData.controlWayName = this.$store.getters.prevention_measure_type_map[row.controlWay]
            this.measuresData.firstTypeName = this.$store.getters.prevention_measures_sort_map[row.firstType]
            const secondTypeItem = this.measuresSelectData[row.firstType]
            this.measuresData.secondTypeName = this.selectName(secondTypeItem, row.secondType)
            this.measuresData = Object.assign({ tasks: [] }, row)
            this.measuresData.tasks.forEach((item, index) => {
                item.index = index + 1
                return item
            })
        } else if (!isReadonly) {
            this.measureIsReadonly = false
            this.measuresData = Object.assign({ tasks: [] }, row)
            this.measuresData.tasks.forEach((item, index) => {
                item.index = index + 1
                return item
            })
        }
        this.subUpdate = true
    }
    //删除
    public deleteMeasure(row) {
        this.$confirm('确认删除所选数据', '确认数据', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.analysisUpdateParams.measures.splice(this.analysisUpdateParams.measures.findIndex(item => item.index === row.index), 1)
            if (row.id) {
                if (!this.analysisUpdateParams.deleteIds) {
                    this.analysisUpdateParams.deleteIds = []
                }
                this.analysisUpdateParams.deleteIds.push(row.id)
            }
            this.analysisUpdateParams.measures.forEach((item, i) => {
                item.index = i + 1
            })
        }).catch(() => {
            //
        });
    }

    //评估方法 -- btn
    public methodCallback(data, item) {
        if (data.value === "cancel") {
            this.methodUpdateParams = {
                lslvalue: null,
                lssvalue: null,
                lsgrade: null,
                leclvalue: null,
                lecevalue: null,
                leccvalue: null,
                lecgrade: null,

            } as any;//评估方法
            this.showRiskUpdate = false

            if (this.riskType === 'ls') {
                if (this.checkRiskType.type === 'subRisk') {
                    this.analysisUpdateParams.remainChoose.splice(this.analysisUpdateParams.remainChoose.findIndex(item => item === 'LS'), 1)
                } else {
                    this.analysisUpdateParams.riskChoose.splice(this.analysisUpdateParams.riskChoose.findIndex(item => item === 'LS'), 1)
                }
            } else if (this.riskType === 'lec') {
                if (this.checkRiskType.type === 'subRisk') {
                    this.analysisUpdateParams.remainChoose.splice(this.analysisUpdateParams.remainChoose.findIndex(item => item === 'LEC'), 1)
                } else {
                    this.analysisUpdateParams.riskChoose.splice(this.analysisUpdateParams.riskChoose.findIndex(item => item === 'LEC'), 1)
                }
            }

            this.compareRiskLevel()
        } else if (data.value === 'lslvalue') {
            this.showRiskValueModal = 'lslvalue'
            this.showRiskValueUpdate = true
            this.riskTitle = '选择L值'
            this.riskTableData = L_VALUE
            this.riskTableColumn = L_COLUMN
        } else if (data.value === 'lssvalue') {
            this.showRiskValueModal = 'lssvalue'
            this.showRiskValueUpdate = true
            this.riskTitle = '选择S值'
            this.riskTableData = S_VALUE
            this.riskTableColumn = S_COLUMN
        } else if (data.value === 'leclvalue') {
            this.showRiskValueModal = 'leclvalue'
            this.showRiskValueUpdate = true
            this.riskTitle = '选择L值'
            this.riskTableData = L_LECVALUE
            this.riskTableColumn = L_LECCOLUMN
        } else if (data.value === 'lecevalue') {
            this.showRiskValueModal = 'lecevalue'
            this.showRiskValueUpdate = true
            this.riskTitle = '选择E值'
            this.riskTableData = E_VALUE
            this.riskTableColumn = E_COLUMN
        } else if (data.value === 'leccvalue') {
            this.showRiskValueModal = 'leccvalue'
            this.showRiskValueUpdate = true
            this.riskTitle = '选择C值'
            this.riskTableData = C_VALUE
            this.riskTableColumn = C_COLUMN
        } else if (data.value === 'save') {
            //风险评估
            if (this.checkRiskType.type === 'risk') {
                if (this.showRiskValueModal.indexOf('ls') !== -1) {
                    const riskLsValue = JSON.parse(JSON.stringify(this.methodUpdateParams))
                    this.analysisUpdateParams.riskLsLevel = riskLsValue.lsgrade
                    this.analysisUpdateParams.riskLsValue = JSON.stringify(riskLsValue)

                } else if (this.showRiskValueModal.indexOf('lec') !== -1) {
                    const riskLecValue = JSON.parse(JSON.stringify(this.methodUpdateParams))
                    this.analysisUpdateParams.riskLecLevel = riskLecValue.lecgrade
                    this.analysisUpdateParams.riskLecValue = JSON.stringify(riskLecValue)
                }
            } else if (this.checkRiskType.type === 'subRisk') {//残余风险评估
                if (this.showRiskValueModal.indexOf('ls') !== -1) {
                    const riskLsValue = JSON.parse(JSON.stringify(this.methodUpdateParams))
                    this.analysisUpdateParams.remainLsLevel = riskLsValue.lsgrade
                    this.analysisUpdateParams.remainLsValue = JSON.stringify(riskLsValue)
                } else if (this.showRiskValueModal.indexOf('lec') !== -1) {
                    const riskLecValue = JSON.parse(JSON.stringify(this.methodUpdateParams))
                    this.analysisUpdateParams.remainLecLevel = riskLecValue.lecgrade
                    this.analysisUpdateParams.remainLecValue = JSON.stringify(riskLecValue)
                }
            }
            this.methodUpdateParams = {
                lslvalue: null,
                lssvalue: null,
                lsgrade: null,
                leclvalue: null,
                lecevalue: null,
                leccvalue: null,
                lecgrade: null,

            } as any;
            this.riskUpdateForm()
            this.remainUpdateForm()
            this.showRiskUpdate = false
            this.compareRiskLevel()
        }

    }

    public popCallback(data) {
        if (data.value === 'cancel') {
            this.showRiskValueUpdate = false
        } else if (data.value === 'save') {
            //LS 计算
            if (this.showRiskValueModal.indexOf('ls') !== -1) {
                if (this.showRiskValueModal === 'lslvalue') {
                    this.methodUpdateParams.lslvalue = this.currentRow.grade
                } else if (this.showRiskValueModal === 'lssvalue') {
                    this.methodUpdateParams.lssvalue = this.currentRow.grade
                }
                let lsdata = this.countLS(this.methodUpdateParams.lslvalue, this.methodUpdateParams.lssvalue)
                this.methodUpdateParams.lsrvalue = lsdata.rvalue
                this.methodUpdateParams.lsgrade = lsdata.lsgrade
                //清除必填提示
                this.clearShowError(this.methodUpdateParams, this.riskLSUpdateOptions)
            } else if (this.showRiskValueModal.indexOf('lec') !== -1) {
                //LEC 计算
                if (this.showRiskValueModal === 'leclvalue') {
                    this.methodUpdateParams.leclvalue = this.currentRow.grade
                } else if (this.showRiskValueModal === 'lecevalue') {
                    this.methodUpdateParams.lecevalue = this.currentRow.grade
                } else if (this.showRiskValueModal === 'leccvalue') {
                    this.methodUpdateParams.leccvalue = this.currentRow.grade
                }
                let lecdata = this.contLEC(this.methodUpdateParams.leclvalue, this.methodUpdateParams.lecevalue, this.methodUpdateParams.leccvalue)
                this.methodUpdateParams.lecdvalue = lecdata.dvalue
                this.methodUpdateParams.lecgrade = lecdata.lecgrade
                //清除必填提示
                this.clearShowError(this.methodUpdateParams, this.riskLECUpdateOptions)
            }
            this.showRiskValueUpdate = false;
        }
    }
    /**
     * 清除必填提示
     * @param methodUpdateParams 输入的数据
     * @param updateOPtion 需要清除的form
     */
    public clearShowError(methodUpdateParams: any, updateOPtion: any) {
        for (let i in methodUpdateParams) {
            if (methodUpdateParams[i]) {
                const option = updateOPtion.find(item => item.key === i) as any;
                option.showError = false
            }
        }
    }

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

    public handleChange(data, item) {
        this.subBasicForm()
        if (item.key === "reviewStartTime") {
            let diff = moment(data).diff(moment().subtract(1, 'day'), "months")
            if (diff < 1) {
                this.$message.error("请选择 " + moment().add(1, 'month').format('YYYY-MM-DD') + ' 之后的日期');
                this.analysisUpdateParams.reviewStartTime = null
                return
            }
        }
    }


    // 单选评估值
    public handleCurrentChange(data) {
        this.currentRow = data;
    }

    //LS 计算R值，风险等级
    public countLS(lvalue, svalue) {
        let lsdata = {} as any;
        if (lvalue && svalue) {
            lsdata.rvalue = lvalue * svalue
            if (lsdata.rvalue < 9) {
                lsdata.lsgrade = 1
            } else if (lsdata.rvalue < 13) {
                lsdata.lsgrade = 2
            } else if (lsdata.rvalue < 17) {
                lsdata.lsgrade = 3
            } else if (lsdata.rvalue < 26) {
                lsdata.lsgrade = 4
            }
        }
        return lsdata
    }
    //LEC 计算D值，风险等级
    public contLEC(lvalue, evalue, cvalue) {
        let lecdata = {} as any;
        if (lvalue && evalue && cvalue) {
            lecdata.dvalue = lvalue * evalue * cvalue
            if (lecdata.dvalue < 71) {
                lecdata.lecgrade = 1
            } else if (lecdata.dvalue < 160) {
                lecdata.lecgrade = 2
            } else if (lecdata.dvalue < 320) {
                lecdata.lecgrade = 3
            } else if (lecdata.dvalue > 319) {
                lecdata.lecgrade = 4
            }
        }
        return lecdata
    }

    analyCallback(data) {
        //Emit false
        if (data.value === "cancel") {
            this.visible = false;
        } else if (data.value === "store") {


            let vaildParams = true
            if ((this.$refs.basicForm as any).vaildParams() === false) {
                //基本信息
                vaildParams = false
            }
            if ((this.$refs.riskForm as any).vaildParams() === false) {
                //风险
                vaildParams = false
            }
            if ((this.$refs.remantRiskForm as any).vaildParams() === false) {
                //残余风险
                vaildParams = false
            }
            if (vaildParams === true) {
                if (this.analysisUpdateParams.measures.length === 0) {
                    this.$message.warning('请添加管控措施！')
                    return
                }

                const tmpUpdateParams = JSON.parse(JSON.stringify(this.updateParams))
                // this.analysisUpdateParams.measures = this.currentMeasureData.datas
                //整理数据
                this.collationAnalysisDataMethod();

                tmpUpdateParams.steps.splice(tmpUpdateParams.steps.findIndex((item: any) => item.id === this.analysisUpdateParams.id), 1, this.analysisUpdateParams)

                const riskLevel = tmpUpdateParams.steps.map((item) => item.riskLevel ? item.riskLevel : 0);
                const maxRiskLevel = Math.max(...riskLevel);
                const remainRiskLevel = tmpUpdateParams.steps.map((item) => item.remainLevel ? item.remainLevel : 0);
                const maxRemainRiskLevel = Math.max(...remainRiskLevel);

                if (!tmpUpdateParams.analRiskLevel) {
                    const areaData = this.areaList.find((itm: any) => itm.value === tmpUpdateParams.areaId) as any;
                    tmpUpdateParams.analRiskLevel = areaData.analRiskLevel
                }
                tmpUpdateParams.riskLevel = maxRiskLevel > tmpUpdateParams.analRiskLevel ? maxRiskLevel : tmpUpdateParams.analRiskLevel
                tmpUpdateParams.remainRiskLevel = maxRemainRiskLevel
                if (this.type === "device") {
                    if (tmpUpdateParams.items) {
                        tmpUpdateParams.items = tmpUpdateParams.steps
                    }
                }
                this.updateParams = tmpUpdateParams
                this.visible = false;
            }

        }
    }

    public collationAnalysisDataMethod() {
        this.analysisUpdateParams.measuresNum = this.analysisUpdateParams.measures.length;
        this.analysisUpdateParams.seriousResult = this.analysisUpdateParams.seriousResult.join(",");
        this.analysisUpdateParams.safetySign = this.analysisUpdateParams.safetySign.join(",");
        this.analysisUpdateParams.riskChoose = this.analysisUpdateParams.riskChoose.join(",");
        this.analysisUpdateParams.remainChoose = this.analysisUpdateParams.remainChoose.join(",");
        let diff = moment(this.analysisUpdateParams.reviewStartTime).diff(moment().subtract(1, 'day'), "months");
        if (diff > 0) {
            this.analysisUpdateParams.status = 2;
        }
        if (this.analysisUpdateParams.riskChoose.indexOf('LS') === -1) {
            this.analysisUpdateParams.riskLsValue = null;
        }

        if (this.analysisUpdateParams.riskChoose.indexOf('LEC') === -1) {
            this.analysisUpdateParams.riskLecValue = null;
        }

        if (this.analysisUpdateParams.remainChoose.indexOf('LS') === -1) {
            this.analysisUpdateParams.remainLsValue = null;
        }

        if (this.analysisUpdateParams.remainChoose.indexOf('LEC') === -1) {
            this.analysisUpdateParams.remainLecValue = null;
        }
    }

    //分析法 
    @Emit("actionCallback")
    actionCallback(data) {
        //Emit false
    }
}

</script>
<style lang="scss" scoped src="../views/common.component.scss"></style>
<style lang="scss" scoped>
::v-deep {
    .el-table__body {
        tr {
            &.current-row {
                &>td {
                    // background-color: #68C23A !important;
                    color: #409EFF;
                    background: #E3F1FF !important;
                    // border: 1px solid #409EFF;

                }
            }
        }
    }

    .el-table--enable-row-hover {
        .el-table__body {
            tr {
                &:hover {
                    &>td {
                        cursor: pointer;
                    }
                }
            }
        }
    }
}

.measure-tips {
    width: 100%;
    color: #F56C6C;
    margin-left: -100px;
    margin-bottom: 20px;
    margin-top: -10px;
    font-size: 20px;
    scale: 0.5;
}
</style>