<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto row" partial>
          <div class="col-sm-4 form-group form-input-group" style="width: 20%">
            <label class="control-label">小区名称</label>
            <input type="text" class="form-control" v-model="model.f_residential_area"
                   style="height:34px;margin-top: 8px" placeholder="小区名称" condition="cp.f_residential_area = '{}'">
          </div>
          <div class="col-sm-4 form-group form-input-group" style="width: 20%">
            <label class="control-label">小区地址</label>
            <input type="text" class="form-control" v-model="model.f_address"
                   style="height:34px;margin-top: 8px" placeholder="小区地址" condition="area.f_area_address like '{}%'">
          </div>
          <div class="col-sm-4 form-group form-input-group" style="width: 20%">
            <label class="control-label">行政区域</label>
            <v-select id="f_admin_area"
                      placeholder='行政区域' style="margin-top: 8px"
                      v-model='model.f_admin_area'
                      :value.sync="model.f_admin_area"
                      :options='$parent.$parent.adminAreas'
                      condition="cp.f_distinct = '{}'"
                      close-on-select clear-button>
            </v-select>
          </div>
          <div class="col-sm-4 form-group form-input-group" style="width: 20%">
            <label class="control-label">运行区域</label>
            <v-select id="f_distinct"
                      placeholder='运行区域' style="margin-top: 10px"
                      v-model='model.f_opera_area'
                      :value.sync="model.f_opera_area"
                      :options='$parent.$parent.distincts'
                      condition="cp.f_opera_area = '{}'"
                      close-on-select clear-button>
            </v-select>
          </div>
          <div class="col-sm-4 form-group form-input-group" style="width: 20%">
            <label class="control-label">开始时间</label>
            <datepicker
              :value.sync="model.f_check_start"
              placeholder='安检开始时间' style="margin-top: 8px"
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd'"
              :show-rest-button="reset"
              v-model = "model.f_check_start"
              condition="cp.f_offsite_time >= '{} 00:00:00'">
            </datepicker>
          </div>
          <div class="col-sm-4 form-group form-input-group" style="width: 20%">
            <label class="control-label">结束时间1</label>
            <datepicker
              :value.sync="model.f_check_end"
              placeholder='安检结束时间' style="margin-top: 8px"
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd'"
              :show-rest-button="reset"
              v-model = "model.f_check_end"
              condition="cp.f_offsite_time <= '{} 23:59:59'">
            </datepicker>
          </div>
          <div class="row">
            <div class="span" style="margin-top:8px;float:right">
              <button class="button_spacing button_search" @click="search()">查询</button>
            </div>
          </div>

          <modal :show.sync="$parent.$parent.showModal" v-ref:modal>
            <div slot="modal-header" class="modal-header">
              <h4 class="modal-title">
                选择计划
              </h4>
            </div>
            <div slot="modal-body" class="modal-body">
              <plan-chooser :f.sync="$parent.$parent.fun"  v-ref:chooser></plan-chooser>
            </div>
            <div slot="modal-footer" class="modal-footer">
              <button  class="btn btn-success width-80" @click="$parent.$parent.ok">确认</button>
              <button  class="btn btn-success width-80" @click="$parent.$parent.cancel">取消</button>
            </div>
          </modal>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>小区名称</th>
            <th>小区编号</th>
            <th>小区地址</th>
            <th>行政区域</th>
            <th>运行区域</th>
            <th>通气点火户数</th>
            <th>DN15</th>
            <th>DN20</th>
            <th>DN25</th>
            <th>DN32</th>
            <th>DN40</th>
            <th>DN50</th>
            <th>DN80</th>
          </tr>
        </template>
        <template partial='body'>
          <td>{{row.f_residential_area}}</td>
          <td>{{row.f_residential_area_id}}</td>
          <td>{{row.f_area_address}}</td>
          <td>{{row.f_distinct}}</td>
          <td>{{row.f_opera_area}}</td>
          <td>{{row.con}}</td>
          <td>{{row.dn15}}</td>
          <td>{{row.dn20}}</td>
          <td>{{row.dn25}}</td>
          <td>{{row.dn32}}</td>
          <td>{{row.dn40}}</td>
          <td>{{row.dn50}}</td>
          <td>{{row.dn80}}</td>
        </template>
      </data-grid>
    </criteria-paged>
    <check-msg v-if="showcheckmsg" :show="showcheckmsg" :data="checkInfo"></check-msg>
  </div>
