<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>

<template>
  <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 app-row">
              <img src="../../assets/小区.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label for="f_residential_area" >当前小区：{{$parent.$parent.f_residential_area}}</label>
            </div>
            <div class="row app-row">
              <div class="col-xs-4">
                <img src="../../assets/用户ID.png" style="width: 20px;margin-bottom: 5px" alt="">
                <label for="f_userinfo_code" class="font text-left">用户编号：</label>
              </div>
              <div class="col-xs-8" >
                <input id="f_userinfo_code" class="search_input input-font"
                       v-model=model.f_userinfo_code condition="f_userinfo_code like '%{}%'" />
              </div>
            </div>
            <div class="row app-row">
              <div class="col-xs-4">
                <img src="../../assets/用户ID.png" style="width: 20px;margin-bottom: 5px" alt="">
                <label  class="font text-left">表号：</label>
              </div>
              <div class="col-xs-8" >
                <input class="search_input input-font"
                       v-model=model.f_meter_no condition="f_meter_no like '%{}%'" />
              </div>
            </div>
            <div class="row app-row">
              <div class="col-xs-4">
              <img src="../../assets/用户姓名.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label for="f_user_name" class="font text-left">用户姓名：</label>
              </div>
              <div class="col-xs-8" >
              <input id="f_user_name" class="search_input input-font"
              v-model=model.f_user_name condition="f_user_name like '%{}%'" />
              </div>
            </div>
            <div class="row app-row">
              <div class="col-xs-4">
                <img src="../../assets/用户类型.png" style="width: 20px;margin-bottom: 5px" alt="">
                <label for="f_user_type" class="font text-left">用户类型：</label>
              </div>
              <v-select id="f_user_type" :value.sync="model.f_user_type"
                        :options='$parent.$parent.user_types' placeholder='请选择用户类型'
                        v-model="model.f_user_type" condition="f_user_type='{}'" close-on-select clear-button></v-select>
            </div>
            <div class="row app-row">
              <img src="../../assets/小区.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label class="font text-left">用户地址：</label>
              <input  id="f_keyword" class="search_input input-font"
                      v-model=model.f_keyword condition="f_address like '%{}%'" />
            </div>

            <div class="row app-row">
              <img src="../../assets/小区.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label class="font text-left">楼&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：</label>
              <input  id="f_building" class="search_input input-font"
                      v-model=model.f_building condition="ti.f_building like '%{}%'" />
            </div>
            <div class="row app-row">
              <img src="../../assets/小区.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label class="font text-left">单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;元：</label>
              <input  id="f_unit" class="search_input input-font"
                      v-model=model.f_unit condition="ti.f_unit like '%{}%'" />
            </div>
            <div class="row app-row">
              <div class="col-xs-4">
              <img src="../../assets/是否已检.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label for="f_state" class="font text-left">是否已检：</label>
                </div>
              <div class="col-xs-8" >
              <v-select id="f_state" :value.sync="model.f_state"
              :options='$parent.$parent.check_states' placeholder='请选择是否已检'
              v-model="model.f_state" condition="f_state='{}'" close-on-select clear-button></v-select>
                </div>
        </div>
            <div class="row app-row">
              <div class="col-xs-4">
              <img src="../../assets/安检状态.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label for="f_last_state" class="font text-left">安检状态：</label>
                </div>
              <v-select id="f_last_state" :value.sync="model.f_last_state"
              :options='$parent.$parent.last_check_states' placeholder='请选择安检状态'
              v-model="model.f_last_state" condition="f_last_check_state='{}'" close-on-select clear-button></v-select>
            </div>
            <div class="row app-row">
              <div class="col-xs-4">
                <img src="../../assets/安检状态.png" style="width: 20px;margin-bottom: 5px" alt="">
                <label for="f_flow_direction" class="font text-left">挂表方式：</label>
              </div>
              <v-select  id="f_flow_direction" :value.sync="model.f_flow_direction"
                        :options='$parent.$parent.flow_direction' placeholder='请选择挂表方式'
                        v-model="model.f_flow_direction" condition="f_flow_direction='{}'" close-on-select clear-button></v-select>
             </div>
            <div class="row app-row">
              <div class="col-xs-4">
              <img src="../../assets/上传状态.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label for="f_upload_state" class="font text-left">上传状态：</label>
                </div>
              <v-select id="f_upload_state" :value.sync="model.f_upload_state"
              :options='$parent.$parent.upload_states' placeholder='请选择上传状态'
              v-model="model.f_upload_state" condition="f_upload_state='{}'" close-on-select clear-button></v-select>
            </div>

           <!-- <div class="row app-row">
              <div class="col-xs-4">
              <img src="../../assets/审核状态.png" style="width: 20px;margin-bottom: 5px" alt="">
              <label for="f_approved" class="font text-left">审核状态：</label>
                </div>
              <v-select id="f_approved" :value.sync="model.f_approved"
              :options='$parent.$parent.verify_state' placeholder='请选择审核状态'
              v-model="model.f_approved" condition="f_approved='{}'" close-on-select clear-button></v-select>
            </div>-->
            <div class="row text-center" style="margin-top: 20px;">
              <button class="btn btn-lg btn-font btn-color" style="width: 45%" type="button" @click="$parent.$parent.selfSearch()">查询</button>
            </div>
            <div style="height:30px;"></div>
          </form>
        </div>
      </criteria>
      <list :model="model" partial='list'>
        <div partial>
          <div @click="$parent.$parent.$parent.inspect(row)" class="auto app-text" style="margin-top: 5px;">
            <div class="panel" style="padding: 10px 10px 5px 10px;">
              <div class="panel-body panel-self">
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">用户编号</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_userinfo_code }}</p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">用户姓名</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_user_name }}</p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">用户类型</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_user_type }}
                  </p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">用户电话</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_user_phone2 ? row.f_user_phone2 : row.f_user_phone }}
                    <img src="../../assets/phone.png" style="width: 6%;" v-on:click.stop.prevent='$parent.$parent.$parent.makeAPhoneCall(row.f_user_phone2 ? row.f_user_phone2 : row.f_user_phone)'></img>
                  </p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-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">
                  <p class="panel-title col-xs-4 text-left font">用户地址</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_address }}</p>
                </div>
                <!--<div class="row">-->
                  <!--<p class="panel-title col-xs-4 text-left font">上次安检状态</p>-->
                  <!--<p class="panel-title col-xs-8 text-left input-font">{{ row.f_last_check_state }}</p>-->
                <!--</div>-->
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">安检状态</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_state }}</p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">上传状态</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_upload_state }}</p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">预约时间</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_prearranged_date }}</p>
                </div>
                <div class="row">
                  <p class="panel-title col-xs-4 text-left font">有无计划</p>
                  <p class="panel-title col-xs-8 text-left input-font">{{ row.f_no_checkplan }}</p>
                </div>
                <div class="row text-right" v-show="row.f_upload_state != '已传'" >
                  <button type="button" class="btn yybtn-color" v-on:click.stop.prevent='$parent.$parent.$parent.reserve($index)'>预约</button>&nbsp;
                  <button type="button" class="btn qxbtn-color" v-if ='row.f_prearranged_date'
                          v-on:click.stop.prevent='$parent.$parent.$parent.cancelReserve($index, row.id)'>取消预约</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </list>
    </criteria-paged>
    <modal :show.sync="showModal">
      <div slot="modal-header" class="modal-header">
        <h4 class="modal-title">
          选择预约时间
        </h4>
      </div>
      <div slot="modal-body" class="modal-body">
        <input type="datetime-local" v-model="f_preset_dt">
      </div>
      <div slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-default" @click="genuineReserve">确认</button>
      </div>
    </modal>
    <back-page :need-back='true'  @flag="$back()"></back-page>
  </div>
