<template>
  <div class="content-main">
	<el-tabs v-model="activeName" @tab-click="handletabClick">
		<el-tab-pane label="进销存汇总" name="jxc"></el-tab-pane>
		<el-tab-pane label="补贴汇总" name="bt"></el-tab-pane>
	</el-tabs>
    <el-row class="search-bm-boder">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <!-- <el-form-item label="申报单位:">
          <el-input clearable v-model="formInline.sskdmc" placeholder="请输入"></el-input>
        </el-form-item> -->
		<el-form-item label="汇总期数:">
			<el-date-picker
			      v-model="formInline.period"
			      type="month"
			      placeholder="选择期数"
			      format="yyyy年MM月"
			      value-format="yyyy年MM月">
			    </el-date-picker>
          <!-- <el-input clearable v-model="formInline.period" placeholder="请输入"></el-input> -->
        </el-form-item>
        <el-form-item :label="activeName=='bt' ? '审批日期:' : '汇总日期:'">
          <el-date-picker
          @change="ChangereportDate"
			v-model="formInline.reportDate"
			type="daterange"
			range-separator="至"
			start-placeholder="开始日期"
			end-placeholder="结束日期"
			format="yyyy-MM-dd"
			value-format="yyyy-MM-dd">
		  </el-date-picker>
        </el-form-item>
		<!-- <el-form-item label="审批日期:">
		  <el-date-picker
		  @change="ChangereportDate"
			v-model="formInline.reportDate"
			type="daterange"
			range-separator="至"
			start-placeholder="开始日期"
			end-placeholder="结束日期"
			format="yyyy-MM-dd"
			value-format="yyyy-MM-dd">
		  </el-date-picker>
		</el-form-item> -->
		<el-form-item label="状态:">
		   <el-select
		   v-if="activeName == 'bt'"
		     clearable
		     class="inputCompatible"
		     v-model="formInline.status"
		     placeholder="请选择"
		   >
		     <el-option
		       v-for="item in statusOptionsbt"
		       :key="item.value"
		       :label="item.label"
		       :value="item.value"
		     ></el-option>
		   </el-select>
		   <el-select
		   v-else
		     clearable
		     class="inputCompatible"
		     v-model="formInline.status"
		     placeholder="请选择"
		   >
		     <el-option
		       v-for="item in statusOptions"
		       :key="item.value"
		       :label="item.label"
		       :value="item.value"
		     ></el-option>
		   </el-select>
		</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="getList"
              >查询</el-button
            >
            <el-button
              type="primary"
              @click="
                doSearch();
              "
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </el-row>
	<el-row
	  class="addBtnBg"
	  style="
	    background: rgb(237, 245, 253);
	    line-height: 50px;
	    margin-bottom: 10px;
	  "
	>
	  <el-button
	    type="primary"
	    class="iconfont"
	    size="small"
	    @click="addSubmit(undefined,'xg')"
	    >生成汇总表</el-button
	  >
	</el-row>
    <el-row>
      <el-table
        :data="tableData"
        stripe
        v-loading="listLoading"
        border
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" label="序号" width="70">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
		<!-- <el-table-column
		  align="center"
		  prop="sskdmc"
		  label="申报单位"
		  show-overflow-tooltip
		>
		</el-table-column> -->
        <el-table-column
          align="center"
          prop="period"
          label="汇总期数"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="reportDate"
		  :label="activeName == 'bt' ? '审批日期' : '汇总日期'"
          show-overflow-tooltip
        >
		<template slot-scope="scope">
		  <span>{{activeName == 'bt' ? scope.row.leaderDate : scope.row.recevieDate}}</span>
		</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="initial"
          :label="activeName == 'bt' ? '小麦' : '期初库存'"
          show-overflow-tooltip
        >
		<template slot-scope="scope">
		  <span>{{activeName == 'bt' ? scope.row.wheat : scope.row.initial}}</span>
		</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="incomeTotal"
          :label="activeName == 'bt' ? '稻谷' : '收入'"
          show-overflow-tooltip
        >
		<template slot-scope="scope">
		  <span>{{activeName == 'bt' ? scope.row.paddy : scope.row.incomeTotal}}</span>
		</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop=""
          :label="activeName == 'bt' ? '大米' : '支出'"
          show-overflow-tooltip
        >
		<template slot-scope="scope">
		  <span>{{activeName == 'bt' ? scope.row.rice : scope.row.expenseTotal}}</span>
		</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="ending"
          :label="activeName == 'bt' ? '面粉' :'期末库存'"
          show-overflow-tooltip
        >
		<template slot-scope="scope">
		  <span>{{activeName == 'bt' ? scope.row.flour : scope.row.ending}}</span>
		</template>
        </el-table-column>
		<el-table-column
		v-if="activeName == 'bt'"
		  align="center"
		  prop="eatOil"
		  label="食用油"
		  show-overflow-tooltip
		>
		</el-table-column>
        <el-table-column
          align="center"
          prop="statusName"
          label="状态"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column align="center" label="操作" width="180px">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                @click="addSubmit(scope.row,'ck')"
                style="float: left"
                type="primary"
                size="small"
                class="operate_show_btn iconfont iconchakan2"
              ></el-button>
            </el-tooltip>
			<el-tooltip
			v-if="(scope.row.status == '91000290' || scope.row.status == '91000301') && buttonNameList.indexOf('修改') > -1"
			  class="item"
			  effect="light"
			  content="修改"
			  placement="top"
			>
			  <el-button
			    @click="addSubmit(scope.row,'xg')"
			    style="float: left"
			    type="primary"
			    size="small"
			    class="operate_edit_btn iconfont iconxiugai1"
			  ></el-button>
			</el-tooltip>
            <el-tooltip
			v-if="(scope.row.status == '91000300' || scope.row.status == '91000401') && buttonNameList.indexOf('复核') > -1"
              class="item"
              effect="light"
              content="复核"
              placement="top"
            >
              <el-button
                @click="addSubmit(scope.row,'fh')"
                style="float: left"
                type="primary"
                size="small"
                class="operate_edit_btn iconfont iconshenhe"
              ></el-button>
            </el-tooltip>
			<el-tooltip
			v-if="(scope.row.status == '91000400' || scope.row.status =='91000501') && buttonNameList.indexOf('财务审核') > -1"
			  class="item"
			  effect="light"
			  content="财务审核"
			  placement="top"
			>
			  <el-button
			    @click="addSubmit(scope.row,'cwsh')"
			    style="float: left"
			    type="primary"
			    size="small"
			    class="operate_edit_btn iconfont iconshenhe"
			  ></el-button>
			</el-tooltip>
			<el-tooltip
			v-if="scope.row.status == '91000500' && buttonNameList.indexOf('负责人审核') > -1"
			  class="item"
			  effect="light"
			  content="负责人审核"
			  placement="top"
			>
			  <el-button
			    @click="addSubmit(scope.row,'fzrsh')"
			    style="float: left"
			    type="primary"
			    size="small"
			    class="operate_edit_btn iconfont iconshenhe"
			  ></el-button>
			</el-tooltip>
           <el-tooltip
		   v-if="scope.$index == 0 && buttonNameList.indexOf('删除') > -1"
             class="item"
             effect="light"
             content="删除"
             placement="top"
           >
             <el-button
               @click="deleteSubmit(scope.row)"
               style="float: left"
               type="primary"
               size="small"
               class="operate_delete_btn iconfont iconshanchu"
             ></el-button>
           </el-tooltip>
		   <el-tooltip
		     class="item"
		     effect="light"
		     content="导出"
		     placement="top"
		   >
		     <el-button
		       @click="addSubmit(scope.row,'ck')"
		       style="float: left"
		       type="primary"
		       size="small"
		       class="operate_edit_btn iconfont icondaochu11"
		     ></el-button>
		   </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
	<el-row>
	  <el-pagination
	    @size-change="handleSizeChange"
	    @current-change="handleCurrentChange"
	    :current-page.sync="pageForm.pageNum"
	    :page-sizes="[10, 20, 30, 40]"
	    :page-size="pageForm.pageSize"
	    layout="total, sizes, prev, pager, next, jumper"
	    :total="pageForm.total"
	  ></el-pagination>
	</el-row>
  </div>
