<template>
    <div class="content-main fumigationRecordPage">
        <el-form ref="formInline" :model="formInline" class="search-bm-boder" inline>
            <el-row>
                <el-form-item label="粮食品种:" label-width="80px">
                    <el-select v-model="formInline.breedName" clearable placeholder="请选择" style="width:180px;">
                        <el-option
                                v-for="item in pzOption"
                                :key="item.dictCode"
                                :label="item.dictName"
                                :value="item.dictCode"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="仓房名称:" label-width="80px">
                    <el-input v-model="formInline.houseName" clearable style="width:180px;"></el-input>
                </el-form-item>
                <el-form-item label="熏蒸方式:" label-width="80px">
                    <el-select v-model="formInline.fumigationType" clearable style="width:180px;">
                        <el-option
                                v-for="item in xzfsOption"
                                :key="item.dictCode"
                                :label="item.dictName"
                                :value="item.dictCode"
                        ></el-option>
                    </el-select>
                </el-form-item>


                <el-form-item>
                    <div class="search-button">
                        <span class="search-line"></span>
                        <div class="search-select" @click="searchShowMoreBool = !searchShowMoreBool">
                            更多条件
                            <i class="el-icon-arrow-up" v-show="searchShowMoreBool"></i>
                            <i class="el-icon-arrow-down" v-show="!searchShowMoreBool"></i>
                        </div>
                        <el-button type="primary" @click="queryFun" size="small" class="iconfont iconchaxun">查询</el-button>
                        <el-button
                                type="primary"
                                @click="resetFun"
                                size="small"
                                class="iconfont iconzhongzhi"
                        >重置</el-button>
                    </div>
                </el-form-item>
            </el-row>
            <el-row v-show="searchShowMoreBool">
