<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.dwid"-->
<!--                            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="year"-->
<!--                            class="inputWidth"-->
<!--                            value-format="yyyy"-->
<!--                            :picker-options="pickerOptions"-->
<!--                            placeholder="选择日期"-->
<!--                            v-model="formInline.tbnd"-->
<!--                    ></el-date-picker>-->
                    <quarter @getValue="getValue" :defaultValue="nowTime"  v-if="flag"></quarter>
                </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></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>
            <h2 style="font-size: 20px;font-weight: 500;text-align:center;">粮食和物资储备行政管理部门{{tbnd}}第{{jd}}季度执法督查机构、人员和经费落实情况统计表</h2>
        </el-row>
        <el-row id="pdfId">
            <el-row style="padding:10px"> 填报单位:
<!--                {{ resJcdwmc }}-->
                <span style="float: right;padding-right: 10px;">单位：个、万元</span></el-row>
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    border
                    v-loading="loading"
            >
                <el-table-column label="" align="center">
                    <el-table-column prop="orgname" label="行政区级" width="120" align="center">
                    </el-table-column>
                </el-table-column>
                <el-table-column label="行政区数（个）" align="center">
                    <template slot-scope="scope">
                        <span v-if="isSj">{{scope.row.xzqs}}</span>
                        <el-input v-model="scope.row.xzqs" v-else></el-input>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="index"
                        label="粮食和物资储备部门行政机构设置情况"
                        align="center"
                >
                    <el-table-column
                            prop="xsxzhj"
                            label="合计"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.xzjgszhj}}</span>
                            <el-input v-model="scope.row.xzjgszhj" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="xzbz"
                            label="独立机构"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.dljg}}</span>
                            <el-input v-model="scope.row.dljg" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="sybz"
                            label="加挂粮食和物资储备局牌子"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.jglshwzcbp}}</span>
                            <el-input v-model="scope.row.jglshwzcbp" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="sybz"
                            label="职能整合入其他部门"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.znzhrqtbm}}</span>
                            <el-input v-model="scope.row.znzhrqtbm" v-else></el-input>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="index"
                        label="粮食和物资储备部门人员编制情况"
                        align="center"
                >
                    <el-table-column
                            prop="xsxzhj"
                            label="合计"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.rybzhj}}</span>
                            <el-input v-model="scope.row.rybzhj" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="xzbz"
                            label="行政编制"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.xzbz}}</span>
                            <el-input v-model="scope.row.xzbz" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="sybz"
                            label="事业编制 "
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.sybz}}</span>
                            <el-input v-model="scope.row.sybz" v-else></el-input>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="index"
                        label="粮食和物资储备部门内设执法督查机构情况"
                        align="center"
                >
                    <el-table-column
                            prop="jgshj"
                            label="合计"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.zfdchj}}</span>
                            <el-input v-model="scope.row.zfdchj" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="bbpzclhj"
                            label="独立设立执法督查（监督检查）处（科、股）"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.dlslzfdc}}</span>
                            <el-input v-model="scope.row.dlslzfdc" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="dyjcrc"
                            label="加挂执法督查（监督检查）处（科、股）牌子"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.jgzfdc}}</span>
                            <el-input v-model="scope.row.jgzfdc" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="dyjcgs"
                            label="职能整合入其他处（科、股）"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.znzhdqtc}}</span>
                            <el-input v-model="scope.row.znzhdqtc" v-else></el-input>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="xzcf"
                        label="具备行政执法（监督检查）资格的人数（人）"
                        align="center"
                >
                    <el-table-column
                            prop="dyjcjg"
                            label="合计"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.zfzgrshj}}</span>
                            <el-input v-model="scope.row.zfzgrshj" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="index"
                            label="其中：获得粮食和物资储备监督检查证的人数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.qzhdlshwzcbz}}</span>
                            <el-input v-model="scope.row.qzhdlshwzcbz" v-else></el-input>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                        prop="xzcf"
                        label="执法督查经费落实情况"
                        align="center"
                >
                    <el-table-column
                            prop="dyjcjg"
                            label="落实经费的粮食和物资储备部门数"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.lsjfdlshwzcbbm}}</span>
                            <el-input v-model="scope.row.lsjfdlshwzcbbm" v-else></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="index"
                            label="年度落实经费金额"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.ndlsjfje}}</span>
                            <el-input v-model="scope.row.ndlsjfje" v-else></el-input>
                        </template>
                    </el-table-column>
                </el-table-column>
            </el-table>
        </el-row>

    </div>
</template>

