
<script lang="ts">
import { Component } from 'vue-property-decorator';
import template from "./duty.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';
import moment from 'moment';

@Component({
    template,
    components: {
        FormComponent,
        TableComponent,
        DrawComponent,
        UnitTreeComponent,
    },
})
export default class DutyManagerComponent 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: "analControlName",
        type: "text",
    }, {
        name: "分析对象",
        key: "itemName",
        type: "text",
    }];

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

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

    public showFile = false;

    public currentUrl = null;

    public account = JSON.parse(localStorage.getItem("account") as any);

    public userListName = {} as any;

    public subUpdateOptions = [] as any;
    public subUpdateForm() {
        this.subUpdateOptions = [{
            name: "是否包保责任人任务",
            key: "insuranceDutyFlag",
            format: "insuranceDutyFlagName",
            type: "radio",
            width: "100%",
            labelWidth: 'auto',
            require: true,
            disable: true,
            datas: [
                {
                    name: "否",
                    value: 0
                },
                {
                    name: "是",
                    value: 1
                }
            ]
        }, {
            name: "隐患排查任务",
            key: "taskName",
            type: "text",
            width: "100%",
            require: true,
            disable: true,
        }, {
            name: "排查结果",
            key: "result",
            type: "radio",
            width: "100%",
            require: true,
            datas: [
                {
                    name: "正常",
                    value: 1
                },
                {
                    name: "异常",
                    value: 2
                }
            ]
        }, {
            name: "是否为隐患",
            key: "dangerFlag",
            format: "dangerFlagName",
            type: "radio",
            width: "100%",
            labelWidth: 'auto',
            require: true,
            disable: this.updateParams.result === 2,
            datas: [
                {
                    name: "否",
                    value: 0
                },
                {
                    name: "是",
                    value: 1
                }
            ]
        }, {
            name: "问题描述",
            key: "taskName",
            type: "textarea",
            width: "100%",
            require: true,
        }, {
            name: "隐患类别",
            key: "taskType",
            format: "taskTypeName",
            type: "select",
            width: "100%",
            require: true,
            datas: this.$store.state.prevention_hazard_category,
        }, {
            name: "隐患归属人",
            key: "ownerId",
            type: "select",
            width: "100%",
            require: true,
            datas: this.$store.state.userList,
        },
        {
            name: "IMEI码",
            key: "imeiCode",
            type: "text",
            width: "100%",
            require: true,
            disable: true,
        },
        {
            name: " 排查人",
            key: "checkUserId",
            type: "select",
            width: "100%",
            require: true,
            disable: true,
            datas: this.$store.state.userList,
        },
        {
            name: " 排查时间",
            key: "checkTime",
            type: "text",
            width: "100%",
            require: true,
            disable: true,
        }, {

            name: "附件",
            key: "resourceId",
            ref: "file",
            type: "upload",
            width: "calc(50% - 20px)",
            showError: false,
            onSucess: this.onSuccess2,
            onMove: this.onRemove2,
            onPreview: this.onPreview,
            autoUpload: true,
            accept: "image/png, image/jpeg,.doc,.docx,.xls,.xlsx,.pdf",
            listType: "text",
            tip: "请上传.pdf,.png,.jpg,.doc.docx,.xls,.xlsx格式文件",
            fileList: this.fileList,
            btn: [{
                name: "点击上传",
                value: "upload",
                size: "small",
                type: "primary"
            }]
        }
        ]
    }

    public subActions = [{
        name: "取消",
        value: "cancel"
    }, {
        name: "保存并提交",
        value: "save",
        type: "primary"
    }]



    created() {
        this.subUpdateForm()
    }
    // 树点击
    public handleNodeClick(data) {
        this.params.unitName = "";
        this.params.areaName = "";
        if (data.areaId) {
            this.params.unitName = data.name
        } else {
            this.params.areaName = data.name === "全部" ? "" : data.name
        }
        this.getTableData()
    }

    public buildTable() {
        this.tableColumn.push({ name: '管控对象', key: "analControlName" });
        this.tableColumn.push({ name: '分析对象', key: "itemName" });
        this.tableColumn.push({ name: '管控措施', key: "measureDescription" });
        this.tableColumn.push({ name: '隐患排查任务', key: "taskName" });
        this.tableColumn.push({ name: '结束时间', key: "endTime" });
        this.tableColumn.push({ name: '责任人', key: "taskChargeUserName" });
        this.tableColumn.push({ name: '生成时间', key: "generateTime" });
        this.tableColumn.push({
            name: '任务状态', width: "110px", key: "status", render: (data) => {
                if (data.status == 2) {
                    return "<span class='noDraw'>待排查</span>"
                } else if (data.status == 1) {
                    return "<span class='color_1'>已排查</span>"
                } else if (data.status == 3) {
                    return "<span class='color_3'>超期未排查</span>"
                }
            },
            filters: this.$store.state.prevention_task_status.map(item => {
                return {
                    text: item.name,
                    value: item.value,
                }
            }), filterMethod: (data, row) => {
                return row.status === data;
            }
        });
    }

    public callback(data, type) {
        if (type) {
            this.params[type] = data;
            this.getTableData();
            return
        }
        // 查询
        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))
        }
    }

    // 分页数据
    public getTableData() {
        this.tableService.selectByPage(this.params).then(res => {
            this.tableData = res.data as any;
        })
    }

    public subActionCallback(data) {
        if (data.value === "cancel") {
            this.showUpdate = false
            this.updateParams = {} as any;
        } else if (data.value === "save") {
            this.doSave()
        }
    }

    public doSave() {
        if (this.fileList.length > 0) {
            let resourceId = this.fileList.map((item) => {
                return item.id
            })
            this.updateParams.resourceId = resourceId.join(";")
        }

        this.$store.state.userList.map((item) => {
            this.userListName[item.value] = item.name
        })


        this.updateParams.taskChargeUserName = this.userListName[this.updateParams.checkUserId]
        this.updateParams.ownerName = this.userListName[this.updateParams.ownerId]
        this.tableService.addOrUpdate(this.updateParams, this.updateParams.id).then((res) => {
            this.$message.success("编辑成功！");
            this.showUpdate = false
            this.getTableData();
        })
    }

    public changes(data, item) {
        if (item && item.key === "result") {
            if (data === 2) {
                this.updateParams.dangerFlag = 1
            }
            this.subUpdateForm()
        }

    }

    // 重置数据
    public reset() {
        this.params = {
            pageNum: 1,
            pageSize: 20,
        } 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) {
        const mathData = Math.floor(Math.random() * 1000);
        this.updateParams = Object.assign({
            checkUserId: this.account.userId,
            checkTime: moment().format('YYYY-MM-DD HH:mm'),
            imeiCode: moment().format('DDHHmm') + mathData,
        }, row)
        this.updateParams.status = 1
        this.updateParams.endTime = moment().format('YYYY-MM-DD HH:mm:ss')
        this.subUpdateForm()
        this.showUpdate = true
    }
}
</script>
<style lang="scss" scoped src="../../common.component.scss"></style>