</template>

<script>
  import LocalPagedList from './LocalPagedList'
import Vue from 'vue'
import { PagedList } from 'vue-client'
import * as Util from '../Util'

export default {
  title: '安检计划项',
  props: ['f_plan_id', 'f_residential_area'],
  data() {
    return {
      model: Vue.android ? new LocalPagedList('androidGetuserinfo', 5, {f_plan_id: 'this.f_plan_id'}) : new PagedList(`AndroidRest/api/af-safecheck/sql/androidGetuserinfo`, 5, {f_plan_id: 'this.f_plan_id' }),
      showModal: false,
      param: null,
      f_preset_dt : Util.to3339TimeString(),
      flow_direction:[
        {label: '全部', value: ''},
        {label: '左表', value: '左表'},
        {label: '右表', value: '右表'}
      ],
      check_states: [
        {label: '请选择安检状态', value: ''},
        {label: '未检', value: '未检'},
        {label: '已检', value: '已检'},
      ],
      last_check_states: [
        {label: '请选择上次安检状态', value: ''},
        {label: '入户', value: '入户'},
        {label: '拒检', value: '拒检'},
        {label: '到访不遇', value: '到访不遇'},
        {label: '未使用天然气', value: '未使用天然气'}
      ],
      upload_states: [
        {label: '请选择上传状态', value: ''},
        {label: '未传', value: '未传'},
        {label: '已传', value: '已传'}
      ],
      user_types: [
        {label: '请选择用户类型', value: ''},
        {label: '民用', value: '民用'},
        {label: '非民用', value: '非民用'},
      ],
      verify_state: [
        {label: '请选择审核状态', value: ''},
        {label: '未审核', value: '未审核'},
        {label: '打回', value: '打回'},
      ],
    }
  },
  methods: {
    reload(){
      this.$refs.paged.loadPage(this.$refs.paged.model.pageIndex)
    },
    search(args) {
      this.model.rows = []
      this.model.search(args.condition, args.model)
    },
    selfSearch () {
      let condition = ""
      if(this.$refs.paged.$refs.cri.model.f_keyword)
        condition += " and (ti.f_building like '%"+this.$refs.paged.$refs.cri.model.f_keyword+"%' or ti.f_unit like '%"+this.$refs.paged.$refs.cri.model.f_keyword+"%' or ti.f_floor like '%"+this.$refs.paged.$refs.cri.model.f_keyword+"%' or ti.f_room like '%"+this.$refs.paged.$refs.cri.model.f_keyword+"%' or ti.f_address like '%"+this.$refs.paged.$refs.cri.model.f_keyword+"%')"
      if(this.$refs.paged.$refs.cri.model.f_userinfo_code)
        condition += " and ti.f_userinfo_code like '%"+this.$refs.paged.$refs.cri.model.f_userinfo_code+"%'"
      if(this.$refs.paged.$refs.cri.model.f_user_name)
        condition += " and ti.f_user_name like '%"+this.$refs.paged.$refs.cri.model.f_user_name+"%'"
      if(this.$refs.paged.$refs.cri.model.f_state[0])
        condition += " and ti.f_state='"+this.$refs.paged.$refs.cri.model.f_state[0]+"'"
      if(this.$refs.paged.$refs.cri.model.f_last_state[0])
        condition += " and ti.f_last_check_state='"+this.$refs.paged.$refs.cri.model.f_last_state[0]+"'"
      if(this.$refs.paged.$refs.cri.model.f_upload_state[0])
        condition += " and ti.f_upload_state='"+this.$refs.paged.$refs.cri.model.f_upload_state[0]+"'"
      if(this.$refs.paged.$refs.cri.model.f_user_type[0])
        condition += " and ti.f_user_type='"+this.$refs.paged.$refs.cri.model.f_user_type[0]+"'"
      // if(this.$refs.paged.$refs.cri.model.f_approved[0])
      //   condition += " and ti.f_approved='"+this.$refs.paged.$refs.cri.model.f_approved[0]+"'"
      if(this.$refs.paged.$refs.cri.model.f_meter_no)
        condition += " and tm.f_meter_no like '%"+this.$refs.paged.$refs.cri.model.f_meter_no+"%'"
      if(this.$refs.paged.$refs.cri.model.f_flow_direction[0])
        condition += " and tm.f_flow_direction = '"+this.$refs.paged.$refs.cri.model.f_flow_direction[0]+"'"
      if(this.model.f_plan_id)
        condition += " and ti.f_plan_id = '"+this.model.f_plan_id+"'"
      if (this.f_residential_area) {
      this.model.search("ti.f_residential_area = '" + this.f_residential_area + "'" +condition)
    }else{
        this.model.search("ti.f_residential_area is null" + condition)
      }
    },
    inspect(row) {
      if (row.f_upload_state == '未传' || (row.f_upload_state == '已传' && row.f_state == '未检')) {
        this.$resetpost(`${this.$androidUtil.getProxyUrl()}/api/af-safecheck/sql/查询计划项表信息`, {data: {f_userid: row.f_userinfoid}}).then((response) => {
          console.log(JSON.stringify(response))
        row['f_plan_meters'] = response.data

        if (row.f_user_type == '民用') {
          this.$goto('paper-set', {f_plan_id: row.f_plan_id, item: row, role: 'inspect'}, 'self', this.reload)
        } else {
          this.$goto('paper-set-noncivil', {f_plan_id: row.f_plan_id, item: row, role: 'inspect'}, 'self', this.reload)
        }
      })
      }else(
        this.$showMessage("该安检单已被上传！")
      )
    },
    makeAPhoneCall(phoneNumber) {
      this.$androidUtil.makeAPhoneCall(phoneNumber)
    },
    reserve (idx) {
      this.param = idx
      this.showModal = true
    },
    cancelReserve (idx, id) {
      if(Vue.android){
        let res =  this.$androidUtil.bzLogic('BookingCheck', {dt: '', id: id})
        this.$set('model.rows[' + idx + '].f_prearranged_date', null)
        if (res.code == 200) {
          this.$showMessage("取消预约成功")
        }
      }else {
        this.$androidUtil.bzLogic('BookingCheck', {dt: '', id: id}).then((repanse) => {
          this.$set('model.rows[' + idx + '].f_prearranged_date', null)
          if (repanse.data.code == 200) {
            alert("取消预约成功!")
          }
        })
      }},
    genuineReserve () {
      if(Vue.android){
        if (!this.f_preset_dt) {
          this.$showMessage('请选择一个日期')
          return
        }
        let pdt = this.f_preset_dt.replace('T', ' ')
        let respanse =  this.$androidUtil.bzLogic('BookingCheck', {id: this.model.rows[this.param].id, dt: pdt})
        this.$set('model.rows[' + this.param + '].f_prearranged_date', pdt)
        if(respanse.code == 200) {
          this.$showMessage("预约成功!")
          this.reload()
        }
        this.showModal = false
      }else {
        if (!this.f_preset_dt) {
          alert('请选择一个日期')
          return
        }
        let pdt = this.f_preset_dt.replace('T', ' ')
        this.$androidUtil.bzLogic('BookingCheck', {id: this.model.rows[this.param].id, dt: pdt}).then((res) => {
          this.$set('model.rows[' + this.param + '].f_prearranged_date', pdt)
          if(res.data.code == 200) {
            alert("预约成功!")
            this.reload()
          }
          this.showModal = false
        })
      }
    }
  },
  ready() {
      this.model.f_plan_id = this.f_plan_id
      // this.model.metercond = '1 = 1'
    //  this.selfSearch()
  }
}
</script>
