<template>
  <div class="flex-row binary" id="unit">
    <div class="basic-main">
      <div class="flex">
        <div class="flex" style="flex:1;">
          <criteria-paged :model="model" v-ref:paged>
            <criteria @condition-changed='$parent.search' partial='criteria' v-ref:criteria>
              <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
                <div class="row" width="100%">
                  <div class="col-sm-2">
                    <label class="font_normal_body">表&nbsp;钢&nbsp;号&nbsp;</label>
                    <input class="form-control" condition="f_meternumber like '%{}%'"
                           placeholder='表钢号'
                           type="text"
                           v-model="model.f_meternumber"
                    >
                  </div>

                  <div class="col-sm-2">
                    <label class="font_normal_body">使用公司</label>
                    <right-tree :source.sync='$parent.$parent.obj.source' :userid.sync='$parent.$parent.obj.userid'
                                @re-res="$parent.$parent.getRes"></right-tree>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">客户名称</label>
                    <input class="form-control" condition="f_user_name like '%{}%'" placeholder='客户名称'
                           type="text"
                           v-model="model.f_user_name">
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">用气地址</label>
                    <input class="form-control" condition="f_address like '%{}%'" placeholder='用气地址'
                           type="text"
                           v-model="model.f_address"
                    >
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">用户类型</label>
                    <v-select :options="$parent.$parent.Getusertype" :value.sync="model.f_user_type"
                              close-on-select
                              condition="f_user_type = '{}'"
                              placeholder='用户类型'
                              v-model="model.f_user_type"
                    >
                    </v-select>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">用气性质</label>
                    <v-select :options="$parent.$parent.Gasproperties" :value.sync="model.f_gasproperties"
                              close-on-select
                              condition="f_gasproperties = '{}'"
                              placeholder='用气性质'
                              v-model="model.f_gasproperties">
                    </v-select>
                  </div>

                  <div class="col-sm-2">
                    <label class="font_normal_body" style="word-break:keep-all;">上报时间</label>
                    <datepicker :format="'yyyy-MM-dd HH:mm:ss'"
                                :value.sync="model.startDate"
                                condition="f_insert_date >= '{}'"
                                placeholder="起始时间"
                                v-model="model.startDate"
                    ></datepicker>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                    <datepicker :format="'yyyy-MM-dd HH:mm:ss'"
                                :value.sync="model.endDate"
                                condition="f_insert_date <= '{}'"
                                placeholder="结束时间"
                                v-model="model.endDate"
                    ></datepicker>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body" style="word-break:keep-all;">安装时间</label>
                    <datepicker :format="'yyyy-MM-dd HH:mm:ss'"
                                :value.sync="model.startDate1"
                                condition="f_input_date >= '{}'"
                                placeholder="起始时间"
                                v-model="model.startDate1"
                    ></datepicker>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                    <datepicker :format="'yyyy-MM-dd HH:mm:ss'"
                                :value.sync="model.endDate1"
                                condition="f_input_date >= '{}'"
                                placeholder="结束时间"
                                v-model="model.endDate1"
                    ></datepicker>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">故障状态</label>
                    <v-select :options="$parent.$parent.FaultStates"
                              :value.sync="model.faultstate"
                              multiple
                              placeholder='故障状态'
                              v-model="model.faultstate">
                    </v-select>
                  </div>

                  <div class="col-sm-2">
                    <label class="font_normal_body">阀门状态</label>
                    <v-select :options="$parent.$parent.Valvestates" :value.sync="model.f_valvestate"
                              close-on-select
                              condition="f_valvestate like '%{}%'"
                              placeholder='阀门状态'
                              v-model="model.f_valvestate">
                    </v-select>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">上报类型</label>
                    <v-select :options="$parent.$parent.Getuploadtype" :value.sync="model.f_upload_type"
                              close-on-select
                              condition="f_upload_type = '{}'"
                              placeholder='上报类型'
                              v-model="model.f_upload_type"
                    >
                    </v-select>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body">用气量区间</label>
                    <input class="form-control" condition="f_oughtamount >= '{}'" placeholder='最小气量' type="text"
                           v-el:start_gas v-model="model.start_gas" v-next-el='start_gas'>
                  </div>
                  <div class="col-sm-2">
                    <label class="font_normal_body" style="word-break:keep-all;">&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                    <input class="form-control" condition="f_oughtamount <= '{}'" placeholder='最大气量' type="text"
                           v-el:end_gas v-el:xq v-model="model.end_gas" v-next-el='end_gas'
                    >
                  </div>
                </div>

                <div class="row" style="float: right">
                  <button @click="search()" class="btn btn-success width-100" v-el:cba>查询</button>
                  <button @click="$parent.$parent.clearmsg" class="btn btn-default width-100">清空</button>
                  <!--<button class="btn btn-success width-100" @click="$parent.$parent.SaveAsFile('webmeterhandplan')">导出</button>-->
                </div>
              </div>
            </criteria>
            <data-grid :model="model" id="webmeterhandplan" partial='list' class="list_area table_sy" 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">
                    <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>本次用气量(m³)</nobr>
                  </th>
                  <th style="text-align:center">
                    <nobr>气价(元/m³)</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">
                    <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">
                    <nobr>关阀走气</nobr>
                  </th>
                  <th style="text-align:center">
                    <nobr>拆表报警</nobr>
                  </th>
                  <th style="text-align:center">
                    <nobr>外部晶振标志</nobr>
                  </th>
                </tr>
              </template>
              <template partial='body' partial='list' v-ref:grid>
                <tr>
                  <td style="text-align:center">
                    <nobr>{{row.f_filiale}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_user_type}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_gasproperties}}</nobr>
                  </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_input_date}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>
                  <span @click="$parent.$parent.$parent.dealmsg(row)"><a>
                    {{row.f_meternumber}}
                  </a></span>
                    </nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_meter_brand}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_meter_style}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_balance}}</nobr>
                  </td>
                  <td style="text-align:center">{{row.f_last_tablebase}}</td>
                  <td style="text-align:center">{{row.f_tablebase}}</td>
                  <td style="text-align:center">{{row.f_oughtamount}}</td>
                  <td style="text-align:center">{{row.f_price}}</td>
                  <td style="text-align:center">{{row.f_oughtfee}}</td>
                  <td style="text-align:center">
                    <nobr>{{row.f_curbalance}}</nobr>
                  </td>
                  <td style="text-align:center;width: 170px;">
                    <nobr>{{row.f_upload_type}}</nobr>
                  </td>
                  <td style="text-align:center;width: 170px;">
                    <nobr>{{row.f_hand_date}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_last_hand_date}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_table_msg}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_last_meter_state_msg}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_insert_date}}</nobr>
                  </td>
                  <td style="text-align:center">
                    <nobr>{{row.f_software_version}}</nobr>
                  </td>

                  <td style="text-align:center">
                    <img :title='row.f_signal' height="12"
                         src="../../../../static/WebMetericon/1.png"
                         v-if="row.f_signal>-80" width="24"/>
                    <img :title='row.f_signal' height="12"
                         src="../../../../static/WebMetericon/2.png"
                         v-if="row.f_signal>-100&&row.f_signal<=-80" width="24"/>
                    <img :title='row.f_signal' height="12"
                         src="../../../../static/WebMetericon/3.png"
                         v-if="row.f_signal>-120&&row.f_signal<=-100" width="24"/>
                    <img :title='row.f_signal' height="12"
                         src="../../../../static/WebMetericon/4.png"
                         v-if="row.f_signal<=-120" width="24"/>
                  </td>
                  <td style="text-align:center">
                    <img :title='row.f_batterylevel' height="12"
                         src="../../../../static/WebMetericon/01.png"
                         v-if="row.f_batterylevel>=6.00" width="24"/>
                    <img :title='row.f_batterylevel' height="12"
                         src="../../../../static/WebMetericon/02.png"
                         v-if="row.f_batterylevel>=5.00&&row.f_batterylevel<6.00" width="24"/>
                    <img :title='row.f_batterylevel' height="12"
                         src="../../../../static/WebMetericon/03.png"
                         v-if="row.f_batterylevel>=4.00&&row.f_batterylevel<5.00" width="24"/>
                    <img :title='row.f_batterylevel' height="12"
                         src="../../../../static/WebMetericon/04.png"
                         v-if="row.f_batterylevel<4.00" width="24"/>
                  </td>
                  <td style="text-align:center;width: 51px">{{row.f_valvestate==1?'关阀':'开阀'}}</td>
                  <td style="text-align:center;width: 51px">
                    <img :title='故障' height="12"
                         src="../../../../static/WebMetericon/guzhang.png"
                         v-if="row.f_magneticinterference==1" width="12"/>
                    <img :title='正常' height="12"
                         src="../../../../static/WebMetericon/zhengchang.png"
                         v-if="row.f_magneticinterference==0" width="17"/>
                  </td>
                  <td style="text-align:center;width: 51px">
                    <img :title='关阀' height="12"
                         src="../../../../static/WebMetericon/guzhang.png"
                         v-if="row.f_cycleshutvalue==1" width="12"/>
                    <img :title='正常' height="12"
                         src="../../../../static/WebMetericon/zhengchang.png"
                         v-if="row.f_cycleshutvalue==0" width="17"/>
                  </td>
                  <td style="text-align:center;width: 51px">
                    <img :title='关阀' height="12"
                         src="../../../../static/WebMetericon/guzhang.png"
                         v-if="row.f_overcurrentprotection==1" width="12"/>
                    <img :title='正常' height="12"
                         src="../../../../static/WebMetericon/zhengchang.png"
                         v-if="row.f_overcurrentprotection==0" width="17"/>
                  </td>
                  <td style="text-align:center;width: 51px">
                    <img :title='走气' height="12"
                         src="../../../../static/WebMetericon/guzhang.png"
                         v-if="row.f_leakageflag==1" width="12"/>
                    <img :title='正常' height="12"
                         src="../../../../static/WebMetericon/zhengchang.png"
                         v-if="row.f_leakageflag==0" width="17"/>
                  </td>
                  <td style="text-align:center;width: 51px">
                    <img :title='拆表' height="12"
                         src="../../../../static/WebMetericon/guzhang.png"
                         v-if="row.f_splitalarm==='1'" width="12"/>
                    <img :title='正常' height="12"
                         src="../../../../static/WebMetericon/zhengchang.png"
                         v-if="row.f_splitalarm==='0'" width="17"/>
                  </td>
                  <td style="text-align:center;width: 51px">
                    <img :title='停止' height="12"
                         src="../../../../static/WebMetericon/guzhang.png"
                         v-if="row.f_xtipllstopflag==='1'" width="12"/>
                    <img :title='正常' height="12"
                         src="../../../../static/WebMetericon/zhengchang.png"
                         v-if="row.f_xtipllstopflag==='0'" width="17"/>
                  </td>

                </tr>
              </template>
              <template partial='foot'></template>
            </data-grid>
          </criteria-paged>
        </div>
        <!--异常抄表录入-->
        <modal :show.sync="handshow" backdrop="false" v-ref:modal>
          <header class="modal-header" slot="modal-header">
            <button @click="handchange()" class="close" type="button"><span>&times;</span></button>
            <h4 class="modal-title">表号{{handExce.exceobj.f_meternumber}}</h4>
          </header>
          <article class="modal-body" slot="modal-body">
            <div class="col-sm-12">
              <label class="font_normal_body">上报异常类型</label>
              <textarea class="ver-textarea" class="form-control" name="name"
                        placeholder='上报异常类型' rows="2"
                        style="width: 100%"
                        v-model='handExce.f_error_type'></textarea>
            </div>
            <div class="col-sm-12 form-group form-fix-width">
              <label class="font_normal_body">异常描述</label>
              <textarea class="ver-textarea" class="form-control" name="name"
                        placeholder='上报异常类型'
                        rows="2" style="width: 100%"
                        v-model='handExce.f_error_msg'></textarea>
            </div>
          </article>
          <footer class="modal-footer" slot="modal-footer">
            <button @click='handchange()' class="btn btn-default" type="button" v-show="handshow">取消</button>
            <button @click='saveException' class="btn btn-success" type="button" v-show="handshow">确认</button>
          </footer>
        </modal>
      </div>
    </div>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'

  export default {
    title: '数据采集',
    data() {
      return {
        model: new PagedList('rs/sql/dataCollectionQuery', 50),
        row: null,
        //搜索显示开关
        searchshow: false,
        //抄表录入开关
        handshow: false,
        isdeal: true,
        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,
        },
        handExce: {
          //异常对象
          exceobj: {},
          //异常类型
          f_error_type: "",
          //异常描述
          f_error_msg: '',
        },
        show: false,
      }
    },
    props: ['searchobj', 'isdeal'
    ],
    ready() {

    },
    methods: {
      //跳转处理页面
      dealmsg(val) {
        val.model = this.model.model;
        this.$emit('deal-msg', val)
      },
      //查询开关
      seachchange() {
        this.searchshow = !this.searchshow
      },
      //异常开关
      handchange(val) {
        if (val) {
          console.log("开启弹窗");
          this.handExce.exceobj = val
        } else {
          console.log("返回置空");
          this.handExce = {}
        }
        this.handshow = !this.handshow
      },
      //保存输入
      saveException() {
        //准备数据
        var Exejson = {};
        //抄表记录id
        Exejson.data_id = this.handExce.exceobj.id;
        //异常表档案ID
        Exejson.f_userfiles_id = this.handExce.exceobj.f_userfiles_id;
        //异常户档案ID
        Exejson.f_userinfo_id = this.handExce.exceobj.f_userinfo_id;
        //异常表号
        Exejson.f_meternumber = this.handExce.exceobj.f_meternumber;
        //分公司
        Exejson.f_filiale = this.handExce.exceobj.f_filiale;
        //分公司编号
        Exejson.f_filialeids = this.handExce.exceobj.f_filialeids;
        //营业网点
        Exejson.f_outlets = this.handExce.exceobj.f_outlets;
        //组织机构
        Exejson.f_orgstr = this.handExce.exceobj.f_orgstr;
        //异常类型
        Exejson.f_error_type = this.handExce.f_error_type;
        //异常说明
        Exejson.f_error_msg = this.handExce.f_error_msg;
        console.log("提交异常数据");
        console.log(Exejson);
        try {
          this.$resetpost('rs/entity/t_exception', Exejson, {resolveMsg: null, rejectMsg: null}).then((req) => {
            console.log(val);
            this.handExce = {};
            console.log("提交成功")
          }).then(() => {
            this.$showMessage("异常保存成功！")
          })
        } catch (e) {
          this.handExce = {};
          console.log("提交失败");
          this.$showMessage("异常保存失败！")
        }
        this.handshow = false
      },
      DealSignal(val) {
        console.log(val.model.f_signal);
        if (val.model && val.model.f_signal) {
          if (val.model.f_signal == '信号正常') {
            val.condition = val.condition + " and " + "f_signal>-100 "
          }
          if (val.model.f_signal == '信号异常') {
            val.condition = val.condition + " and " + "f_signal<-100 "
          }
        }
        return val
      },
      DealFaults(val) {
        if (val.model && val.model.faultstate) {
          console.log(val.model.faultstate);
          val.model.faultstate.forEach((item) => {
            if (item == '磁干扰') {
              val.condition = val.condition + " and " + "f_magneticInterference=1 "
            }
            if (item == '周期关阀') {
              val.condition = val.condition + " and " + "f_cycleShutValue=1 "
            }
            if (item == '过流保护关阀') {
              val.condition = val.condition + " and " + "f_overCurrentProtection=1 "
            }
            if (item == '关阀走气') {
              val.condition = val.condition + " and " + "f_leakageFlag=1 "
            }
            if (item == '拆表报警') {
              val.condition = val.condition + " and " + "f_SplitAlarm=1 "
            }
          })
        }
        return val
      },
      // 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.criteria.model = {}
      },
      search(args) {
        // 处理故障类型
        args = this.DealFaults(args);
        // 处理信号
        args = this.DealSignal(args);
        args.condition = `${args.condition} and f_filialeids like '${this.obj.resids}%'`;
        this.model.search(args.condition, args.model)
      },
      getRes(obj) {
        this.obj.res = obj.res;
        this.obj.resids = obj.resids
      },
    },

    watch: {
      // 监听查询对象
      'searchobj'(val) {
        if (this.searchobj != null) {
          this.$refs.paged.$refs.criteria.model.f_meternumber = this.searchobj.f_meternumber;
          /*          this.$refs.paged.$refs.criteria.model.startDate = this.searchobj.model.startDate
                    this.$refs.paged.$refs.criteria.model.endDate = this.searchobj.model.endDate*/
          this.$refs.paged.$refs.criteria.search()
        }
      }
    },
    computed: {
      // 上报异常类型
      Gasproperties() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用气性质')]
      },
      Getusertype() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      Getuploadtype() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('上报类型')]
      },
      // 阀门状态
      Valvestates() {
        return [{label: '全部', value: '%%'}, {label: '关阀', value: '1'}, {label: '开阀', value: '0'}]
      },
      // 故障状态
      FaultStates() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('故障状态')]
      }
    }
  }
</script>
<style>
  .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>
