<template>
  <div class="auto">
    <div class="auto">
      <criteria-paged :model="model" v-ref:paged>
        <criteria partial='criteria' @condition-changed='search' v-ref:cri>
          <div partial>
            <form>
                <div class="row col-xs-12 col-sm-12 col-md-6 app-row">
                <div class="row col-xs-12 col-sm-6">
                  <div class="col-xs-4 col-sm-4 col-md-4">
                    <img src="../../assets/档案信息.png" style="width: 20px;margin-bottom: 5px" alt="">
                    <label for="f_plan_name" class="font text-left">小区名称：</label>
                  </div>
                  <div class="col-xs-8 col-sm-8" >
                    <input id="f_plan_name" class="search_input input-font"
                           v-model="model.f_residential_area" condition="f_residential_area like '%{}%'" />
                  </div>
                </div>
                  <div class="row col-xs-12 col-sm-6">
                    <div class="col-xs-4 col-sm-4 col-md-4">
                      <img src="../../assets/档案信息.png" style="width: 20px;margin-bottom: 5px" alt="">
                      <label for="old_id" class="font text-left">小区ID：</label>
                    </div>
                    <div class="col-xs-8 col-sm-8" >
                      <input id="old_id" class="search_input input-font"
                             v-model="model.old_id" condition="old_id = '{}'" />
                    </div>
                  </div>
                <div class="row col-xs-12 col-sm-6">
                  <div class="col-xs-4 col-sm-4 col-md-4">
                    <img src="../../assets/档案信息.png" style="width: 20px;margin-bottom: 5px" alt="">
                    <label for="f_plan_name" class="font text-left">街道：</label>
                  </div>
                  <div class="col-xs-8 col-sm-8">
                    <input id="f_plan_name" class="search_input input-font"
                           v-model="model.f_street" condition="f_street like '%{}%'" />
                    <!--<v-select id="f_entry"-->
                              <!--placeholder='计划类型'-->
                              <!--:value.sync="model.f_plan_type"-->
                              <!--:value-single="true"-->
                              <!--:options='$parent.$parent.types'-->
                              <!--v-model="model.f_plan_type"-->
                              <!--condition="f_plan_type = '{}'"-->
                              <!--close-on-select clear-button> </v-select>-->
                  </div>
                </div>

              </div>
              <div class="row app-row">
              </div>
              <div class="row col-xs-12 col-sm-12 col-md-6 text-center" style="margin-top: 20px;">
                <button type="button" class="btn btn-lg btn-font btn-color" style="width: 45%;" @click="search">查询</button>
              </div>
              <!--<div class="row col-xs-12 col-sm-12 col-md-6 app-btn">-->
              <!--<button class="btn btn-primary col-xs-6" type="button"  @click="search"><span class="glyphicon  glyphicon-search" style="margin-right: 20px;">查询</span></button>-->
              <!--</div>-->
              <div style="height:30px;"></div>
            </form>
          </div>
        </criteria>
        <list :model="model" partial='list'>
          <div partial>
            <div class="auto app-text"  @click="$parent.$parent.$parent.inspect(row)" style="margin-top: 5px;" >
              <div class="panel" style="padding: 10px 10px 5px 10px;">
                <div class="panel-body panel-self">
                  <div class="row col-xs-12 col-sm-12 col-md-6">
                    <p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">小区名称</p>
                    <p class="panel-title col-xs-8 text-left input-font">{{ row.f_residential_area }}</p>
                  </div>
                  <div class="row col-xs-12 col-sm-12 col-md-6">
                    <p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">小区ID</p>
                    <p class="panel-title col-xs-8 text-left input-font">{{ row.old_id }}</p>
                  </div>
                  <div class="row col-xs-12 col-sm-12 col-md-6">
                    <p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">街道</p>
                    <p class="panel-title col-xs-8 text-left input-font">{{row.f_street}}</p>
                  </div>
                  <!--<div class="row col-xs-12 col-sm-12 col-md-6">-->
                    <!--<p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">计划类型</p>-->
                    <!--<p class="panel-title col-xs-8 text-left input-font">{{ row.f_plan_type }}</p>-->
                  <!--</div>-->
                  <!--<div class="row col-xs-12 col-sm-12 col-md-6">-->
                  <!--<p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">计划总数</p>-->
                  <!--<p class="panel-title col-xs-8 text-left input-font">{{ row.item_counts }}</p>-->
                <!--</div>-->
                  <!--<div class="row col-xs-12 col-sm-12 col-md-6">-->
                    <!--<p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">下发时间</p>-->
                    <!--<p class="panel-title col-xs-8 text-left input-font">{{ row.f_issue_time }}</p>-->
                  <!--</div>-->
                  <!--<div class="row">-->
                    <!--<p class="panel-title col-xs-4 col-sm-4 col-md-4 text-left font">下发人员</p>-->
                    <!--<p class="panel-title col-xs-8 text-left input-font">{{ row.f_send_operator }}/{{row.f_plan_month}}</p>-->
                  <!--</div>-->
                  <div class="row col-xs-12 col-sm-12 col-md-6 text-right">
                    <button type="button" class="btn yybtn-color" v-on:click.stop.prevent='$parent.$parent.$parent.addPlanItem(row)'>新增楼栋</button>&nbsp;
                    <button type="button" class="btn yybtn-color" v-on:click.stop.prevent='$parent.$parent.$parent.deletePlan(row)' v-if="!$parent.$parent.$parent.isDown(row)">清空</button>&nbsp;
                    <button type="button" class="btn yybtn-color" v-on:click.stop.prevent='$parent.$parent.$parent.createUserInfo(row)' v-if="!$parent.$parent.$parent.isDown(row)">生成档案</button>&nbsp;
                  </div>
                </div>
              </div>

            </div>
          </div>
        </list>
      </criteria-paged>
    </div>
    <!--<div class="row text-center" style="margin-top: 20px;">-->
      <!--<button type="button" class="btn btn-lg btn-font btn-color" style="width: 45%;" @click="downHand()">共享计划下载</button>-->
      <!--&lt;!&ndash;<button type="button" class="btn btn-lg btn-font btn-color" style="width: 45%;" @click="database()">数据库导出</button>&ndash;&gt;-->
    <!--</div>-->
    <!-- <div class="auto" style="padding-top: 20px;" v-if='show'>
      <p class="text-warning" style="padding: 0 10px">正在下载抄表单...</p>
    </div> -->
    <!-- <hand-info v-if="show" :data="handinfo"></hand-info> -->
    <!-- <div class="auto" v-if="!show">
      <p class="text-warning" style="padding: 0 10px">
        暂无可操作的抄表信息
      </p>
    </div> -->
    <modal :show.sync="showAddPlan" v-ref:modal backdrop="false">
      <div slot="modal-header" class="modal-header">
      <span class="modal-title font">
        新增楼栋信息
      </span>
      </div>
      <div slot="modal-body" class="modal-body">
        <div style="width: 95%;margin: auto">
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">街道</label>
            <div class="col-xs-8">
              <input type="text"  class="form-control" maxlength="100" v-model="addPlan.f_street" readonly>
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">小区</label>
            <div class="col-xs-8">
              <input type="text"  class="form-control" maxlength="100" v-model="addPlan.f_residential_area" readonly>
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">起始楼号</label>
            <div class="col-xs-8">
              <input type="text"  class="form-control" maxlength="100" v-model="addPlan.f_building_min" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">截止楼号</label>
            <div class="col-xs-8">
              <input type="text"  class="form-control" maxlength="100" v-model="addPlan.f_building_max" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">起始单元</label>
            <div class="col-xs-8">
              <input type="text"  class="form-control" maxlength="100" v-model="addPlan.f_unit_min" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">截止单元</label>
            <div class="col-xs-8">
              <input type="text"  class="form-control" maxlength="100" v-model="addPlan.f_unit_max" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">起始楼层</label>
            <div class="col-xs-8">
              <input type="number"  class="form-control" maxlength="100" v-model="addPlan.f_floor_min" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">截止楼层</label>
            <div class="col-xs-8">
              <input type="number"  class="form-control" maxlength="100" v-model="addPlan.f_floor_max" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">起始房号</label>
            <div class="col-xs-8">
              <input type="number"  class="form-control" maxlength="100" v-model="addPlan.f_room_min" >
            </div>
          </div>
          <div class="row"  style="margin-top: 6px" >
            <label class="col-xs-4 control-label font text-left">截止房号</label>
            <div class="col-xs-8">
              <input type="number"  class="form-control" maxlength="100" v-model="addPlan.f_room_max" >
            </div>
          </div>
          <!-- TODO  暂时默认长安公司 -->
          <!--<div class="row"  style="margin-top: 6px" >-->
            <!--<label class="col-xs-4 control-label font text-left">所属公司</label>-->
            <!--<div class="col-xs-8">-->
              <!--&lt;!&ndash;<input type="number"  class="form-control" maxlength="100" v-model="addPlan.f_room_max" >&ndash;&gt;-->
              <!--<v-select :value.sync="addPlan.f_subunit"-->
                        <!--:options='subcompanys' placeholder='请选择分公司'-->
                        <!--:value-single="true"-->
                        <!--v-model="addPlan.f_subunit"  close-on-select clear-button>-->

              <!--</v-select>-->
            <!--</div>-->
          <!--</div>-->
        </div>
      </div>
      <div slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-danger" @click="clean()">清除</button>
        <button type="button" class="btn btn-cancel" @click="cancel()">取消</button>
        <button type="button" class="btn btn-save" @click="save()">确定</button>
      </div>
    </modal>

    <back-page :need-back='true'  @flag="$back()"></back-page>
  </div>

