<template>
  <div class="flex-row" v-if="!showdetail">
    <div id='app1' class="basic-main" 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" novalidate partial>
            <div class="row">
                <!--<div class="form-group">-->
                <!--<input @keyup.enter="search"   style="width:60%"  type="text" class="input_search" 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-2 form-group">
                  <label class="font_normal_body">客户编号</label>
                  <input @keyup.enter="search" style="width:60%" type="text" class="input_search"
                         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-2 form-group">
                  <label class="font_normal_body">客户名称</label>
                  <input @keyup.enter="search" style="width:60%" type="text" class="input_search"
                         v-model="model.f_user_name" placeholder='客户名称'
                         condition="f_user_name = '{}'" v-el:xq v-next-el='username' v-el:username
                  >
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户地址</label>
                  <input @keyup.enter="search" style="width:60%" type="text" class="input_search" 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-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;表号&nbsp;&nbsp;&nbsp;</label>
                  <input @keyup.enter="search" style="width:60%" type="text" class="input_search"
                         v-model="model.f_meternumber" placeholder='表号'
                         condition="f_meternumber like '%{}%'" v-next-el='meternumber' v-el:meternumber
                  >
                </div>

              <div style="float:right">
                <button class="button_search button_spacing" @click="search()">查询</button>
                <button class="button_clear button_spacing" @click="$parent.$parent.clearmsg">清空</button>
                <button class="button_clear button_spacing" @click="$parent.$parent.shielding">批量拉黑</button>
                <export-excel :data="$parent.$parent.getCondition" class="button_export button_spacing"
                              :field="$parent.$parent.getfield"  :sumname="$parent.$parent.sumName"
                              sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="webmeter_getUserLoseQuery" template-name='用户失联查询'
                              :choose-col="true"></export-excel>
    <!--              <button class="button_export button_spacing" @click="$parent.$parent.SaveAsFile('lostuserlist')">导出</button>-->
    <!--              <button class="button_export button_spacing" style="width: max-content" @click="$parent.$parent.showTotalForm()">显示汇总</button>-->
    <!--              <button class="button_search button_spacing" @click="$parent.$parent.showEcharts()">显示图表</button>-->
                <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()"></div>
              </div>
            </div>
            <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp;</label>
              <right-tree @re-res="$parent.$parent.getorg"
                          :initresid='$parent.$parent.org'></right-tree>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">客户类型</label>
              <v-select style="width:60%" id="f_user_type"
                        v-model="model.f_user_type"
                        placeholder='客户类型'
                        :value.sync="model.f_user_type"
                        :options='$parent.$parent.usertypes'
                        condition="f_user_type = '{}'"
                        close-on-select>
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">用气性质</label>
              <v-select style="width:60%" id="f_gasproperties"
                        v-model="model.f_gasproperties"
                        placeholder='用气性质'
                        :value.sync="model.f_gasproperties"
                        :options='$parent.$parent.Gasproperties'
                        condition="f_gasproperties = '{}'"
                        close-on-select>
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">气表品牌</label>
              <v-select
                placeholder='气表品牌'
                :value.sync="model.f_meter_brand"
                v-model="model.f_meter_brand"
                :options='$parent.$parent.WarningType'
                @change="$parent.$parent.getMeterType"
                close-on-select
                condition="f_meter_brand='{}'">
              </v-select>
            </div>
            <div class="col-sm-2 form-group" >
              <label class="font_normal_body">气表型号</label>
              <v-select v-if="$parent.$parent.styles"
                        placeholder='气表型号'
                        :value.sync="model.f_meter_style"
                        v-model="model.f_meter_style"
                        :options='$parent.$parent.Warningstyle'
                        close-on-select
                        condition="f_meter_style='{}'">
              </v-select>
              <v-select v-if="!$parent.$parent.styles"
                        placeholder='气表型号'
                        :value.sync="model.f_meter_style"
                        v-model="model.f_meter_style"
                        :options='$parent.$parent.Warningstyles'
                        close-on-select
                        condition="f_meter_style='{}'">
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">用户状态</label>
              <v-select
                placeholder='用户状态'
                value-single
                style="width:60%"
                :value.sync="model.f_user_state"
                v-model="model.f_user_state"
                :options='$parent.$parent.userstate'
                close-on-select condition="f_user_state ='{}'">
              </v-select>
            </div>
            <div class="col-sm-2 form-group  flex-row" style="white-space: nowrap">
              <label class="font_normal_body">失联天数&nbsp;</label>
              <div class="row">
                <input type="text" style="width:27%" class="input_search" v-model="model.SLday1"
                       condition="SLday >= {} " placeholder="">
                <label class="font_normal_body"> ≤ x ≤ </label>
                <input type="text" style="width:27%" class="input_search" v-model="model.SLday2"
                       condition="SLday <= {} " placeholder="">
              </div>
            </div>
            <div class="col-sm-2 form-group ">
              <label for="contain_newfile" class="font_normal_body">开户未上报</label>
              <v-select id="contain_newfile"
                        v-model="model.notappear"
                        style="width:60%"
                        :value.sync="model.notappear"
                        :options='$parent.$parent.not_appear'
                        :value-single="true"
                        close-on-select>
              </v-select>
            </div>
              <div class="col-sm-2 form-group">
                <label for="f_user_usetype" class="font_normal_body ">用气类型</label>
                <v-select :value.sync="model.f_user_usetype" :search="false"
                          @change="$parent.$parent.userUseTypeChange()"
                          v-model="model.f_user_usetype"
                          :options='$parent.$parent.userusetypes'
                          condition="f_user_usetype = '{}'"
                          placeholder='用气类型'
                          value-single="true"
                          close-on-select>
                </v-select>
              </div>
              <div class="col-sm-2 form-group">
                <label for="f_use_institution" class="font_normal_body">用气机构</label>
                <v-select :value.sync="model.f_use_institution" :search="false"
                          @change="$parent.$parent.userUseInstitutionChange()"
                          v-model="model.f_use_institution"
                          :options='$parent.$parent.useinstitutions'
                          condition="f_use_institution = '{}'"
                          placeholder='用气机构'
                          value-single="true"
                          close-on-select>
                </v-select>
              </div>
              <div class="col-sm-2 form-group">
                <label for="f_user_usenature" class="font_normal_body ">使用性质</label>
                <v-select :value.sync="model.f_user_usenature" :search="false"
                          v-model="model.f_user_usenature"
                          :options='$parent.$parent.userusenatures'
                          condition="f_user_usenature = '{}'"
                          placeholder='使用性质'
                          value-single="true"
                          close-on-select>
                </v-select>
              </div>
          </div>
          </div>
        </criteria>
        <data-grid id="lostuserlist" :model="model" partial='list' v-ref:grid class="list_area table_sy" :optional="true">
          <template partial='head'>
              <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>
          </template>
          <template partial='body'>
            <td style="text-align:center">{{$index+1}}</td>
            <td style="text-align: center;"><nobr>
              <span @click="$parent.$parent.$parent.showmsg(row)"><a>{{row.f_userinfo_code}}</a></span>
            </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">{{row.f_user_type}}</td>
            <td style="text-align:center">{{row.f_gasproperties}}</td>
            <td style="text-align:center">{{row.f_user_phone}}</td>
            <td style="text-align:center">{{row.f_balance_amount}}</td>
            <td style="text-align:center"><nobr>{{row.f_meternumber}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_startuse_date}}</nobr></td>
            <td style="text-align:center">
              <nobr>{{row.initinsertdate}}</nobr>
            </td> <td style="text-align:center">
            <nobr>{{row.lastinsertdate}}</nobr>
          </td>
          <td style="text-align:center">{{row.f_meter_base}}</td>

            <td style="text-align:center">
              <nobr>{{row.slday}}</nobr>
            </td>
            <td style="text-align:center">{{row.f_meter_brand}}</td>
            <td style="text-align:center">
              <nobr>{{row.f_meter_style}}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{row.f_user_state}}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{row.f_orgname}}</nobr>
            </td>
          </template>
      </data-grid>
      </criteria-paged>
    </div>
    <modal :show.sync="shieldingShow" backdrop="false">
      <header slot="modal-header" class="modal-header text-center">
        <h4 class="modal-title">用户拉黑</h4>
      </header>
      <article slot="modal-body">
        <validator name='v'>
          <form novalidate class="form-horizontal select-overspread ">
            <div class="auto col-sm-11 col-md-offset-1"  style="margin-top: 10px;">
              <div class="col-sm-4 form-group" id="f_type"  :class="[$v.black.required ? 'has-error' : 'has-success']">
                <label for="f_type" style="margin-left: -13px;" class="font_normal_body" title="参数:黑名单类型">黑名单类型</label>
                <input type="text" class="input_search" style="width:60%" v-show="false" v-model="$refs.black.selectedItems"
                       v-validate:black='{required: true }'>
                <v-select :value.sync="shieldingmodel.f_type" v-model="shieldingmodel.f_type" width="53%"
                          :options='types' placeholder='黑名单类型' title="参数:黑名单类型"
                          :search="false" :value-single="true" close-on-select v-ref:black></v-select>
              </div>
            </div>

            <div class="auto col-sm-11 col-md-offset-1"  style="margin-top: 10px;">
              <div class="col-sm-12 form-group" id="f_comments" :class="[$v.f_comments.required ? 'has-error' : 'has-success']">
                <label for="f_comments" class=" font_normal_body"><nobr>原&emsp;&emsp;因</nobr></label>
                <input class="input_search" type="text" class="input_search" style="width:87%" v-model="shieldingmodel.f_comments"
                       placeholder="原因" v-validate:f_comments='{required: true }' >
              </div>
            </div>
          </form>
        </validator>
      </article>
      <footer slot="modal-footer" class="modal-footer">
          <button type="button" class="btn btn-default" @click='cancelShielding()'>取消</button>
          <button type="button" class="btn btn-success" @click='confirmShielding()' :disabled='!$v.valid'>确认</button>
      </footer>
    </modal>
  </div>
  <div  class="flex-row" style="width:99%;height:98%;margin-bottom:0px" v-if="showdetail">
    <user-info-detail-manage-new :f_userinfo_id="rowdata.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage-new>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  import UserGasEcharts from './UserGasEcharts'
  import plugin from 'system-clients/src/plugins/GetLoginInfoService'
  import {HttpResetClass} from 'vue-client'
  let readySomething = async function (self) {

    await self.$MagLoadParams.loadParam()
    self.initParams()
    self.$refs.paged.$refs.cri.model.notappear='不包含'
    self.$refs.paged.$refs.cri.model.SLday1=10
    self.$refs.paged.$refs.cri.model.f_user_state='正常'
    self.$refs.paged.$refs.cri.search()
    self.$refs.paged.$refs.grid.selectInit()
  }
  export default {
    title: '失联分析',
    props: ['data'],
    components: {UserGasEcharts},

    data() {
      return {
        model: new PagedList('api/af-revenue/sql/webmeter_getUserLoseQuery', 20),
        getfield:{
          "f_userinfo_code":"客户编号","f_user_name":"客户名称",
          "f_address":"客户地址","f_user_type":"客户类型","f_gasproperties":"用气性质",
          "f_meternumber":"表号",
          "f_user_phone":"联系电话",
          "f_balance_amount":"系统余额",

          "f_startuse_date":"启用日期",
          "initinsertdate":"初次上报日期",
          "lastinsertdate":"最后上报日期",
          "f_meter_base":"最后表读数",
          "slday":"失联天数",
          "f_meter_brand":"气表厂家","f_orgname":"组织机构"},
        reportStr: null,
        show: false,
        showdetail:false,
        rowdata:{},
        criteriaShow:false,
        shieldingShow: false,
        types: this.$appdata.getParam('黑名单类型'),
        outlets: [],
        operator: [],
        shieldingmodel: {
          f_type: '',
          f_comments: ''
        },
        filename: '',
        useinstitutions: [],
        userusenatures: [],
        WarningType: [
          {label: '全部', value: ''}
        ],
        Warningstyles: [
          {label: '全部', value: ''}
        ],
        styles:false,
        org:[this.$login.f.orgid],
        orgCondtionStr: ` and f_orgid = ${this.$login.f.orgid}`,
        echarts: false,
        searchshow: false,
        isActiveTotalForm: false,
        condition: '1=1',
        groupname: ['max_days_type'],
        getgroupname: [
          {label: '客户类型', value: 'f_user_type'},
          {label: '用气性质', value: 'f_gasproperties'},
          {label: '分公司', value: 'f_orgname'},
          {label: '最长失联天数', value: 'max_days_type'},
        ]
      }
    },
    ready() {
      readySomething(this)
      this.getMeterTypeAndBrand('f_meter_brand', '1=1')
    },

    methods: {
      userUseInstitutionChange () {
        this.userusenatures = []
        this.$refs.paged.$refs.cri.model.f_user_usenature = ''
        if (this.$refs.paged.$refs.cri.model.f_use_institution) {
          let str = this.$refs.paged.$refs.cri.model.f_use_institution + '使用性质'
          this.userusenatures = this.$appdata.getParam(str)
        }
      },
      userUseTypeChange () {
        this.useinstitutions = []
        this.$refs.paged.$refs.cri.model.f_use_institution = ''
        if (this.$refs.paged.$refs.cri.model.f_user_usetype) {
          let str = this.$refs.paged.$refs.cri.model.f_user_usetype + '机构'
          this.useinstitutions = this.$appdata.getParam(str)
        }
      },
      getMeterType() {
        this.Warningstyle = [{label: '全部', value: ''}]
        this.styles = false
        let f_meter_brand = this.$refs.paged.$refs.cri.model.f_meter_brand
        this.getMeterTypeAndBrand('f_meter_style', `f_meter_brand='${f_meter_brand}'`)
      },
      getMeterTypeAndBrand(groupname, condition) {
        condition = `${condition} and ui.f_orgid like '${this.$login.f.orgid}%'`
        let self = this
        let load = new HttpResetClass()
        load.load('POST', 'api/af-revenue/sql/getMeterStyleAndBrand1', {
          data: {
            condition: condition,
            groupname: groupname
          }
        }, {warnMsg: null, resolveMsg: null}).then((res) => {
          for (let i = 0; i < res.data.length; i++) {
            if (groupname == 'f_meter_brand') {
              let dr = {}
              dr.label = res.data[i].f_meter_brand
              dr.value = res.data[i].f_meter_brand
              //气表品牌
              self.WarningType.push(dr)
            } else if (groupname == 'f_meter_style') {
              self.Warningstyle= [
                {label: '全部', value: ''}
              ],
               self.$refs.paged.$refs.cri.model.f_meter_style=''
              let dr = {}
              dr.label = res.data[i].f_meter_style
              dr.value = res.data[i].f_meter_style
              //气表类型
              self.Warningstyle.push(dr)
            }
          }
          if (self.Warningstyle.length > 1) {
            self.showMeterStyle()
          }
        })

      },
      showMeterStyle() {
        this.styles = true
      },
      hidden(){
        this.criteriaShow = !this.criteriaShow
      },
      showmsg(obj){
        this.rowdata=obj
        this.showdetail=true
      },
      cancel() {
        this.showdetail = false
      },
      clickshow() {
        this.searchshow = !this.searchshow
      },
      search(args) {
        this.condition = this.$refs.paged.$refs.cri.condition + `${this.orgCondtionStr}`
        args.condition = this.condition
        if(this.$refs.paged.$refs.cri.model.notappear) {
          if (this.$refs.paged.$refs.cri.model.notappear === '不包含') {
            args.condition = args.condition + " and initinsertdate <> '开户未上报'"
          }
        }
        this.model.search(args.condition, args.model)
      },
      getorg(obj) {
        if (obj.resids.length>0) {
          this.orgCondtionStr = " and f_orgid in " + plugin.convertToIn(obj.resids)
        }else
        {
          this.orgCondtionStr = " and f_orgid = " + this.$login.f.orgid
        }
      },
      clearmsg() {
        this.$refs.paged.$refs.cri.model = {}
      },
      shielding() {
        if(!(this.$refs.paged.$refs.grid.getRowData().length > 0)) {
          this.$showAlert('请至少选择一项', 'warning', 1500)
          return
        }
        this.shieldingShow = true
      },
      cancelShielding() {
        this.shieldingShow = false
        this.$refs.paged.$refs.grid.selectInit()
      },
      async confirmShielding() {
        let data = {
          rows: this.$refs.paged.$refs.grid.getRowData(),
          f_operat_type: '拉黑',
          f_operator: this.$login.f.name,
          f_operatorid: this.$login.f.id,
          f_orgid: this.$login.f.orgid,
          f_orgname: this.$login.f.orgs,
          f_depid: this.$login.f.depids,
          f_depname: this.$login.f.deps,
          f_operate_date: this.$login.toStandardTimeString(),
          f_userinfo_id: {
            f_userinfo_id: this.model.f_userinfo_id,
            f_user_state: '正常',
            version: this.model.version
          },
          shieldingmodel: this.shieldingmodel
        }
        console.log(data)
        await this.$resetpost('rs/logic/batchShielding',data, {resolveMsg: '拉黑成功', rejectMsg: '拉黑失败'})
        this.cancelShielding()
      },
      close() {
        this.show = false
      },
      initParams () {
        let arr = []
        this.$MagGetSaleParam.getGasbrand().forEach((item) => {
          let temp = {}
          if(item.value.f_meter_type==='物联网表'){
            temp.label = item.label
            temp.value = item.value.f_meter_brand
            arr.push(temp )
          }

        })
        this.meterbrands = [{label: '全部', value: ''}, ...arr]
      },
      // 显示柱状图
      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
      }
    },
    watch: {
      'data'(val) {
        if (val.f_files_path) {
          this.getFileContent(val.f_files_path)
        } else {
          this.reportStr = null
        }
      }
    },
    computed: {
      // usertypes () {
      //   return this.$appdate.getParams("客户类型")
      // },
      getCondition() {
        let condition=this.$refs.paged.$refs.cri.condition + this.orgCondtionStr
        if(this.$refs.paged.$refs.cri.model.notappear) {
          if (this.$refs.paged.$refs.cri.model.notappear === '不包含') {
            condition = condition + " and initinsertdate <> '开户未上报'"
          }
        }
        return {

          condition: condition
        }
      },
      userusetypes () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用气类型')]
      },
      usertypes() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      Gasproperties() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用气性质')]
      },
      f_outlets() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('网点查询用')]
      },
      valveStatus() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气表类型')]
      },
      not_appear(){
        return [{label: '包含', value: '包含'},{label: '不包含', value: '不包含'} ]
      },
      getstate() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('最新状态')]
      },
      userstate() {
        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;
    /*background: #00A3F0;*/
    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>
