<template>
  <div class="content-main">
    <el-row class="search-bm-boder">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
		<el-form-item label="汇总期数:">
		  <el-date-picker
		      v-model="formInline.month"
		      type="month"
		      placeholder="选择汇总期数"
		      format="yyyy-MM"
		      value-format="yyyy-MM">
		    </el-date-picker>
		</el-form-item>
		<el-form-item label="汇总日期:">
          <el-date-picker
              v-model="formInline.summaryDay"
              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>
          <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="
                formInline = { };
                doSearch();
              "
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row></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,'sc')"
	    >生成汇总表</el-button
	  >
	</el-row>
	<div class="flexBox">
	 <div style="flex: 1; text-align: right">
		<span>单位：吨</span>
	  </div>
	</div>
    <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="hzqs"
          label="汇总期数"
          show-overflow-tooltip
        >
        </el-table-column>
		<el-table-column
		  align="center"
		  prop="summarydate"
		  label="汇总日期"
		  show-overflow-tooltip
		>
		</el-table-column>
        <el-table-column
          align="center"
          prop="qmkc"
          label="期末库存汇总"
          show-overflow-tooltip
        >
		<template slot-scope="scope">
			{{String(scope.row.qmkc).indexOf(".") > -1
			  ? Number(scope.row.qmkc).toLocaleString(undefined, {
				  minimumFractionDigits: 3,
				  maximumFractionDigits: 3,
				})
			  : Number(scope.row.qmkc).toLocaleString()}}
		</template>
        </el-table-column>
		<el-table-column
		  align="center"
		  prop="feiqmkc"
		  label="期末库存汇总(非调节)"
		  show-overflow-tooltip
		>
		<template slot-scope="scope">
			{{String(scope.row.feiqmkc).indexOf(".") > -1
			  ? Number(scope.row.feiqmkc).toLocaleString(undefined, {
				  minimumFractionDigits: 3,
				  maximumFractionDigits: 3,
				})
			  : Number(scope.row.feiqmkc).toLocaleString()}}
		</template>
		</el-table-column>
		<el-table-column
		  align="center"
		  prop="tiaoqmkc"
		  label="期末库存汇总(调节)"
		  show-overflow-tooltip
		>
		<template slot-scope="scope">
			{{String(scope.row.tiaoqmkc).indexOf(".") > -1
			  ? Number(scope.row.tiaoqmkc).toLocaleString(undefined, {
				  minimumFractionDigits: 3,
				  maximumFractionDigits: 3,
				})
			  : Number(scope.row.tiaoqmkc).toLocaleString()}}
		</template>
		</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.$index == 0"
		     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,'dc')"
		       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 {feiDivideDetails,delDivideDetails} from '@/api/productionYear/index'
import { authorityControl } from "@/api/login/login.js";
  import {dateFormat} from '@/utils/index'
export default {
  data() {
    return {
      formInline: {
        month:'',
		summaryDay:[],
      },
      listLoading: false,
      tableData: [
        {
          lhnd:'2019'
        }
      ],
      // pageForm: {
      //   pageNum: 1,
      //   pageSize: 10,
      //   total: 0,
      // },
	  buttonNameList:[],
    };
  },
  created() {
	this.getButtonList(
	  sessionStorage.getItem("resourceCode"),
	  sessionStorage.getItem("userCode")
	);
    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);
	      });
	    });
	  },
    doSearch() {
      // this.pageForm.pageNum = 1;
      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"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          this.formInline = JSON.parse(sessionStorage.getItem("searchData"));
          // this.pageForm = JSON.parse(sessionStorage.getItem("pageData"));
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(this.formInline));
          // sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
        }
      }
      var data={
        // ...this.pageForm,
       // ...this.formInline,
	   endDate:this.formInline.summaryDay ? this.formInline.summaryDay[1] : '' ,
	   startDate:this.formInline.summaryDay ? this.formInline.summaryDay[0] : '',
	   hzqs:this.formInline.month
	   // type:'budget'
      }
      feiDivideDetails(data).then((res) => {
        if(res.infoCode == 200){
			
         this.tableData = res.data;
		 this.tableData.forEach((item) =>{
			item.summarydate=dateFormat(item.summarydate)
		 })
		 if(this.tableData.length >0){
		   let str =this.tableData[0].hzqs
		   // str.indexOf('年')
		   var year = str.substring(0,4)
		   var month=str.substring(str.indexOf('-')+1,str.length)
		   console.log(month,'6644665-------')
		   if(month <12){
			   if(month <9){
				    month=parseInt(month)+1
					month='0'+month
				   // console.log(month,'iii')
				 // month='0'+parseInt(month)+1  
			   }else{
				  month=parseInt(month)+1
			   }
		     
		   }else{
		     year =parseInt(year)+1
		     month='01'
		   }
		   this.reportMonth=year+'-'+month.toString()
		 }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}月`
		   // this.reportMonth=`${year}-${month}`
		   this.reportMonth='2023-01'
		 }
         // this.pageForm.total = res.data.total;
         this.listLoading=false
        }else{
          this.$message.error(res.msg);
		  this.listLoading=false
        }
      });

    },
    //新增
    addSubmit(row,type,scspType) {
		this.$router.push({
		  name: "productionYearDetail",
		  query: {
		    id:row ? row.id : undefined,
		    type:type ? type : undefined,
			reportMonth:row ? row.hzqs : this.reportMonth
		  },
		});
    },
	deleteSubmit(row){
		console.log('666666666========')
		this.$confirm("是否确认删除？", "提示", {
		  confirmButtonText: "确定",
		  cancelButtonText: "取消",
		  type: "warning",
		}).then(() => {
			console.log('666666666========')
			var data={
				hzqs:row.hzqs
			}
		  delDivideDetails(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>