</template>
<script>
  import AppData from '../../stores/AppData'
  import { PagedList } from 'vue-client'
  import * as Util from '../Util'
  import co from 'co'
  import Vue from 'vue'
  // const downloadHand = function * (self, row) {
  //   let res = yield self.$showMessage('正在进行安检计划下载，请确认！', ['confirm', 'cancel'])
  //   let flag = false
  //   if (res === 'confirm') {
  //     flag = true
  //   }
  //   if (flag) {
  //     HostApp.__this__ = self
  //     self.logicWithHint('DownCheckPlanInfo', {
  //       id: row.id
  //     }, 'javascript:HostApp.__this__.downloadCallBack()', 1)
  //     //安卓端
  //     //PC端模拟安卓
  //     // if (result.data.state === 0) {
  //     //   console.log(result.data.data)
  //     //   if (result.data.data.length > 0) {
  //     //     self.handcount = result.data.data.length
  //     //     self.handinfo = [...result.data.data]
  //     //     self.$showMessage(`下载成功`)
  //     //     self.reload()
  //     //   } else {
  //     //     self.$showMessage('无可下载的共享计划！')
  //     //   }
  //     // } else {
  //     //   self.$showMessage('下载失败！')
  //     // }
  //   }
  // }

  export default {
    title: '计划下载',
    data () {
      return {
        model: new PagedList( `${this.$androidUtil.getProxyUrl()}/rs/sql/SafecheckGetAreaInfo`,20,
          {}),
        checkPlanInfo: [],
        localArea:[],
        addPlan:{
          f_building_min:'',
          f_building_max:'',
          f_street:null,
          f_residential_area:null,
          f_unit_min:'',
          f_unit_max:'',
          f_floor_min:'',
          f_floor_max:'',
          f_room_min:'',
          f_room_max:'',
          f_area_id:0,
          f_create_time:'',
          f_is_efficient:'否',
          f_subunit:'',
          op_spot:'',
          // f_sum:0
        },
        subcompanys:[
          {label: '请选择分公司', value: ''},
          {label: '西安市惠安天然气销售有限公司', value: '西安市惠安天然气销售有限公司'},
          {label: '西安市长安天然气有限公司', value: '西安市长安天然气有限公司'},
          {label: '西安市安泰新瑞天然气有限公司', value: '西安市安泰新瑞天然气有限公司'},
          {label: '西安市沣瑞天然气有限公司', value: '西安市沣瑞天然气有限公司'}
        ],
        addPlans:[],
        showAddPlan:false,
        row:''
      }
    },
    ready () {
      // Vue.user.f_fengongsi = '中海油.海南分公司'
      this.getlocalArea()
    },
    methods: {
      clean(){
        let newAddplan = {
          f_building_min:'',
          f_building_max:'',
          f_unit_min:'',
          f_unit_max:'',
          f_floor_min:'',
          f_floor_max:'',
          f_room_min:'',
          f_room_max:'',
          // f_sum:0
        }
        Object.assign(this.addPlan,this.addPlan,newAddplan)
        console.log('this.addPlan===>'+JSON.stringify(this.addPlan))
      },
      inspect(row){
        console.log('rowrow:row',row)
        let value = this.$androidUtil.path({alias: 'getLocalAreaById', data:{f_area_id:row.id}})
        console.log(value)
        if(value.data.length>0){
        this.$goto('local-area-info', {item: row}, 'self', this.reload)
        }else{
          this.$showMessage('本地暂无楼栋信息！')
        }
      },
      reload(){
        console.log('页面刷新 ')
        this.$refs.paged.loadPage(this.$refs.paged.model.pageIndex)
      },
      getlocalArea(){
        let value = this.$androidUtil.path({alias: 'getLocalArea', data:{}})
        console.log(value)
        this.localArea = value.data
      },
      addPlanItem (row) {
        this.showAddPlan = true
        this.row = row
        if(this.row.f_residential_area != " ")
          this.addPlan.f_residential_area = this.row.f_residential_area
        if(this.row.f_street != " ")
          this.addPlan.f_street = this.row.f_street
      },
      save(){
        let isNumber = (str) => {return Number(str) != NaN}
        if(!(isNumber(this.addPlan.f_building_max)) || !(isNumber(this.addPlan.f_building_min))){
          if(this.addPlan.f_building_max != this.addPlan.f_building_min){
            this.$showMessage('楼号为非数字时，最大楼号和最小楼号需一致')
            return
          }
        }
        if(!(isNumber(this.addPlan.f_unit_max)) && !(isNumber(this.addPlan.f_unit_min))){
          if(this.addPlan.f_unit_max != this.addPlan.f_unit_min){
            this.$showMessage('单元为非数字时，最大单元和最小单元需一致')
            return
          }
        }
        this.addPlan.f_area_id = this.row.id
        this.addPlan.f_create_time = Util.toStandardTimeString()
        //TODO 暂定默认长安公司
        this.addPlan.f_subunit = '长安公司'
        // if(this.addPlan.f_subunit == ''){
        //   this.$showMessage('请选择分公司!')
        //   return
        // }
        // this.addPlan.f_sum = (this.addPlan.f_building_max - this.addPlan.f_building_min + 1) * (this.addPlan.f_unit_max - this.addPlan.f_unit_min + 1) * (this.addPlan.f_floor_max - this.addPlan.f_floor_min + 1) * (this.addPlan.f_room_max - this.addPlan.f_room_min + 1)
        console.log('isNaN(this.addPlan.f_building_max)==>'+isNaN(this.addPlan.f_building_max))
        console.log('isNaN(this.addPlan.f_building_min)==>'+isNaN(this.addPlan.f_building_min))
        console.log('this.addPlans===>'+JSON.stringify(this.addPlans))
        HostApp.__this__ = this
        this.logicWithHint('CreateLocalArea', {
          // addPlans:this.addPlans,
          addPlan:this.addPlan
        }, 'javascript:HostApp.__this__.CreateAreaCallBack()', 1)
      },
      cancel(){
        this.row = ''
        this.showAddPlan = false
        this.addPlans = []
      },
      createUserInfo(row){
        this.$showMessage('确认生成该小区档案？', ['confirm', 'cancel']).then((res)=>{
          if(res == 'confirm'){
            HostApp.__this__ = this
            this.logicWithHint('CreateUserInfo', {
              id: row.id,
              man:Vue.user.name,
              op_id:Vue.user.ename
            }, 'javascript:HostApp.__this__.createUserCallBack()', 1)
          }
        })
      },
      deletePlan (row) {
        this.$showMessage('确认删除该小区楼栋信息？', ['confirm', 'cancel']).then((res)=>{
          if(res == 'confirm'){
            HostApp.__this__ = this
            this.logicWithHint('DeleteLocalArea', {
              f_area_id: row.id
            }, 'javascript:HostApp.__this__.deleteAreaCallBack()', 1)
          }
        })
      },
      createUserCallBack(jo){
        if (jo.state == 'ok') {
          let result = JSON.parse(jo.result)
          if (result.code === 200 && result.result == '接收成功') {
            this.$showMessage('建档成功！')
          }else if(result.code === 404){
            this.$showMessage('暂无可生成的楼栋信息！')
          } else {
            this.$showMessage('建档失败，'+result.result)
          }
        } else {
          this.$showMessage('请求数据失败，请检查网络状况是否正常！')
        }
        this.getlocalArea()
      },
      deleteAreaCallBack(jo){
        if (jo.state == 'ok') {
          let result = JSON.parse(jo.result)
          if (result.code === 200) {
            this.getlocalArea()
            this.$showMessage(`删除成功！`)
          } else {
            this.$showMessage('删除失败！')
          }
        } else {
          this.$showMessage('服务器内部错误,删除失败！')
        }
      },
      CreateAreaCallBack(jo){
        if (jo.state == 'ok') {
          let result = JSON.parse(jo.result)
          if (result.code === 200) {
            this.cancel()
            this.getlocalArea()
            this.$showMessage(`新增成功！`)
          } else {
            this.$showMessage('新增失败！')
          }
        } else {
          this.$showMessage('服务器内部错误,新增失败！')
        }
      },
      logicWithHint(logic, data, callback, backresult) {
        HostApp.logicWithHint({
          // logic别名，key必须为logic
          'logic': logic,
          // 回调执行方法名，key必须为callback
          'callback': callback,
          // logic执行需要的业务参数
          'data': data,
          // 固定key，代表是否将logic执行结果作为参数传入回调方法，1代表true，0代表false，
          // 执行回调方法传入key为backresult
          // 如需额外拼接参数，写在logic返回值的params，例如xxlogic返回:{"params": "success"}
          'backresult': backresult
        })
      },
    },
    computed:{
      isDown(){
        return function (row) {
          console.log('当前小区===》'+JSON.stringify(row))
          for (let info of this.localArea) {
            console.log('infoinfoinfo===>'+JSON.stringify(info))
            if(info.f_area_id == row.id){
              return false
            }
          }
          return true
        }
      }
    }
  }
