<template>
  <div class="flex bg-white p-10  datapanel" v-show="(!echarts)&&(!isActiveTotalForm)">
    <criteria-paged :model="model" :pager='true' v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.search' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row" width="100%">
            <!--<div class="form-group">-->
            <!--<input  type="text" class="form-control" v-model="model.f_username" placeholder='用户姓名'-->
            <!--condition="f_user_name = '{}'" v-next-el='userid' v-el:username-->
            <!--:size="model.f_user_name ? model.f_user_name.length * 2 : 8">-->
            <!--</div>-->
            <div class="col-sm-3 form-group form-input-group">
              <label class="control-label">表&nbsp;钢&nbsp;号&nbsp;</label>
              <input type="text" class="form-control" v-model="model.f_meternumber" placeholder='表钢号'
                     condition="f_meternumber like '%{}%'" v-next-el='meternumber' v-el:meternumber
              >
            </div>
            <div v-if="$parent.$parent.searchshow" class="form-horizontal select-overspread container-fluid auto">
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">组织机构</label>
                <right-tree :userid.sync='$parent.$parent.obj.userid' :source.sync='$parent.$parent.obj.source'
                            :multiple="true"
                            @re-res="$parent.$parent.getRes"></right-tree>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">用户类型</label>
                <v-select
                  v-model="model.f_user_type"
                  value-single="true"
                  placeholder='用户类型'
                  :value.sync="model.f_user_type"
                  :options='$parent.$parent.Gasusertype'
                  condition="u.f_user_type {}"
                  close-on-select>
                </v-select>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">用气性质</label>
                <v-select
                  v-model="model.f_gasproperties"
                  placeholder='用气性质'
                  value-single="true"
                  :value.sync="model.f_gasproperties"
                  :options='$parent.$parent.Gasproperties'
                  condition="u.f_gasproperties {}"
                  close-on-select>
                </v-select>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">&nbsp;用&nbsp;户&nbsp;号</label>
                <input type="text" class="form-control" v-model="model.f_userinfo_code" placeholder='用户号'
                       condition="f_userinfo_code like '%{}%'" v-el:xq v-next-el='userinfocode' v-el:username
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">用户姓名</label>
                <input type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
                       condition="f_user_name like '%{}%'" v-el:xq v-next-el='username' v-el:username
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">上报区间</label>
                <datepicker placeholder="开始时间"
                            v-model="model.startDate"
                            :value.sync="model.startDate"
                            :disabled-days-of-Week="[]"
                            :format="'yyyy-MM-dd HH:mm:ss'"

                            :show-reset-button="reset">
                </datepicker>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                <datepicker placeholder="结束日期"
                            v-model="model.endDate"
                            :value.sync="model.endDate"
                            :disabled-days-of-Week="[]"
                            :format="'yyyy-MM-dd HH:mm:ss'"

                            :show-reset-button="reset">
                </datepicker>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">安装日期</label>
                <datepicker placeholder="开始时间"
                            v-model="model.startdate"
                            :value.sync="model.startdate"
                            :disabled-days-of-Week="[]"
                            :format="'yyyy-MM-dd HH:mm:ss'"
                            condition="f_gas_date >= '{}'"
                            :show-reset-button="reset">
                </datepicker>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                <datepicker placeholder="结束日期"
                            v-model="model.enddate"
                            :value.sync="model.enddate"
                            :disabled-days-of-Week="[]"
                            :format="'yyyy-MM-dd HH:mm:ss'"
                            condition="f_gas_date <= '{}'"
                            :show-reset-button="reset">
                </datepicker>
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">最长失联</label>
                <input type="text" class="form-control" v-model="model.max_days1" placeholder='天数大于等于'
                       condition="max_days >= {}" v-next-el='max_days' v-el:max_days
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                <input type="text" class="form-control" v-model="model.max_days2" placeholder='天数小于等于'
                       condition="max_days <= {}" v-next-el='max_days' v-el:max_days
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">合计失联</label>
                <input type="text" class="form-control" v-model="model.all_days1" placeholder='天数大于等于'
                       condition="all_days >= {}" v-next-el='all_days' v-el:all_days
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                <input type="text" class="form-control" v-model="model.all_days2" placeholder='天数小于等于'
                       condition="all_days <= {}" v-next-el='all_days' v-el:all_days
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">最新失联</label>
                <input type="text" class="form-control" v-model="model.new_days1" placeholder='天数大于等于'
                       condition="new_days >= {}" v-next-el='new_days' v-el:new_days
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                <input type="text" class="form-control" v-model="model.new_days2" placeholder='天数小于等于'
                       condition="new_days <= {}" v-next-el='new_days' v-el:new_days
                >
              </div>
              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">用气地址</label>
                <input type="text" class="form-control" v-model="model.f_area" placeholder='用气地址'
                       condition="f_address like '%{}%'" v-el:xq v-next-el='address' v-el:area
                >
              </div>
              <div class="col-sm-3 form-group form-input-group" title="参数：用户等级">
                <label class="control-label">用户等级</label>
                <v-select
                  placeholder='用户等级'
                  :value.sync="model.f_user_level"
                  v-model="model.f_user_level"
                  :options='$parent.$parent.f_user_level'
                  close-on-select
                  condition="f_user_level='{}'">
                </v-select>
              </div>

              <div class="col-sm-3 form-group form-input-group">
                <label class="control-label">阀门状态</label>
                <v-select v-model="model.f_valvestate"
                          placeholder="阀门状态"
                          :value.sync="model.f_valvestate"
                          :options='$parent.$parent.valveStatus'
                          close-on-select >
                </v-select>
              </div>

            </div>

            <div class="col-sm-3 form-group ">
              <label class="control-label"></label>
              <button class="btn btn-success" @click="search()">查询</button>
              <label class="control-label"></label>
              <button class="btn btn-default" @click="$parent.$parent.clearmsg">清空</button>
              <label class="control-label" v-if="!$parent.$parent.excelTable">超过十万条限制导出</label>
              <export-excel v-if="$parent.$parent.excelTable"
                            :data="{condition: $parent.$parent.condition1,startDate: model.startDate, endDate: model.endDate}"
                            :field="$parent.$parent.getfield"
                            sqlurl="rs/logic/webmeter_exportfile" sql-name="webmeter_getNewLoseQuery"
                            template-name='LostContactAnalysis'
                            :choose-col="true"></export-excel>

            </div>
            <div class="col-sm-3 form-group ">
              <label class="control-label"></label>
              <button class="btn btn-success" @click="$parent.$parent.showTotalForm()">显示汇总</button>
              <label class="control-label"></label>
              <button class="btn btn-success" @click="$parent.$parent.showEcharts()">显示图表</button>
            </div>
            <div v-show="!$parent.$parent.searchshow" @click="$parent.$parent.clickshow"
                 style="position: relative;float: right;margin-right: 1%;">
              <img src="../../../static/webMeter/shousuo.png"><span>展开</span>
            </div>
            <div v-show="$parent.$parent.searchshow" @click="$parent.$parent.clickshow"
                 style="position: relative;float: right;margin-right: 1%;">
              <img src="../../../static/webMeter/shangsheng.png"><span>收缩</span>
            </div>

          </div>
        </div>
      </criteria>
      <data-grid id="lostuserlist" :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th style="text-align:center">
              <nobr>组织机构</nobr>
            </th>
            <th style="text-align:center">
              <nobr>用户号</nobr>
            </th>
            <th style="text-align:center">
              <nobr>用户类型</nobr>
            </th>
            <th style="text-align:center">
              <nobr>用气性质</nobr>
            </th>
            <th style="text-align:center">
              <nobr>用户姓名</nobr>
            </th>
            <th style="text-align:center">
              <nobr>用气地址</nobr>
            </th>
            <th style="text-align:center">
              <nobr>安装日期</nobr>
            </th>
            <th style="text-align:center">
              <nobr>表钢号</nobr>
            </th>
            <th style="text-align:center" title="指查询时间区间内，连续失联最长的天数">
              <nobr>最长失联天数</nobr>
            </th>
            <th style="text-align:center" title="指查询时间区间内，共计失联的总天数">
              <nobr>合计失联天数</nobr>
            </th>
            <th style="text-align:center">
              <nobr>最长失联类型</nobr>
            </th>
            <th style="text-align:center">
              <nobr>合计失联类型</nobr>
            </th>
            <th style="text-align:center" title="指查询时间区间内，首次上报的日期">
              <nobr>首次上报日期</nobr>
            </th>
            <th style="text-align:center" title="指查询时间区间内，最后上报的日期">
              <nobr>最后上报日期</nobr>
            </th>
            <th style="text-align:center">
              <nobr>期间上报天数</nobr>
            </th>
            <th style="text-align:center" title="指该表具最后一次上报日期到当前日期相差天数">
              <nobr>最新失联天数</nobr>
            </th>
            <th style="text-align:center" title="指该表具最新的失联情况">
              <nobr>最新状态</nobr>
            </th>
            <th  style="text-align:center" title="指该表具最新的阀门状态">
              <nobr>阀门状态</nobr>
            </th>
          </tr>

        </template>
        <template partial='body'>
          <!-- <td style="text-align:center">{{$index+1}}</td>-->
          <!--<td style="text-align:center">{{row.f_user_id}}</td>-->
          <td style="text-align:center">
            <nobr>{{row.f_filiale}}</nobr>
          </td>
          <td style="text-align:center">{{row.f_userinfo_code}}</td>
          <td style="text-align:center">{{row.f_user_type}}</td>
          <td style="text-align:center">{{row.f_gasproperties}}</td>
          <td style="text-align:center">
            <nobr>{{row.f_user_name}}</nobr>
          </td>
          <td>
            <nobr>{{row.f_address}}</nobr>
          </td>
          <td style="text-align:center">
            <nobr>{{row.f_gas_date}}</nobr>
          </td>
          <td style="text-align:center">
            <nobr>{{row.f_meternumber}}</nobr>
          </td>
          <td style="text-align:center">{{row.max_days}}</td>
          <td style="text-align:center">{{row.all_days}}</td>
          <td style="text-align:center">
            <nobr>{{row.max_days_type}}</nobr>
          </td>
          <td style="text-align:center">
            <nobr>{{row.all_days_type}}</nobr>
          </td>
          <td style="text-align:center">
            <nobr>{{row.min_insert_date}}</nobr>
          </td>
          <td style="text-align:center">
            <nobr>{{row.max_insert_date}}</nobr>
          </td>
          <td style="text-align:center">{{row.c}}</td>
          <td style="text-align:center">{{row.new_days}}</td>
          <td style="text-align:center">{{row.f_state}}</td>
          <td style="text-align:center">{{row.f_valvestate}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <div v-show="echarts || isActiveTotalForm" style="height: 6%;width: 100%;">
    <div style="display: inline-block;">
      <button class="btn btn-success" v-show="echarts"  @click="notShowEcharts()">隐藏图表</button>
      <button class="btn btn-success"v-show="isActiveTotalForm" @click="notShowTotalForm()">隐藏汇总</button>
    </div>
    <div style="display: inline-block;">
      <label class="control-label">失联天数类型</label>
      <v-select v-model="loseDaysType"
                placeholder='最长失联天数'
                value-single="true"
                :value.sync="loseDaysType"
                :options='getloseDaysType'
                close-on-select></v-select>
    </div>
    <div class="mystyle" v-show="echarts" style="display: inline-block;">
      <label class="control-label">图表统计项</label>
      <v-select v-model="groupname"
                placeholder='分组项'
                value-single="true"
                :value.sync="groupname"
                :options='getgroupname'
                close-on-select></v-select>
    </div>
  </div>
  <div class="span" v-show="echarts" style="width: 100%;height: 90%;">
    <cr-lost-contact-echarts :groupname="groupname[0]" :condition="condition" :startdate="startDate" :losetype="loseDaysType[0]"
                          :enddate="endDate" :echarts="echarts"></cr-lost-contact-echarts>
  </div>
  <div class="span" v-show="isActiveTotalForm" style="width: 100%;height: 90%;">
    <cr-lost-contact-eform :condition="condition" :startdate="startDate" :losedaystype="loseDaysType[0]"
                        :enddate="endDate"></cr-lost-contact-eform>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  import * as Util from '../../Util'
  import getLodop from '../../plugins/LodopFuncs'
  import {HttpResetClass} from 'vue-client'

  export default {
    title: '失联分析',
    data() {
      return {
        model: new PagedList('rs/sql/webmeter_getNewLoseQuery', 20, {
          startDate: 'this.model.startDate',
          endDate: 'this.model.endDate'
        }),
        reportStr: null,
        show: false,
        startDate: '',
        endDate: '',
        Gasproperties2: [],
        outlets: [],
        operator: [],
        filename: '',
        echarts: false,
        searchshow: false,
        isActiveTotalForm: false,
        condition: '1=1',
        condition1: '1=1',
        filialeCodeStr: '',
        BatteryStatus:[
          {label: '全部', value: ''},
          {label: '未装电池', value: '='},
          {label: '电池正常', value: '!='}
        ],
        groupname: ['f_user_type'],
        getgroupname: [
          {label: '用户类型', value: 'f_user_type'},
          {label: '用气性质', value: 'f_gasproperties'},
          {label: '分公司', value: 'f_filiale'}
        ],
        loseDaysType: ['max_days'],
        getloseDaysType: [
          {label: '最长失联天数', value: 'max_days'},
          {label: '合计失联天数', value: 'all_days'},
          {label: '最新失联天数', value: 'new_days'}
        ],
        valveStatus: [
          {label: '全部', value: ''},
          {label: '开阀', value: '= 0'},
          {label: '关阀', value: '= 1'},
          {label: '未上报', value: 'is null'}
        ],
        excelTable: true,
        getfield: {
          'f_filiale': '组织机构',
          'f_userinfo_code': '用户号',
          'f_user_type': '用户类型',
          'f_gasproperties': '用气性质',
          'f_user_name': '用户姓名',
          'f_address': '用气地址',
          'f_gas_date': '安装日期',
          'f_meternumber': '表钢号',
          'max_days': '最长失联天数',
          'all_days': '合计失联天数',
          'max_days_type': '最长失联类型',
          'all_days_type': '合计失联类型',
          'min_insert_date': '首次上报日期',
          'max_insert_date': '最后上报日期',
          'c': '期间上报天数',
          'new_days': '最新失联天数',
          'f_state': '最新状态'
        },
        obj: {
          userid: this.$login.f.id,
          source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
          res: this.$login.f.f_fengongsi,
          resids: this.$login.f.f_orgids
        }
      }
    },
    ready() {
      this.$refs.paged.$refs.cri.model.startDate = Util.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.cri.model.endDate = Util.toStandardDateString() + ' 23:59:59'
    },
    methods: {
      clickshow() {
        this.searchshow = !this.searchshow
      },
      search(args) {
        let start = this.$refs.paged.$refs.cri.model.startDate
        let end = this.$refs.paged.$refs.cri.model.endDate
        if (start == null || start == '' || end == null || end == '') {
          return this.$showMessage("上报区间的开始和结束时间不能为空！")
        }
        this.startDate = start
        this.endDate = end
        let othercondition = '1=1'
        const valveState = this.$refs.paged.$refs.cri.model.f_valvestate
        if (Array.isArray(valveState) && valveState != '' && valveState.length > 0) {
          othercondition = valveState
            .map(item => `f_valvestate ${item}`)
            .join('')
        }
        this.model.params.valve = othercondition
        const filialeCondition = this.filialeCodeStr || this.getDefaultOrgCondition()
        if (filialeCondition) {
          args.condition = `${args.condition} and ${filialeCondition}`
        }
        let str = args.condition
        this.condition1 = str
        this.model.search(args.condition, args.model)
        console.log(str + "str")
        this.condition = str
        // this.condition = this.$refs.paged.$refs.cri.condition
      },
      getRes(obj) {
        this.obj.res = obj.res
        this.obj.resids = obj.resids
        const ids = (obj.resids || '').split(',').map(item => item.trim()).filter(item => item)
        if (ids.length <= 1) {
          this.filialeCodeStr = ids[0] ? `u.f_filialeids like '${ids[0]}%'` : ''
        } else {
          const parts = ids.map(code => `u.f_filialeids like '${code}%'`)
          this.filialeCodeStr = `(${parts.join(' or ')})`
        }
      },
      getDefaultOrgCondition () {
        const defaultOrg = this.$login.f.f_orgids || ''
        return defaultOrg ? `u.f_filialeids like '${defaultOrg}%'` : ''
      },
      SaveAsFile(str) {
        this.$showMessage('您是否需要导出本页数据？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            let style = '<style>' +
              'td {border-width: 1px;padding: 2px;border-style: solid; border-color:black; border-collapse:collapse;} .tdright{text-align: right;}  .tdcenter{text-align: center;}</style>';
            let strHtml = style + document.getElementById(str).innerHTML;
            console.log(strHtml);
            let LODOP = getLodop();
            LODOP.PRINT_INIT('');
            LODOP.ADD_PRINT_TABLE(5, 5, '99%', '100%', strHtml);
            LODOP.SAVE_TO_FILE('失联分析.xls')
          }
        })
      },
      clearmsg () {
        this.$refs.paged.$refs.cri.model = {}
      },
      close () {
        this.show = false
      },
      // 显示柱状图
      showEcharts() {
        this.echarts = true
        this.isActiveTotalForm = false
      },
      notShowTotalForm() {
        this.echarts = false
        this.isActiveTotalForm = false
      },
      // 不显示柱状图
      notShowEcharts() {
        this.echarts = false
        this.isActiveTotalForm = false
      },
      // 显示汇总数据表格
      showTotalForm() {
        this.echarts = false
        this.isActiveTotalForm = true
      },
      uniq(array) {
        array.sort();
        let arraytype = [{label: '全部', value: ''}]
        let dr = {label: '', value: ''}
        if (array[0] == 'n') {
          dr = {label: '空值', value: 'is null'}
        } else {
          dr = {label: array[0], value: "='" + array[0] + "'"}
        }
        arraytype.push(dr)
        let temp = [array[0]];
        console.log(array[0] + "array[0]")
        for (var i = 1; i < array.length; i++) {
          if (array[i] !== temp[temp.length - 1]) {
            if (array[i] == 'n') {
              dr = {label: '空值', value: 'is null'}
            } else {
              dr = {label: array[i], value: "='" + array[i] + "'"}
            }
            arraytype.push(dr);
            temp.push(array[i]);
          }
        }
        /*console.log(arraytype[1].label + "label")
        console.log(arraytype[1].value + "value")*/
        return arraytype;
      }
    },
    watch: {
    },
    computed: {
      signtype1() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('标记名称')]
      },
      getSignStatus() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('标记状态')]
      },
      f_user_level() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户等级')]
      },
      Gasusertype () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      Gasproperties () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用气性质')]
      }
    }
  }
</script>
<style>
  .form-input-group label {
    text-align: right;
    width: auto;
  }

  .mystyle {
    float: right;
    padding-right: 10%;
  }

  .mystyle1 {
    padding-top: 10px;

  }

  .mystyle2 {
    width: 100%;
    height: 35px;
    padding-left: 15px;
    float: left;
  }

  .datapanel {
    color: #333;
    background-color: white;
    box-shadow: darkgrey 0.5px 0.5px 0.5px 0.5px;
    padding: 10px 30px 10px 30px;
    border-radius: 15px;
  }
</style>
