<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.nshj}}</span>
                        <el-input v-model="scope.row.nshj" 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.xzzfhj}}</span>
                            <el-input v-model="scope.row.xzzfhj" 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="xsxzhj"
                            label="合计"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <span v-if="isSj">{{scope.row.xzjghj}}</span>
                            <el-input v-model="scope.row.xzjghj" 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.zzjgsl}}</span>
                            <el-input v-model="scope.row.zzjgsl" 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.fzzjgsl}}</span>
                            <el-input v-model="scope.row.fzzjgsl" 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, getZfdList, 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,
                },
                tbnd: null,
                options:[],
                loading: false,
                pickerOptions:{
                    disabledDate(time) {
                        return time.getTime() > Date.now()-8.64e7;
                    },
                },
                tableData: [],
                tree: [],
                jd: null,
                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,
                            });
                            // this.tableData.push({
                            //     name: item.orgName
                            // })
                        })
                    }else{
                        // this.tableData.push({
                        //     name: res.data.orgName
                        // })
                    }
                    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
                };
                getZfdList(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,
                            }
                            this.tableData.forEach((item,index)=>{
                                if(item.sybz || item.xzbz) {item.xzzfhj = item.sybz + item.xzbz}
                                if(item.fzzjgsl || item.zzjgsl) {item.xzjghj = item.fzzjgsl + item.zzjgsl}
                                if(item.xzjghj || item.xzzfhj) {item.nshj = item.xzjghj + item.xzzfhj}
                                if(index != 0){
                                    item.sybz?obj.sybz += Number(item.sybz):null
                                    item.xzbz?obj.xzbz += Number(item.xzbz):null
                                    item.xzzfhj?obj.xzzfhj += Number(item.xzzfhj):null
                                    item.fzzjgsl?obj.fzzjgsl += Number(item.fzzjgsl):null
                                    item.zzjgsl?obj.zzjgsl += Number(item.zzjgsl):null
                                    item.xzjghj?obj.xzjghj += Number(item.xzjghj):null
                                    item.nshj?obj.nshj += Number(item.nshj):null
                                    item.orgname = item.orgname.split('区')[0] + '区'
                                }
                            })
                            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'
                console.log(reportName,'====reportName')
                let data = {
                    tbnd
                };
                exportZfd(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>