</script>
<style>
  .bg {
    background-color: blue;
    height: 1px;
    border: 0;
  }
  .app-row {
    background-color: white;
    padding: 10px 10px 0 10px;
    border-bottom: 1px solid rgba(235, 235, 235, 0.5);
  }
  .search_input {
    border: 0;
    outline: none;
  }
  .font{
    font: 15px PingFang-SC-Medium;
    color: #666666;
  }
  .input-font{
    font: 15px PingFang-SC-Medium;
    color: #333333;
  }
  .btn-font{
    font:600 16px PingFang-SC-Bold;
    color: #499EDF;
  }
  .btn-color{
    background-color: #FFFFFF;
    border-radius: 10px ;
    border: 1px solid #499EDF;
  }
  .app-text {
    font-size: 12px;
  }
  .panel-self{
    border-radius: 10px;
    border:1px solid #499EDF;
    background-color: #F8F8F8;
  }
  .yybtn-color{
    background-color:#499edf;
    border-radius: 4px ;
    border: 1px solid #499EDF;
    color: #FFFFFF;
    font: 14px PingFang-SC-Bold;
  }
  .qxbtn-color{
    background-color: #FFFFFF;
    border-radius: 4px ;
    color: #499edf;
    font: 14px PingFang-SC-Bold;
    border: 1px solid #499EDF;
  }

</style>
