
<script lang="ts">
import { Component } from 'vue-property-decorator';
import template from "./dutyRecords.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 DutyService from "@/service/duty.service"
import FormOption from "hbt-common/models/formOptions"
import BtnOption from "hbt-common/models/btnOptions"
import DrawComponent from '@/components/draw.component.vue';
import UnitTreeComponent from '@/components/tree.component.vue';


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

export default class DutyRecordsManagerComponent extends BaseRecordComponent<any> {
    public tableService = new DutyService();

    public params = {} as any;

    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"
    }];
    public footerActions = [{
        name: "选择全部",
        value: "selectAll",
        type: "primary"
    }, {
        name: "反向选择",
        value: "reverse"
    }];
    public formOptions: FormOption<BtnOption>[] = [{
        name: "排查结果",
        key: "result",
        type: "select",
        datas: [{
            name: "正常",
            value: 1
        }, {
            name: "异常",
            value: 2
        }]
    }, {
        name: "是否为隐患",
        key: "dangerFlag",
        type: "select",
        datas: [{
            name: "否",
            value: 0
        }, {
            name: "是",
            value: 1
        }]
    }];

    public showUpdate = false;

    public updateParams = {} as any;

    public selectData = [];

    public fileList = [] as any;
    public photoList = [] as any;

    public showFile = false;

    public currentUrl = null;

    public subUpdateOptions = [] as any;
    public subUpdateForm() {
        this.subUpdateOptions = [{
            name: "是否包保责任人任务",
            key: "insuranceDutyFlag",
            format: "insuranceDutyFlagName",
            type: "text",
            labelWidth: 'auto',
            require: true,
            width: "calc(50% - 20px)"
        }, {
            name: "隐患排查任务",
            key: "taskName",
            type: "text",
            require: true,
            width: "calc(50% - 20px)"
        }, {
            name: "排查结果",
            key: "result",
            format: "resultName",
            type: "radio",
            require: true,
            width: "calc(50% - 20px)"
        }, {
            name: "是否为隐患",
            key: "dangerFlag",
            format: "dangerFlagName",
            type: "radio",
            require: true,
            width: "calc(50% - 20px)",
        }, {
            name: "问题描述",
            key: "taskName",
            type: "textarea",
            require: true,
            width: "calc(50% - 20px)",
        }, {
            name: "隐患类别",
            key: "dangerType",
            format: "taskTypeName",
            type: "select",
            require: true,
            width: "calc(50% - 20px)",
        }, {
            name: "隐患归属人",
            key: "ownerId",
            format: "ownerName",
            require: true,
            width: "calc(50% - 20px)",
        },
        {
            name: "IMEI码",
            key: "imeiCode",
            type: "text",
            width: "calc(50% - 20px)",
            require: true,
        },
        {
            name: " 排查人",
            key: "checkUserId",
            format: 'checkUserName',
            type: "text",
            width: "calc(50% - 20px)",
            require: true,
        },
        {
            name: " 排查时间",
            key: "checkTime",
            type: "text",
            width: "100%",
            require: true,
        }, {

            name: "附件",
            key: "resourceId",
            ref: "file",
            format: "resourceIdName",
            type: "upload",
            width: "100%",
            showError: false,
            onSucess: this.onSuccess2,
            onMove: this.onRemove2,
            onPreview: this.onPreview,
            autoUpload: true,
            accept: "image/png, image/jpeg",
            listType: "picture-card",
            fileList: this.photoList,
        }
        ]
    }



    created() {

    }
    // 树点击
    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: "taskName", with: "250px" });
        this.tableColumn.push({
            name: '排查结果', key: "result", render: (data) => {
                if (data.status == 0) {
                    return "<span class='noDraw'>异常</span>"
                } else if (data.status == 1) {
                    return "<span class='color_1'>正常</span>"
                }
            }
        });
        this.tableColumn.push({ name: '问题描述', key: "description" });
        this.tableColumn.push({
            name: '排查人', key: "checkUserId", render: (data) => {
                if (data.checkUserId) {
                    return this.$store.getters.user_map[data.checkUserId]
                }
            }
        });
        this.tableColumn.push({ name: '排查时间', key: "checkTime" });
        this.tableColumn.push({
            name: '是否为隐患', key: "dangerFlag", render: (data) => {
                if (data.status == 1) {
                    return "<span class='noDraw'>是</span>"
                } else if (data.status == 0) {
                    return "<span>否</span>"
                }
            }
        });
        this.tableColumn.push({ name: '隐患归属人', key: "ownerName" });

    }

    public callback(data, type) {
        if (type) {
            this.params[type] = data;
            this.getTableData();
            return
        }
        // 查询
        if (data.value === "search") {
            this.getTableData()
            console.log('this.tableData',this.tableData);
            
            // 重置
        } 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))
        }
    }
    // 重置数据
    public reset() {
        this.params = {
            pageNum: 1,
            pageSize: 20,
        } as any;
    }
    // 分页数据
    public getTableData() {
        this.tableService.selectByPage2(this.params).then(res => {
            this.tableData.datas = res.data as any;
        })
    }

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

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


    public toggleAll() {
        
        this.tableData.datas.forEach((item,index)=>{
            (this.$refs.multipleTable as any).toggleRowSelection(item);
        })
    }
    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 onSuccess(res, file, fileList) {
        if (res.code === 200) {
            this.photoList.push({
                name: res.data.originalName,
                url: res.data.url,
                type: res.data.type,
                id: res.data.id
            });
            this.updateParams.photo = this.photoList.length || null
        }
    }
    public onSuccess2(res, file, fileList) {
        if (res.code === 200) {
            this.fileList.push({
                name: res.data.originalName,
                url: res.data.url,
                type: res.data.type,
                id: res.data.id
            })
        }

    }

    public onRemove(file, fileList) {
        this.photoList.splice(this.photoList.findIndex(item => item.id === file.response.data.id), 1)
        this.updateParams.photo = this.photoList.length || null
    }
    public onRemove2(file, fileList) {
        this.fileList.splice(this.fileList.findIndex(item => item.id === file.response.data.id), 1)
    }

    public onPreview(file) {
        if (file.type.indexOf("png") >= 0 || file.type.indexOf("jp") >= 0) {
            this.currentUrl = file.url;
            this.showFile = true;
        } else {
            window.open(file.url, "_blank")
        }
    }


    public showSubModal(row) {
        this.photoList = []
        if (row.resourceId) {
            this.tableService.getFileUrls({ ids: row.resourceId.split(";") }).then((photos: any) => {
                this.photoList = photos.data.map(item => {
                    return {
                        name: item.originalName,
                        url: item.url,
                        type: item.type,
                        id: item.id
                    }
                })
                this.subUpdateForm()
            })
        }

        this.updateParams = Object.assign({
            insuranceDutyFlagName: row.insuranceDutyFlag === 0 ? '否' : '是',
            resultName: row.result === 1 ? '正常' : '异常',
            dangerFlagName: row.dangerFlag === 0 ? '否' : '是',
            taskTypeName: this.$store.getters.prevention_hazard_category_map[row.dangerType],
            checkUserName: this.$store.getters.user_map[row.checkUserId]
        }, row)
        this.showUpdate = true
        this.updateParams.resourceId = null
        this.subUpdateForm()
    }
}
</script>
<style lang="scss" scoped src="../../common.component.scss"></style>