</template>

<script>
import {
  purchaseStockSummaryPage,storageSubsidySummaryPage,purchaseStockSummaryDelete,storageSummaryDelete
} from "@/api/cwbb/index";
import { authorityControl } from "@/api/login/login.js";

export default {
  data() {
    return {
		activeName:'jxc',
		statusOptionsbt:[
			{
			    label:'待提交',
			    value:'91000290'
			},
			{
			    label:'待复核',
			    value:'91000300'
			},
			{
			    label:'复核驳回',
			    value:'91000301'
			},
			{
			    label:'待财务领导批示',
			    value:'91000400'
			},
			{
			    label:'财务领导驳回',
			    value:'91000401'
			},
			{
			    label:'待单位负责人批示',
			    value:'91000500'
			},
			{
			    label:'单位负责人驳回',
			    value:'91000501'
			},
			{
			    label:'审批完成',
			    value:'91000600'
			}
		],
		statusOptions:[
			{
			    label:'待提交',
			    value:'91000290'
			},
			{
			    label:'待复核',
			    value:'91000300'
			},
			{
			    label:'复核驳回',
			    value:'91000301'
			},
			{
			    label:'审批完成',
			    value:'91000600'
			}
		],
      formInline: {
        sskdmc:'',
        period:'',
		reportDate:[],
		status:'',
		dataType:0
      },
	  pageForm: {
	    pageNum: 1,
	    pageSize: 10,
	    total: 0,
	  },
      listLoading: false,
      tableData: [],
	  buttonNameList:[],
    };
  },
  created() {
	  this.getButtonList(
	    sessionStorage.getItem("resourceCode"),
	    sessionStorage.getItem("userCode")
	  );
	  // console.log(`${year}-${month}-${date}`); // 2022-5-7
	  if (this.$route.query.active == "bt") {
	    this.activeName = "bt";
	    // this.bhWidth='250'
	  } else {
	    this.activeName = "jxc";
	    // this.bhWidth='250'
	  }
    this.getList("start");
  },
  methods: {
	  getButtonList(menuCode, userCode) {
	    let codeData = {
	      resourceCode: menuCode,
	      userId: userCode,
	    };
	    authorityControl(codeData).then((res) => {
	      let buttonNameList = [];
	      res.data.forEach((item) => {
	        this.buttonNameList.push(item.resourceName);
	      });
	    });
	  },
	  ChangereportDate(val){
	    console.log(val,'ppiu88=========')
	    this.formInline.startDate=val[0]
	    this.formInline.endDate=val[1]
	  },
	  handletabClick(tab, event) {
		  // const now = new Date();
		  // const year = now.getFullYear(); // 年
		  // // const month = now.getMonth()+1
		  // const month = now.getMonth() <9 ? '0'+(now.getMonth()+ 1) : now.getMonth()+ 1; // 月
	      (this.formInline = {
			  sskdmc:'',
	        reporter:'',
	        period:'',
			// period:`${year}年${month}月`,
	        reportDate:'',
	        status:'',
			dataType:0,
			startDate:'',
			endDate:"",
	      });
	    this.activeName = tab.name;
	    this.getList();
	  },
    doSearch() {
		// const now = new Date();
		// const year = now.getFullYear(); // 年
		// // const month = now.getMonth()+1
		// const month = now.getMonth() <9 ? '0'+(now.getMonth()+ 1) : now.getMonth()+ 1; // 月
		// this.formInline.period=`${year}年${month}月`
		this.formInline= {
		  sskdmc:'',
		  // period:`${year}年${month}月`,
		  period:'',
		reportDate:[],
		status:'',
		dataType:'0',
		startDate:'',
		endDate:"",
		},
      this.getList();
    },
    //获取列表
    getList(type) {
		 this.listLoading=true
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(this.formInline));
        sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
      } else {
		  
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData")) && JSON.parse(sessionStorage.getItem("searchData")).period) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          this.formInline = JSON.parse(sessionStorage.getItem("searchData"));
          this.pageForm = JSON.parse(sessionStorage.getItem("pageData"));
        } else {
			// const now = new Date();
			// const year = now.getFullYear(); // 年
			// // const month = now.getMonth()+1
			// const month = now.getMonth() <9 ? '0'+(now.getMonth()+ 1) : now.getMonth()+ 1; // 月
			// this.formInline.period=`${year}年${month}月`
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(this.formInline));
          sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
        }
      }
      var data={
       ...this.formInline,
	   ...this.pageForm
      }
	  if(this.activeName == 'jxc'){
		 purchaseStockSummaryPage(data).then((res) => {
		   if(res.infoCode == 200){
		    this.tableData = res.data.data.list
			this.pageForm.total = res.data.data.total;
			 this.listLoading=false
		   }else{
			   this.$message.error(res.msg);
			    this.listLoading=false
		   }
		 }); 
	  }else{
		  storageSubsidySummaryPage(data).then((res) => {
		    if(res.infoCode == 200){
		     this.tableData = res.data.list
			 this.pageForm.total = res.data.total;
			  this.listLoading=false
		    }else{
		        this.$message.error(res.msg);
				 this.listLoading=false
		    }
		  });
	  }
      
    },
    //新增
    addSubmit(row,type) {
		
		if(this.activeName == 'jxc'){
			this.$router.push({
			  name: "storageFeeSummaryDetail",
			  query: {
			    id:row ? row.id : undefined,
			    type:type ? type : undefined,
				period:row ? row.period :undefined
			  },
			});
		}else{
			this.$router.push({
			  name: "storageFeeSummarybtDetail",
			  query: {
			    id:row ? row.id : undefined,
			    type:type ? type : undefined,
				period:row ? row.period :undefined
			  },
			});
		}
      
    },
	deleteSubmit(row){
		var _this=this
		console.log(this.activeName,'yy77777')
		if(this.activeName =='jxc'){
			console.log('900000000000========')
			this.$confirm("是否确认删除？", "提示", {
			  confirmButtonText: "确定",
			  cancelButtonText: "取消",
			  type: "warning",
			}).then(() => {
				
				var data={
					id:row.id
				}
			  purchaseStockSummaryDelete(data)
			    .then((response) => {
			      if (response.infoCode == 200) {
			        _this.getList();
					this.activeName ='jxc'
			        this.$message({
			          showClose: true,
			          message: "删除成功！",
			          type: "success",
			        });
			      }
			    })
			    .catch(function (error) {
			      console.log(error);
			    });
			});
		}else{
			console.log('666666666========')
			this.$confirm("是否确认删除？", "提示", {
			  confirmButtonText: "确定",
			  cancelButtonText: "取消",
			  type: "warning",
			}).then(() => {
				console.log('666666666========')
				var data={
					id:row.id
				}
			  storageSummaryDelete(data)
			    .then((response) => {
			      if (response.infoCode == 200) {
			        _this.getList();
					this.activeName ='bt'
			        this.$message({
			          showClose: true,
			          message: "删除成功！",
			          type: "success",
			        });
			      }
			    })
			    .catch(function (error) {
			      console.log(error);
			    });
			});
		}
		
	},
	handleSizeChange(val) {
	  this.pageForm.pageSize = val;
	  this.getList();
	},
	handleCurrentChange(val) {
	  this.pageForm.pageNum = val;
	  this.getList();
	},
  },
};
</script>
<style scoped>
.h3 {
  text-align: center;
  font-size: 1em;
  line-height: 2.5;
  color: #fff;
  background-color: #49a2fa;
  margin-bottom: 20px;
}
</style>
