<template>
    <div class="planFormulation">
        <el-row>
            <el-form :inline="true" :model="formInline" class="search-bm-boder" ref="formInline">
                <el-form-item label="上报单位：" v-if="isSj">
                    <el-select
                            class="inputWidth"
                            v-model="formInline.jcdwId"
                            placeholder="请选择"
                    >
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="填报周期：">
                    <el-date-picker
                            type="month"
                            class="inputWidth"
                            value-format="yyyy-MM"
                            :picker-options="pickerOptions"
                            placeholder="选择日期"
                            v-model="formInline.tjsj"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <div class="search-button">
                        <span class="search-line"></span>
                        <el-button type="primary" size="small" class="iconfont iconchaxun" @click="doSearch">查询</el-button>
                        <el-button type="primary" @click="resetInfo('formInline')" size="small" class="iconfont iconzhongzhi"> 重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row class="addBtnBg">
<!--            <el-button type="primary" class="iconfont iconbaocun" size="small" @click="saveSubmit()"> 保存</el-button>-->
<!--            <el-button type="primary" class="iconfont icontijiao" size="small" @click="affirmSubmit()"> 确认</el-button>-->
<!--            <el-button type="primary" class="iconfont icontuijian1" size="small" @click="backSubmit()" v-if="isSj"> 退回</el-button>-->
            <el-button type="primary" class="iconfont icondaochu11" size="small" @click="exportSubmit()"> 导出</el-button>
        </el-row>
        <el-row id="pdfId">
            <h2 style="font-size: 20px;font-weight: 500;text-align:center;">{{searchTime}}粮食流通监督检查工作开展情况表</h2>
            <el-row style="padding:10px"> 填报单位: {{ resJcdwmc }} </el-row>
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    border
                    :span-method="objectSpanMethod"
                    v-loading="loading"
            >
                <el-table-column label="项目" align="right" width="220">
                    <el-table-column prop="col1" width="40"></el-table-column>
                    <el-table-column prop="name" label="内容" width="180">
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="index"
                        label="当月检查基本情况"
                        align="center"
                >
                    <el-table-column
                            prop="dyjccs"
                            label="次数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.dyjccs > 0"
                            >{{ scope.row.dyjccs }}</el-link
                            >
                            <span v-else>{{ scope.row.dyjccs }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="dyjcrc"
                            label="人数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.dyjcrc > 0"
                            >{{ scope.row.dyjcrc }}</el-link
                            >
                            <span v-else>{{ scope.row.dyjcrc }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="dyjcgs"
                            label="个数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.dyjcgs > 0"
                            >{{ scope.row.dyjcgs }}</el-link
                            >
                            <span v-else>{{ scope.row.dyjcgs }}</span>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="index"
                        label="累计情况"
                        align="center"
                >
                    <el-table-column
                            prop="ljjccs"
                            label="次数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.ljjccs > 0"
                            >{{ scope.row.ljjccs }}</el-link
                            >
                            <span v-else>{{ scope.row.ljjccs }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="ljjcrc"
                            label="人数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.ljjcrc > 0"
                            >{{ scope.row.ljjcrc }}</el-link
                            >
                            <span v-else>{{ scope.row.ljjcrc }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="ljjcgs"
                            label="个数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.ljjcgs > 0"
                            >{{ scope.row.ljjcgs }}</el-link
                            >
                            <span v-else>{{ scope.row.ljjcgs }}</span>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="xzcf"
                        label="行政处罚处理情况"
                        align="center"
                >
                    <el-table-column
                            prop="dyjczg"
                            label="责令整改(例数)"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.dyjczg > 0"
                            >{{ scope.row.dyjczg }}</el-link
                            >
                            <span v-else>{{ scope.row.dyjczg }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="dyjcjg"
                            label="警告(例数)"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.dyjcjg > 0"
                            >{{ scope.row.dyjcjg }}</el-link
                            >
                            <span v-else>{{ scope.row.dyjcjg }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="ljjcfm"
                            label="罚没"
                            align="center"
                    >
                        <el-table-column
                                prop="index"
                                label="例数"
                                align="center"
                        >
                            <template slot-scope="scope">
                                <el-link
                                        type="primary"
                                        :underline="false"
                                        v-if="scope.row.ljjcfm > 0"
                                >{{ scope.row.ljjcfm }}</el-link
                                >
                                <span v-else>{{ scope.row.ljjcfm }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                                prop="index"
                                label="金额(万元)"
                                align="center"
                        >
                            <template slot-scope="scope">
                                <el-link
                                        type="primary"
                                        :underline="false"
                                        v-if="scope.row.xzcf > 0"
                                >{{ scope.row.xzcf }}</el-link
                                >
                                <span v-else>{{ scope.row.xzcf }}</span>
                            </template>
                        </el-table-column>
                    </el-table-column>
                    <el-table-column
                            prop="index"
                            label="移交其他部门处理(例数)"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-link
                                    type="primary"
                                    :underline="false"
                                    v-if="scope.row.xzcf> 0"
                            >{{ scope.row.xzcf }}</el-link
                            >
                            <span v-else>{{ scope.row.xzcf }}</span>
                        </template>
                    </el-table-column>
                </el-table-column>
            </el-table>

            <el-row style="margin:14px;margin-bottom:25px;" v-if="showText">
                <span>监督检查机构负责人：</span>
                <span style="margin-left: 250px;">填表人：</span>
                <span style="margin-right: 150px;float:right;">填报日期：</span>
            </el-row>
        </el-row>
    </div>
</template>

<script>
    import htmlToPdf from '@/utils/htmlToPdf'
    import {
        tree,
        dailyInspectionReport,
        dailyCheckResult} from "@/api/jdjcbb/inspectionWork"
    import {inspectionUnit} from "@/api/jdjcbb/paracticable";
    import moment from "moment";
    export default {
        name: 'index',
        data() {
            return {
                resJcdwmc: null,
                isSj: true,
                multipleSelection: [],
                formInline: {
                   tjsj: null,
                    tblx: 0,
                    jcdwId: '',
                    jcdwmc: '',
                },
                tjsj: null,
                rydwval: '', //人员单位
                rydw: [],
                czqk: [],
                jcdwmc: '', //
                options:[],
                loading: false,
                pageForm: {
                    pageNum: 1,
                    pageSize: 10,
                    total: 0
                },
                pickerOptions:{
                    disabledDate(time) {
                        let num = 30
                        let m = new Date().getMonth() == 1
                        if(m == 1){
                            if(new Date().getFullYear() / 4 === 0){
                                num = 29
                            }else{
                                num = 28
                            }
                        }else if(m == 0 || m == 2 || m == 4 || m == 6 || m == 7 || m == 9 || m == 11){
                            num = 31
                        }else{
                            num = 30
                        }
                        return time.getTime() > Date.now()-(num * 24 * 3600 * 1000);
                    },
                },
                tableData: [],
                tree: [],
                leftTitleDict: {},
                searchTime: '',
                showText: false,
            }
        },
        created() {
            let lastM = moment().subtract(1, 'month').calendar();//06/01/2020
            let timeArr = lastM.split('/')
            this.formInline.tjsj = timeArr[2] + '-' + timeArr[0]
            this.tjsj = this.formInline.tjsj
            this.searchTime = timeArr[2] + '年' + Number(timeArr[0]) + '月'
            this.init() //有确定数据再打开
            if(sessionStorage.getItem('orgCode').length == 5){
                this.isSj = true
                this.getSbdw()
            }else{
                this.isSj = false
                this.formInline.jcdwId = sessionStorage.getItem('orgCode')
                this.resJcdwmc = sessionStorage.getItem('orgName')
            }
        },
        methods: {
            async init() {
                let res = await dailyInspectionReport()
                res.data.forEach((d) => {
                    this.leftTitleDict[d.dictCode] = d.dictName
                })
                this.getTree()
            },
            //获取上报单位
            getSbdw(){
                //组织单位树 --检查单位列表
                inspectionUnit(sessionStorage.getItem("orgCode")).then(res=>{
                    if (sessionStorage.getItem("orgCode").length > 5) {
                        delete res.data.children;
                    } else {
                        for (let key in res.data) {
                            if (key == "children") {
                                for (let k = 0; k < res.data[key].length; k++) {
                                    delete res.data[key][k].children;
                                }
                            }
                        }
                        //截取16个区粮办
                        let children = res.data.children.slice(0, 16);
                        // children.forEach((item,index)=>{
                        // 	children[index].orgName = item.orgName.split("区")[0] + "区发改委"
                        // })
                        res.data.children = children;
                    }
                    this.options.push({
                        value: res.data.orgCode,
                        label: res.data.orgName,
                    });
                    if(res.data.children && res.data.children.length){
                        res.data.children.forEach(item=>{
                            this.options.push({
                                value: item.orgCode,
                                label: item.orgName,
                            });
                        })
                    }
                    this.options.unshift({
                        value: '',
                        label: '合计',
                    });
                }).catch()
            },
            objectSpanMethod({ row, column, rowIndex, columnIndex }) {
                if (columnIndex === 0) {
                    if (rowIndex === 2) {
                        return [13, 1]
                    } else {
                        return [0, 0]
                    }
                }
                if (columnIndex == 1) {
                    if (rowIndex < 2) {
                        return [1, 2]
                    }
                }
            },
            exportSubmit() {
                this.doSearch()
                this.showText = true
                let exportName = ''
                if(this.formInline.tjsj){
                    exportName = this.formInline.tjsj.split('-')[0] + '年' + Number(this.formInline.tjsj.split('-')[1]) + '月粮食流通监督检查工作开展情况表.pdf'
                }else{
                    exportName = '粮食流通监督检查工作开展情况表.pdf'
                }
                let self = this
                setTimeout(function() {
                    htmlToPdf.downloadPDF(
                        document.querySelector('#pdfId'),
                        exportName
                    )
                    self.showText = false
                }, 100)
            },
            async getTree() {
                try {
                    let res = await tree(sessionStorage.getItem('orgCode'))
                    if (res.infoCode == 200) {
                        // this.tree = formatData([res.data])
                        this.treeData = [];
                        if(sessionStorage.getItem('orgCode').length > 5){
                            delete res.data.children
                        }else{
                            for (let key in res.data) {
                                if (key == "children") {
                                    for (let k = 0; k < res.data[key].length; k++) {
                                        delete res.data[key][k].children
                                    }
                                }
                            }
                            //截取16个区粮办
                            let children = res.data.children.slice(0,16)
                            // children.forEach((item,index)=>{
                            // 	children[index].orgName = item.orgName.split("区")[0] + "区发改委"
                            // })
                            res.data.children = children
                        }
                        this.treeData.push(res.data);
                        this.getList('start')
                    }
                } catch (e) {}
            },
            //重置
            resetInfo(){
                this.pageForm.pageNum = 1;
                this.pageForm.pageSize = 10;
                let lastM = moment().subtract(1, 'month').calendar();//06/01/2020
                let timeArr = lastM.split('/')
                this.formInline.tjsj = timeArr[2] + '-' + timeArr[0]
                this.searchTime = timeArr[2] + '年' + Number(timeArr[0]) + '月'
                // this.formInline.jcdwId = sessionStorage.getItem('orgCode')
                // this.formInline.jcdwmc = sessionStorage.getItem('orgName')
                this.formInline.jcdwId = ''
                this.formInline.jcdwmc = ''
                this.tjsj = this.formInline.tjsj
                this.getList()
            },
            //查询
            doSearch(){
                this.pageForm.pageNum = 1
                this.tjsj = this.formInline.tjsj ? this.formInline.tjsj : ''
                console.log(this.tjsj , '=============')
                if(this.tjsj){
                    this.searchTime = this.tjsj.split('-')[0] + '年' + Number(this.tjsj.split('-')[1]) + '月'
                }else{
                    this.searchTime = ''
                }

                this.getList()
            },
            async getList(type) {
                // this.loading = true
                if(this.formInline.jcdwId){
                    this.options.forEach(item=>{
                        if(item.value == this.formInline.jcdwId){
                            this.formInline.jcdwmc = item.label
                            // this.resJcdwmc = item.label
                        }
                    })
                }
                let data = {
                    // pageNum: this.pageForm.pageNum,
                    // pageSize: this.pageForm.pageSize,
                   tjsj: this.formInline.tjsj,
                    jcdwId: this.formInline.jcdwId,
                    jcdwmc: this.formInline.jcdwmc,
                };
                let res = await dailyCheckResult(data)
                this.resJcdwmc = this.formInline.jcdwmc
                this.loading = false
                if (res.infoCode == 200) {
                    let ObjData = Object.keys(res.data).map((d,i) => {
                        return {
                            ...res.data[d],
                            name: this.leftTitleDict[d] || d,
                            col1: "分项检查"
                        }
                    })
                    this.tableData = []
                    this.tableData.push(ObjData[ObjData.length-3])
                    this.tableData.push(ObjData[ObjData.length-2])
                    this.tableData.push(ObjData[ObjData.length-1])
                    ObjData.forEach(item=>{
                        this.tableData.push(item)
                    })
                    this.tableData.pop()
                    this.tableData.pop()
                    this.tableData.pop()
                    return
                }
                this.tableData = []
            },
            formatDateTime(date) {
                let y = date.getFullYear();
                let m = date.getMonth() + 1;
                m = m < 10 ? "0" + m : m;
                let d = date.getDate();
                d = d < 10 ? "0" + d : d;
                let h = date.getHours();
                h = h < 10 ? "0" + h : h;
                let minute = date.getMinutes();
                minute = minute < 10 ? "0" + minute : minute;
                let second = date.getSeconds();
                second = second < 10 ? "0" + second : second;
                return y + '-' + m;
            },
        }
    }
</script>

<style scoped>
    .planFormulation {
        overflow-y: auto;
        margin: 10px;
        border: 1px solid #f4f5f8;
        padding: 10px;
        min-width: 960px;
    }
    .header {
        width: 100%;
    }
    .table {
        margin-left: 20px;
    }
    .moreSelect {
        position: relative;
        display: inline-block;
        margin-left: 20px;
    }
    .moreSelect::before {
        content: '';
        display: block;
        position: absolute;
        left: -10px;
        width: 0px;
        border-left: 1px dashed #75b5fa;
        height: 35px;
    }
</style>
