<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>
                </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;">粮食行政管理部门成立行政执法队情况统计表</h2>
        </el-row>
        <el-row id="pdfId">
            <el-row style="padding:10px"> 填报单位: {{ resJcdwmc }} </el-row>
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    border
                    v-loading="loading"
            >
                <el-table-column label="" align="center">
                    <el-table-column prop="name" 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.xsxzhj}}</span>
                            <el-input v-model="scope.row.xsxzhj" 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.xsxzhj1}}</span>
                            <el-input v-model="scope.row.xsxzhj1" 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 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>
        </el-row>

    </div>
</template>

<script>
    import htmlToPdf from '@/utils/htmlToPdf'
    import {inspectionUnit, getList, updateList, backList} from "@/api/jdjcbb/paracticable"
    export default {
        name: 'index',
        data() {
            return {
                resJcdwmc: null,
                isSj: true,
                multipleSelection: [],
                formInline: {
                    tbnd: null,
                    tblx: 0,
                    dwid: '',
                    dwmc: '',
                },
                tbnd: null,
                options:[],
                loading: false,
                pageForm: {
                    pageNum: 1,
                    pageSize: 10,
                    total: 0
                },
                pickerOptions:{
                    disabledDate(time) {
                        return time.getTime() > Date.now()-8.64e7;
                    },
                },
                tableData: [
                    {
                        name: '省级'
                    },
                    {
                        name: '地市级'
                    },
                    {
                        name: '县级'
                    },
                ],
                tree: []
            }
        },
        created() {
            this.formInline.tbnd = this.formatDateTime(new Date())
            this.tbnd = this.formInline.tbnd
            // 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: {
            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.options.unshift({
                        value: '',
                        label: '合计',
                    });
                }).catch()
            },
            //重置
            resetInfo(){
                this.pageForm.pageNum = 1;
                this.pageForm.pageSize = 10;
                this.formInline.tbnd = this.formatDateTime(new Date())
                // this.formInline.dwid = sessionStorage.getItem('orgCode')
                // this.formInline.dwmc = sessionStorage.getItem('orgName')
                this.formInline.dwid = ''
                this.formInline.dwmc = ''
                this.tbnd = this.formInline.tbnd
                this.getList()
            },
            //查询
            doSearch(){
                this.pageForm.pageNum = 1
                this.tbnd = this.formInline.tbnd ? this.formInline.tbnd : ''
                this.getList()
            },
            //获取列表
            getList() {
                if(this.formInline.dwid){
                    this.options.forEach(item=>{
                        if(item.value == this.formInline.dwid){
                            this.formInline.dwmc = item.label
                            // this.resJcdwmc = item.label
                        }
                    })
                }
                let data = {
                    pageNum: this.pageForm.pageNum,
                    pageSize: this.pageForm.pageSize,
                    tbnd: this.formInline.tbnd,
                    tblx: '0',
                    dwid: this.formInline.dwid,
                    dwmc: this.formInline.dwmc,
                };
                getList(data)
                    .then(res => {
                        if (res.infoCode == 200) {
                            this.tableData = res.data.list;
                            this.$set(this.pageForm,"total", Number(res.data.total))
                        } 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(){

            },
            handleSizeChange(val) {
                this.pageForm.pageSize = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.pageForm.pageNum = val
                this.getList()
            },
            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 + '';
            },
        }
    }
</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>