<!--                <el-form-item label="药剂:" label-width="80px">-->
<!--                    <el-input v-model="formInline.drugName" clearable style="width:180px;"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="状态:" label-width="80px">
                    <el-select v-model="formInline.dataStatus" clearable style="width:180px;">
                        <el-option
                                v-for="item in dataStatusList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-row>
        </el-form>
        <el-row>
            <el-col :span="24">
                <el-table
                        :data="tableList"
                        v-loading="loading"
                        border
                        stripe
                        highlight-current-row
                >
                    <el-table-column align="center" label="序号" width="60">
                        <template slot-scope="scope">
                            <span>{{ scope.$index + 1 }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="planId" label="熏蒸编号" align="center" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="orgName" label="粮库名称" align="center" :show-overflow-tooltip="true" width="260px"></el-table-column>
                    <el-table-column prop="houseName" label="仓号" align="center" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="breedName" label="粮食品种" align="center" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{getDictName('pzOption',scope.row.breedName)}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="fumigationType" label="熏蒸方式" align="center" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{getDictName('xzfsOption',scope.row.fumigationType)}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="drugName" label="药剂名称" align="center" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{getDictName('yjOption',scope.row.drugName)}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="作业时间" align="center" :show-overflow-tooltip="true">
                      <template slot-scope="scope">
                        {{formatDateTimes(new Date(scope.row.createTime))}}
                      </template>
                    </el-table-column>
                    <el-table-column prop="dataStatus" label="状态" align="center" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span v-if="scope.row.dataStatus == 0">待提交</span>
                            <span v-if="scope.row.dataStatus == 1">企业待审核</span>
                            <span v-if="scope.row.dataStatus == 2">企业待盖章</span>
                            <span v-if="scope.row.dataStatus == 3">购销公司待审核</span>
                            <span v-if="scope.row.dataStatus == 4">购销公司待盖章</span>
                            <span v-if="scope.row.dataStatus == 5">区粮办待审核</span>
                            <span v-if="scope.row.dataStatus == 6">区粮办待盖章</span>
                            <span v-if="scope.row.dataStatus == 7">已备案</span>
                            <span v-if="scope.row.dataStatus == 9">资料上传</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="120">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="light" content="查看" placement="top">
                                <el-button
                                        type="primary"
                                        size="small"
                                        class="iconfont operate_show_btn iconchakan2"
                                        @click.stop="lookFun(scope.row)"
                                ></el-button>
                            </el-tooltip>
                            <el-tooltip
                                    class="item"
                                    effect="light"
                                    content="审核"
                                    placement="top"
                                    v-if="buttonNameList.indexOf('审核') > -1 && scope.row.dataStatus == 5"
                            >
                                <el-button
                                        type="primary"
                                        size="small"
                                        class="iconfont operate_edit_btn iconshenhe"
                                        @click.stop="recordsFun(scope.row)"
                                >
                                </el-button>
                            </el-tooltip>
                            <el-tooltip
                                    class="item"
                                    effect="light"
                                    content="盖章"
                                    placement="top"
                                    v-if="buttonNameList.indexOf('盖章') > -1 && scope.row.dataStatus == 6"
                            >
                                <el-button
                                        type="primary"
                                        size="small"
                                        class="operate_edit_btn iconfont iconqianzhang"
                                        @click.stop="gaizhangFn(scope.row)"
                                >
                                </el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pag-right clearFix">
                    <el-pagination
                            class="right"
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page.sync="pageInfo.pageNum"
                            :page-sizes="[10, 20, 30, 40]"
                            :page-size="pageInfo.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="pageInfo.total"
                    ></el-pagination>
                </div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
    import {
        getList,getDictionaryList
    } from "@/api/grainReserves/zygl/index";
    import * as validate from "@/directives/validate";
    import {getParameter} from "@/utils/index";
    import { authorityControl } from "@/api/menuList";

    export default {
        data() {
            return {
                searchShowMoreBool: false,
                zts: [5,6,7],
                formInline: {
                    breedName: null,
                    orgName: null,
                    houseName: null,
                    fumigationType: null,
                    zts: [5,6,7]
                },
                breedNameList: [
                    {
                        value: '0',
                        label: '玉米',
                    },
                    {
                        value: '1',
                        label: '小麦',
                    },
                    {
                        value: '2',
                        label: '大豆',
                    },
                    {
                        value: '3',
                        label: '稻谷',
                    },
                    {
                        value: '4',
                        label: '其他',
                    },
                ],
                fumigationTypeList: [
                    {
                        value: '0',
                        label: '常规熏蒸',
                    },
                    {
                        value: '1',
                        label: '整仓环流熏蒸',
                    },
                    {
                        value: '2',
                        label: '膜下环流熏蒸',
                    },
                    {
                        value: '3',
                        label: '其他熏蒸方式',
                    },
                ],
                curId: "", //该条数据的id
                typeZgData: [],
                paginationShow: true,
                tableList: [],
                loading: false,
                pageInfo: {
                    pageNum: 1,
                    pageSize: 10,
                    total: 0
                },
                buttonNameList: [],
                dataStatusList: [
                    // {
                    //     value: 0,
                    //     label: '待提交',
                    // },
                    // {
                    //     value: 1,
                    //     label: '企业待审核',
                    // },
                    // {
                    //     value: 2,
                    //     label: '企业待盖章',
                    // },
                    // {
                    //     value: 3,
                    //     label: '购销公司待审核',
                    // },
                    // {
                    //     value: 4,
                    //     label: '购销公司待盖章',
                    // },
                    {
                        value: 5,
                        label: '区粮办待审核',
                    },
                    {
                        value: 6,
                        label: '区粮办待盖章',
                    },
                    {
                        value: 7,
                        label: '已备案',
                    },
                    // {
                    //     value: 11,
                    //     label: '已退回',
                    // },
                ],
                pzOption: [],
                yjOption: [],
                xzfsOption: [],
            };
        },
        watch: {
            "formInline.dataStatus" (newV,oldV) {
                if(newV === 0 || newV){
                    this.formInline.zts = [newV]
                }else{
                    this.formInline.zts = this.zts
                }
            },
        },
        created() {
            if(sessionStorage.getItem('orgCode').length == 5){
                this.zts = [7]
                this.formInline.zts = [7]
            }else{
                this.zts = [5,6,7]
                this.formInline.zts = [5,6,7]
            }
            this.getDictionaryList()
            this.getButtonList();
        },
        methods: {
            //获取按钮
            getButtonList() {
                let codeData = {
                    resourceCode: sessionStorage.getItem("resourceCode"),
                    userId: sessionStorage.getItem("userCode")
                };
                authorityControl(codeData).then(res => {
                    res.data.forEach(item => {
                        this.buttonNameList.push(item.resourceName);
                    });
                    this.getList("start");
                });
            },
            getDictName(key,value) {
                let name = ''
                if(value && this[key].length){
                    this[key].forEach(item=>{
                        value.split(',').forEach(val=>{
                            if(item.dictCode == val){
                                name += item.dictName + ','
                            }
                        })
                    })
                }
                if(name){
                    return name.substring(0,name.length-1)
                }else{
                    return ''
                }
            },
            getDictionaryList(){
                //品种
                getDictionaryList({groupId: 'KJPZ'}).then(res=>{
                    this.pzOption = res.data
                }).catch()
                //药剂
                getDictionaryList({groupId: 'KJYJ'}).then(res=>{
                    this.yjOption = res.data
                }).catch()
                //熏蒸方式
                getDictionaryList({groupId: 'KJXZ'}).then(res=>{
                    this.xzfsOption = res.data
                }).catch()
            },
            //获取列表
            getList(type) {

                let data = {
                    pageNum: this.pageInfo.pageNum,
                    pageSize: this.pageInfo.pageSize,
                    breedName: this.formInline.breedName, //品种名称
                    dataStatus: '', //数据状态，1、已备案。0、待备案
                    fumigationType: this.formInline.fumigationType, //熏蒸方式
                    houseName: this.formInline.houseName, //仓房名称
                    id: null,
                    majorInsectPests: null, //主要害虫
                    orgCode: null, //组织名称
                    orgName: this.formInline.orgName, //组织名称
                    token: sessionStorage.getItem('token'),
                    zts: this.formInline.zts
                }
                if (type != "start") {//非初始化调用：查询、分页
                    sessionStorage.setItem('searchData', JSON.stringify(data))
                } else {//初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
                    if(JSON.parse(sessionStorage.getItem('searchData')) && sessionStorage.getItem('searchData') != "{}"){//详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
                        let searchData = JSON.parse(sessionStorage.getItem('searchData'))
                        this.formInline = getParameter(searchData,["breedName","fumigationType","houseName","orgName"])
                        this.pageInfo = getParameter(searchData,["pageNum","pageSize"])
                    }else {//第一次进去此菜单时，保存上方默认的data
                        sessionStorage.setItem('searchData', JSON.stringify(data))
                    }
                }

                console.log('this.formInline',this.formInline)
                console.log('JSON.parse(sessionStorage.getItem(\'searchData\'))',JSON.parse(sessionStorage.getItem('searchData')))
                getList(JSON.parse(sessionStorage.getItem('searchData'))).then((res)=>{
                    // console.log("res",res)
                    if(res.infoCode && res.infoCode == 200){
                        this.tableList = res.data.items;
                        this.$set(this.pageInfo,"total", res.data.total)
                    }
                }).catch(error=>{
                })
            },
            //查询
            queryFun() {
                //查询
                this.handleCurrentChange(1);
            },
            //重置
            resetFun() {
                //重置
                this.formInline = {
                    zts: this.zts
                }
                this.pageInfo = {
                    pageNum: 1,
                    pageSize: 10,
                    total: 0
                }
                this.queryFun();
            },
            //查看
            lookFun(row){
                this.$router.push({
                    path: "recordsDetails",
                    query: {
                        id: row.id,
                        type: 'look'
                    }
                });
            },
            //备案
            recordsFun(row) {
                this.$router.push({
                    path: "recordsAudit",
                    query: {
                        id: row.id,
                        type: 'audit'
                    }
                });
            },
            gaizhangFn(row) {
                this.$router.push({
                    path: "recordsAudit",
                    query: {
                        id: row.id,
                        type: 'seal'
                    }
                });
            },
            handleSizeChange(val) {
                //条数改变
                this.pageInfo.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val) {
                //页数改变
                this.pageInfo.pageNum = val;
                this.getList();
            },
            formatDateTimes(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 + "-" + d + " " + h + ":" + minute + ":" + second;
            },
        }
    };
</script>
<style lang="scss">
    .fumigationRecordPage {
        .el-select {
            width: 100%;
        }
        .itemStyle .el-form-item__label {
            line-height: 20px;
        }
        .el-dialog__body {
            padding-bottom: 0px;
        }
        .curStyle .el-form-item__label {
            line-height: 20px;
        }
    }
</style>
