<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex" v-if="!show">
        <criteria-paged :model="model" v-ref:paged :pager="false" @sort="sort">
          <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri >
            <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
              <div class="row">
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">场站名称</label>
                  <v-select
                    :value.sync="currentStation"
                    :options='$parent.$parent.stationName' placeholder='请选择'
                    @change="$parent.$parent.stationNameChange"
                    v-model="currentStation"
                    close-on-select ></v-select>
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button class="button_search button_spacing" @click="$parent.$parent.exportToExcel()">导出为Excel</button>

                  <!--<export-excel :data="$parent.$parent.getCondition" :footer="$parent.$parent.footer"-->
                  <!--:field="$parent.$parent.getfield" :header="$parent.$parent.other"-->
                  <!--:defaultselect="$parent.$parent.defaultexport"-->
                  <!--sqlurl="rs/logic/threeDayContrastExport" sql-name="chargeQuery" template-name='收费查询导出'-->

                  <!--:choose-col="true">-->
                  <!--</export-excel>-->
                </div>
              </div>
            </div>
          </criteria>
          <data-grid  :model="model" partial='list' class="list_area table_sy" >
            <template partial='head'>
              <tr>
                <th rowspan="2">{{this.currentStation}}</th>
                <th >09:00</th>
                <th >10:00</th>
                <th >11:00</th>
                <th >12:00</th>
                <th >13:00</th>
                <th >14:00</th>
                <th >15:00</th>
                <th >16:00</th>
                <th >17:00</th>
                <th >18:00</th>
                <th >19:00</th>
                <th >20:00</th>
                <th >21:00</th>
                <th >22:00</th>
                <th >23:00</th>
                <th >00:00</th>
                <th >01:00</th>
                <th >02:00</th>
                <th >03:00</th>
                <th >04:00</th>
                <th >05:00</th>
                <th >06:00</th>
                <th >07:00</th>
                <th >08:00</th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">
                <nobr>{{row.year_month_day}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data09}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data10}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data11}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data12}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data13}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data14}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data15}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data16}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data17}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data18}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data19}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data20}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data21}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data22}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data23}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data00}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data01}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data02}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data03}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data04}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data05}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data06}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data07}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.data08}}</nobr>
              </td>


            </template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
  import TabButton from "../../sale/common/TabButton";
  import Tabs from "../../sale/common/Tabs";
  import {addDate, toStandardDateString, toStandardTimeString} from "../../../Util";
  import {PagedList} from "vue-client";


  export default {
    name: "ThreeDayContrastManage",
    title:'三日对比量',
    components: {Tabs, TabButton},
    data(){
      return{
        currentStation:'',
        //下拉框
        stationName:[],
        model: new PagedList('api/af-revenue/sql/get3DayContrast',24,{stationName:'this.model.currentStation',beginDate:'this.model.startDate',endDate:'this.model.endDate'})
      }
    },
    ready(){
      // 初始化变量
      this.initVariable();
      //初始化场站名
      this.initStationName();
    },
    methods:{
      exportToExcel(){
        this.$resetpost('rs/logic/threeDayContrastExport',{raw_data:this.model.rows,stationName:this.currentStation})
      },
      reset(){
        //客户对日期的定义是非自然日，将自然日中的数据合并
        let data = this.model.rows;
        let length = data.length;
        for(let i=length-1;i>0;i--){
          let a =data[i].year_month_day;
          let b =addDate(data[i-1].year_month_day,1).substr(0,10);
          if(a===b){
            for(let index in data[i]){
              let indexToNum = parseInt(index.substr(4,2));
              if(indexToNum>=9 && indexToNum <= 23){
                data[i][index] = data[i-1][index];
              }
            }
          }
        }
        let today = toStandardDateString();
        let theDayBeforeThreeDay = addDate(today,-3).substr(0,10);
        if(data[0].year_month_day === theDayBeforeThreeDay)
          data.splice(0,1)
      },
      inputDefocus(row,column,newData,stationName,childStationName,lineName){
        if(typeof newData=='string')
          newData = parseFloat(newData);
        let index = this.hasModelBackupTime(row.time);        //判断修改了哪个数据
        if(index !== null ){
          let oldData = this.modelBackup.rows[index][column];
          if(oldData !== newData){
            //如果更改了数据,修改数据,增加log
            let operatorTime = toStandardTimeString();
            let field= '';
            if(column.indexOf('in')>=0)
              field = 'f_input_pressure';
            else if (column.indexOf('out')>=0)
              field = 'f_output_pressure';
            this.$resetpost('rs/logic/modifyData',{stationName:stationName, childStationName:childStationName, lineName:lineName,
              f_create_time:row.time, oldData:oldData,newData:newData,operatorTime:operatorTime,field:field,
              operatorName:this.$login.f.ename, operatorId:this.$login.f.loginid
            });
            //重新查询
            this.search();
          }
        }
        //将rows内数据格式化为number
        for(let i in this.model.rows){
          let rowsData = this.model.rows[i];
          for(let j in rowsData){
            if(j!=='time' && typeof rowsData[j] == 'string')
              rowsData[j] = parseInt(rowsData[j]);
          }
        }
      },
      async initStationName(){
        await this.$resetpost('api/af-revenue/sql/getAllStationName',{data:{}},{resolveMsg:null},{rejectMsg:"获取场站名称失败"}).then(resolve => {
          let tempName = [];
          this.stationName = [{label: '请选择', value: ''}];
          //获取所有场站名称
          for(let i in resolve.data){
            let data = resolve.data[i];
            let temp = {label:"",value:""};
            temp.label = temp.value = data.f_name;
            tempName.push(temp);
          }
          this.stationName = [this.stationName,...tempName]
          console.log("this.stationName=",this.stationName)
        })
      },
      async selfSearch(args){
        this.initVariable();
        await this.model.search(null, args.model)
        this.reset();
        console.log(this.model);
      },
      stationNameChange(val){
        this.currentStation = val[0];
        this.initVariable();
        this.search()
      },
      async search(){
        this.initVariable();
        await this.model.search(null,this.model);
        this.reset();
        console.log(this.model);
      },
      initVariable(){
        //计算起始日期及结束日期
        this.$refs.paged.$refs.cri.model.endDate = toStandardDateString()+' 08:00:00';
        this.$refs.paged.$refs.cri.model.startDate = addDate(this.$refs.paged.$refs.cri.model.endDate,-3);
        this.$refs.paged.$refs.cri.model.endDate = this.$refs.paged.$refs.cri.model.endDate.substr(this.$refs.paged.$refs.cri.model.endDate,10)+' 08:00:00';
        this.$refs.paged.$refs.cri.model.currentStation = this.currentStation;
        //this.model中存一份,方便搜索
        this.model.startDate = this.$refs.paged.$refs.cri.model.startDate;
        this.model.endDate = this.$refs.paged.$refs.cri.model.endDate;
        this.model.currentStation = this.currentStation?this.currentStation:'';
      }
    },
    computed:{

    }
  }
</script>

<style scoped>

</style>