<script>
    import htmlToPdf from '@/utils/htmlToPdf'
    import {inspectionUnit, getJflsList, exportJfls, exportZfd} from "@/api/jdjcbb/paracticable"
    import quarter from '@/components/quarter/index'
    import moment from 'moment'
    export default {
        name: 'index',
        components: {
            quarter
        },
        data() {
            return {
                resJcdwmc: null,
                isSj: true,
                multipleSelection: [],
                formInline: {
                    tbnd: null,
                    tblx: 0,
                    dwid: '',
                    dwmc: null,
                },
                tbnd: null,
                jd: null,
                options:[],
                loading: false,
                pickerOptions:{
                    disabledDate(time) {
                        return time.getTime() > Date.now()-8.64e7;
                    },
                },
                tableData: [],
                tree: [],
                nowTime: '',
                flag: true
            }
        },
        created() {
            this.initTbnd()
            this.getList() //有确定数据再打开
            if(sessionStorage.getItem('orgCode').length == 5){
                // this.isSj = true
                // this.getSbdw()
            }else{
                // this.isSj = false
                // this.formInline.dwid = sessionStorage.getItem('orgCode')
                // this.resJcdwmc = sessionStorage.getItem('orgName')
            }
        },
        methods: {
            initTbnd(){
                //默认上个季度
                let lastM = moment().subtract(3, 'month').calendar();//06/01/2020
                let timeArr = lastM.split('/')
                this.tbnd = timeArr[2]
                let jdTime = ''
                if(timeArr[0] == '01' || timeArr[0] == '02' || timeArr[0] == '03'){
                    this.jd = 1
                    jdTime = '03'
                }else if(timeArr[0] == '04' || timeArr[0] == '05' || timeArr[0] == '06'){
                    this.jd = 2
                    jdTime = '06'
                }else if(timeArr[0] == '07' || timeArr[0] == '09' || timeArr[0] == '09'){
                    this.jd = 3
                    jdTime = '09'
                }else{
                    this.jd = 4
                    jdTime = '12'
                }
                this.nowTime = this.tbnd+'-'+jdTime
                this.formInline.tbnd = this.tbnd+'-'+jdTime
                this.nj = this.tbnd + '第' + this.jd +'季度'
            },
            getValue(val){
                this.nj = val
            },
            selectInit(row,index){
                if(row.sjzt == '0'){
                    return false  //不可勾选
                }else{
                    return true  //可勾选
                }
            },
            disabledFun(sjzt){
                return (sessionStorage.getItem('orgCode').length == 5 && sjzt == '0')? false: true
            },
            //获取上报单位
            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,
                            });
                        })
                    }else{
                    }
                    this.options.unshift({
                        value: '',
                        label: '合计',
                    });
                }).catch()
            },
            //重置
            resetInfo(){
                this.flag = false;
                this.$nextTick(() => {
                    this.flag = true;
                });
                this.initTbnd()
                this.getList()
            },
            //查询
            doSearch(){
                this.formInline.tbnd = this.nj.substring(0,4) + '-' + (this.nj.substring(5,6)*3<10?'0'+this.nj.substring(5,6)*3:this.nj.substring(5,6)*3)
                this.tbnd = this.nj.substring(0,4)
                this.jd = this.nj.substring(5,6)
                this.getList()
            },
            //获取列表
            getList() {
                let tbnd = this.formInline.tbnd
                let timeArr = this.nowTime.split('-')
                let nowY = timeArr[0]
                let nowJ = Math.floor(timeArr[1]/3) + 1
                // if(nowY == tbnd.split('-')[0] && nowJ == (Math.floor(tbnd.split('-')[1]/3))){
                //     tbnd = this.nowTime
                // }
                let data = {
                    tbnd
                };
                getJflsList(data)
                    .then(res => {
                        if (res.infoCode == 200) {
                            this.tableData = res.data;
                            let obj = {
                                orgname: '地市级',
                                bmryhj: null,
                                dljg: null,
                                dlslzfdc: null,
                                fzzjgsl: null,
                                jglshwzcbp: null,
                                jgzfdc: null,
                                lsjfdlshwzcbbm: null,
                                ndlsjfje: null,
                                nshj: null,
                                orgid: "LS001",
                                qzhdlshwzcbz: null,
                                sybz: 0,
                                xzbz: 0,
                                xzjghj: null,
                                xzqs: 0,
                                xzzfhj: null,
                                zjxzzfdhj: null,
                                znzhdqtc: null,
                                znzhrqtbm: null,
                                zzjgsl: null,
                                rybzhj: null,
                                zfzgrshj: null,
                                xzjgszhj: null,
                                zfdchj: null,
                            }
                            this.tableData.forEach((item,index)=>{
                                if((item.sybz || item.sybz == 0) || (item.xzbz || item.xzbz == 0)) {
                                    item.rybzhj = item.sybz + item.xzbz
                                }
                                if(item.qzhdlshwzcbz || item.qzhdlshwzcbz == 0) {
                                    item.zfzgrshj = item.qzhdlshwzcbz
                                }
                                if((item.dljg || item.dljg == 0) || (item.jglshwzcbp || item.jglshwzcbp == 0) || (item.znzhrqtbm || item.znzhrqtbm == 0)) {
                                    item.xzjgszhj = item.dljg + item.jglshwzcbp + item.znzhrqtbm
                                }
                                if((item.dlslzfdc || item.dlslzfdc == 0) || (item.jgzfdc || item.jgzfdc == 0) || (item.znzhdqtc || item.znzhdqtc == 0)) {
                                    item.zfdchj = item.dlslzfdc + item.jgzfdc + item.znzhdqtc
                                }
                                if(index != 0){
                                    if(item.sybz || item.sybz == 0) obj.sybz += Number(item.sybz)
                                    if(item.xzbz || item.xzbz == 0) obj.xzbz += Number(item.xzbz)
                                    if(item.qzhdlshwzcbz || item.qzhdlshwzcbz == 0) obj.qzhdlshwzcbz += Number(item.qzhdlshwzcbz)
                                    if(item.dljg || item.dljg == 0) obj.dljg += Number(item.dljg)
                                    if(item.jglshwzcbp || item.jglshwzcbp == 0) obj.jglshwzcbp += Number(item.jglshwzcbp)
                                    if(item.znzhrqtbm || item.znzhrqtbm == 0) obj.znzhrqtbm += Number(item.znzhrqtbm)
                                    if(item.dlslzfdc || item.dlslzfdc == 0) obj.dlslzfdc += Number(item.dlslzfdc)
                                    item.orgname = item.orgname.split('区')[0] + '区'
                                    if(item.xzqs || item.xzqs == 0) obj.xzqs += Number(item.xzqs)
                                    if(item.jgzfdc || item.jgzfdc == 0) obj.jgzfdc += Number(item.jgzfdc)
                                    if(item.znzhdqtc || item.znzhdqtc == 0) obj.znzhdqtc += Number(item.znzhdqtc)
                                    if(item.lsjfdlshwzcbbm || item.lsjfdlshwzcbbm == 0) obj.lsjfdlshwzcbbm += Number(item.lsjfdlshwzcbbm)
                                    if(item.ndlsjfje || item.ndlsjfje == 0) obj.ndlsjfje += Number(item.ndlsjfje)
                                    if(item.rybzhj || item.rybzhj == 0) obj.rybzhj += Number(item.rybzhj)
                                    if(item.zfzgrshj || item.zfzgrshj == 0) obj.zfzgrshj += Number(item.zfzgrshj)
                                    if(item.xzjgszhj || item.xzjgszhj == 0) obj.xzjgszhj += Number(item.xzjgszhj)
                                    if(item.zfdchj || item.zfdchj == 0) obj.zfdchj += Number(item.zfdchj)
                                }
                            })
                            let sj = this.tableData.shift()
                            sj.orgname = '省级'
                            this.tableData.unshift(obj)
                            this.tableData.unshift(sj)
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.msg
                            });
                        }
                    }).catch()
            },
            //获取此次更改的数据
            returnDataFun(){
                let saveData = []
                if(this.changeIndex.length){
                    this.tableData.forEach((item,index)=>{
                        this.changeIndex.forEach(val=>{
                            if(val == index){
                                saveData.push(item)
                            }
                        })
                    })
                }
                return saveData
            },
            //保存
            saveSubmit() {
                let obj = this.returnDataFun()
                let params = obj
                // updateList(params).then(res=>{
                //     if(res){
                //         this.$message.success('保存成功')
                //     }else{
                //         this.$message.error('保存失败')
                //     }
                // }).catch()
            },
            //提交
            affirmSubmit() {
                let params = this.returnDataFun()
                // updateList(params).then(res=>{
                //     if(res){
                //         this.$message.success('提交成功')
                //     }else{
                //         this.$message.error('提交失败')
                //     }
                // }).catch()
            },
            //退回
            backSubmit() {
                // this.multipleSelection1 //
                // this.multipleSelection2
                // this.multipleSelection3
                let params = []
                this.multipleSelection.forEach(item=>{
                    params.push(item.dwid)
                })
                // backList(params).then(res=>{
                //     if(res.infoCode && res.infoCode == 200){
                //         this.$message.success('退回成功')
                //     }else{
                //         this.$message.error('退回失败')
                //     }
                // }).catch()
            },
            //导出
            exportSubmit(){
                let self = this
                let tbnd = this.formInline.tbnd
                this.jd = this.nj.substring(5,6)
                let timeArr = this.nowTime.split('-')
                let nowY = timeArr[0]
                let nowJ = Math.floor(timeArr[1]/3) + 1
                // if(nowY == tbnd.split('-')[0] && nowJ == (Math.floor(tbnd.split('-')[1]/3))){
                //     tbnd = this.nowTime
                // }
                let reportName = '粮食和物资储备行政管理部门'+this.tbnd+'第'+this.jd+'季度执法督查机构、人员和经费落实情况统计表.xls'
                let data = {
                    tbnd
                };
                exportJfls(data)
                    .then(actionMethod)
                    .catch(actionMethod);
                function actionMethod(response) {
                    if(response.size == 66 || response.size == 82){
                        self.$message.error("网络异常，导出失败")
                        return
                    }
                    let blob = new Blob([response],{type: 'application/vnd.ms-excel'});

                    if (!!window.ActiveXObject || "ActiveXObject" in window) {
                        window.navigator.msSaveOrOpenBlob(blob, reportName);
                    } else {
                        const link = document.createElement('a');
                        link.style.display = 'none';
                        link.href = URL.createObjectURL(blob);
                        link.setAttribute('download', reportName);
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }
                }
            },
            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>