</template>

<script>
import { PagedList } from 'vue-client'
import Vue from 'vue'
import co from 'co'
import AppData from '../../stores/AppData'
import * as Util from '../Util'
import {toStartDateString, toStartDateYearString} from "../Util";

//获取json配置文件
let asyncReady = async function(self) {
  // 获取配置信息
  try {
    let res = await Vue.resetget(`/phone/rs/vue`,{data: {}},{resolveMsg: null, rejectMsg: null})
    Vue.config = res.data
  } catch (error) {
    // 忽略704,文件找不到异常R
    if (error.status !== 704) {
      throw error
    }
  }
}

let select = function * (self) {
  let getGasman = yield self.$resetpost('/rs/search', {
    source: 'this.getParentByType($organization$).getChildByName($安检员$).getUsers()',
    userid: self.$login.f.id
  }, {resolveMsg: null, rejectMsg: null})
  console.log('获取安检员', getGasman)
  getGasman.data.forEach((checker)=>{
  self.checkersid.push({label:checker.name,value:checker.id})
  self.checkersname.push({label:checker.name,value:checker.name})
})
  self.checkersname.push({label:'全部',value:''})
  self.checkersid.push({label:'全部',value:''})
}

export default {
  title: '小区安检统计',
  data () {
    let model = new PagedList('rs/sql/safe_getPipeByArea', 20, {f_plan_id: 'this.f_check_plan.id', f_approved: 'this.f_approved[0]', f_entry_status: 'this.f_entry[0]', f_no_checkplan:'this.f_no_checkplan[0]',f_filialeids: 'this.f_filialeids'})
    model.f_check_plan = {f_plan_name: '', id: ''}
    model.f_approved = ['']
    model.f_entry = ['']
    model.f_no_checkplan = ['']
    model.f_filialeids = this.$login.f.f_orgids
    model.f_subcompany = this.$login.f.f_fengongsi
    return {
      model:model,
      fun:this.$login.f,
      adminAreas: [
        {label: '全部', value: ''}
      ],
      residentialProperties: [
        {label: '物业小区', value: '物业小区'},
        {label: '企事业单位', value: '企事业单位'},
        {label: '安置小区', value: '安置小区'},
        {label: '自建房', value: '自建房'},
        {label: '公租房', value: '公租房'},
        {label: '廉租房', value: '廉租房'}
      ],
      distincts: [
        {label: '全部', value: ''},
        {label: '航天', value: '航天'},
        {label: '韦曲', value: '韦曲'},
        {label: '高新', value: '高新'},
        {label: '沣东', value: '沣东'},
        {label: '中心站', value: '中心站'},
        {label: '沣西', value: '沣西'},
        {label: '浐灞生态区', value: '浐灞生态区'}
      ],
      heating_style: [
        {label: '壁挂炉供暖', value: '壁挂炉供暖'},
        {label: '集中供暖', value: '集中供暖'},
        {label: '其他', value: '其他'},
        {label: '燃气取暖器供暖', value: '燃气取暖器供暖'}
      ],
      checkersid:[],
      checkersname:[],
      showModal: false,
      f_entry_status: [
        {label: '全部', value: ''},
        {label: '正常', value: '入户'},
        {label: '无人', value: '到访不遇'},
        {label: '拒检', value: '拒检'}
      ],
      f_checkplans: [
      {label: '全部', value: ''},
      {label: '集中安检单', value: '集中安检单'},
      {label: '抄表单', value: '抄表单'},
      {label: '无计划安检', value: '无计划安检'},
      {label: '点火通气单', value: '点火通气单'},
      {label: '预约安检单', value: '预约安检单'},
      {label: '预约开户单', value: '预约开户单'},
      {label: '预约过户单', value: '预约过户单'},
      {label: '重点用户跟进单', value: '重点用户跟进单'},
      {label: '一级隐患跟进单', value: '一级隐患跟进单'},
      {label: '隐患跟进单', value: '隐患跟进单'},
      {label: '偷盗气用户跟进单', value: '偷盗气用户跟进单'},
      {label: '临街商铺安检单', value: '临街商铺安检单'},
      {label: '隐患用户查询跟进', value: '隐患用户查询跟进'},
      {label: '重点用户查询跟进', value: '重点用户查询跟进'},
      {label: '大锅灶安检单', value: '大锅灶安检单'}
    ],
      keyuser: [
        {label: '全部', value: ''},
        {label: '职工用户', value: '职工用户'},
        {label: '外联单位用户', value: '外联单位用户'},
        {label: '村内餐饮', value: '村内餐饮'},
        {label: '否', value: '否'},
        {label: '民改商', value: '民改商'},
        {label: '孤寡老人', value: '孤寡老人'},
        {label: '重大隐患', value: '重大隐患'},
        {label: '气量出入', value: '气量出入'},
        {label: '临街商铺', value: '临街商铺'},
        {label: '小区大锅灶', value: '小区大锅灶'},
      ],
      yesorno: [
        {label: '全部', value: ''},
        {label: '是', value: '是'},
        {label: '否', value: '否'}
      ],
      showcheckmsg:false,
      checkInfo:{}
    }
  },
  methods: {
    search(){
      console.log('this.$refs.paged.$refs.criteria.condition',this.$refs.paged.$refs.criteria.condition)
      let con=this.$refs.paged.$refs.criteria.condition
      if(!this.$refs.paged.$refs.criteria.model.f_no_checkplan){
        con=`${this.$refs.paged.$refs.criteria.condition} and f_no_checkplan in ('集中安检单','无计划安检','预约安检单','重点用户跟进单','预约开户单','一级隐患跟进单','隐患跟进单','偷盗气用户跟进单','临街商铺安检单','有计划安检','点火通气单')`
      }
      this.model.search({condition:con,f_plan_id: this.model.f_check_plan.id})
    },
    change (row) {
      if(row.f_no_checkplan == '抄表单'){
        this.$goto('read-meter-message', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
      }else{
        Vue.$login.jwt =  775
        asyncReady(this).then(()=> {
          row.f_upload_state = '已传'
          this.$goto('paper-message', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
        }).catch(
          (respanse) => {
            alert('获取配置文件失败')
          })
      }
    },
    readConfig () {
      let gen =  asyncReady(this)
      return co(gen)
    },
    showChooser () {
      this.showModal = true
    },
    refresh() {
      this.$refs.paged.$refs.criteria.search()
      console.log('modle',this.model)
    },
    ok () {
      this.showModal = false
      if(this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id) {
        this.model.f_check_plan = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow
      }
    },
    cancel () {
      this.showModal = false
      this.model.f_check_plan = {f_plan_name: '', id: ''}
    },
    print(row){
      this.showcheckmsg = true
      this.checkInfo = row
    }
  },
  ready(){
    this.$refs.paged.$refs.criteria.model.f_check_start=Util.toStartDateYearString()
    this.adminAreas = this.$appdata.getParam("行政区域")
    this.function = this.f
    asyncReady(this).then(()=> {
      let gen = select(this)
      co(gen)
    })
  },
  watch:{
    'model.f_no_checkplan'(val){
      if(this.model.f_no_checkplan[0] == '集中安检单'){
        this.showChooser()
      }
    }
  }
}
</script>
