<template>
  <div class="flex-row">
    <div  class="basic-main">
      <!--    <button class="btn btn-xs" v-show="!criteriaShow" @click="show()">-->
      <!--      <span class="glyphicon glyphicon-chevron-down" style="color: #000;"></span>-->
      <!--    </button>-->
      <criteria-paged :model="model"  v-ref:paged>
        <criteria partial='criteria'  class="search_area" @condition-changed='$parent.search' v-ref:criteria>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">
              <div class="col-sm-2" >
                <label class="font_normal_body">用户编号</label>
                <input type="text" class="input_search" style="width: 60%" v-model="model.f_userinfo_code" id="f_userinfo_code" style="width:60%"
                       placeholder="用户编号" condition="f_userinfo_code like '%{}%'">
              </div>
              <div class="col-sm-2" >
                <label class="font_normal_body">用户名称</label>
                <input type="text" class="input_search" style="width: 60%" v-model="model.f_user_name" id="f_user_name" style="width:60%"
                       placeholder="用户名称" condition="f_user_name like '%{}%'">
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">小区名称</label>
                <!--              <input type="text" class="input_search" style="width: 60%" v-model="model.f_residential_area" id="f_residential_area" style="width:60%"-->
                <!--                     placeholder="小区名称" condition="f_residential_area like '%{}%'">-->

                <v-select :value.sync="model.f_residential_area"
                          :value-single="true"
                          v-model="model.f_residential_area"
                          :options='$parent.$parent.areaslist'
                          placeholder='请选择'
                          condition="f_residential_area = '{}'"
                          close-on-select
                          search="true">
                </v-select>
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">地址信息</label>
                <input type="text" class="input_search" style="width: 60%" v-model="model.f_address" id="f_address" style="width:60%"
                       placeholder="地址信息" condition="f_address like '%{}%'">
              </div>


              <div class="col-sm-4" style="float: right" >
                <div style="float: right; margin-top: 8px" class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"  @click="$parent.$parent.hidden()"></div>
                <button type="button" class="button_export button_spacing" style="float: right;margin-top: 8px"  @click="$parent.$parent.itemAdd()" v-if="$parent.$parent.checkplan">添加</button>
                <button type="button" class="button_export button_spacing" style="float: right;margin-top: 8px"  @click="$parent.$parent.itemAddBack()" v-if="$parent.$parent.checkplan">返回</button>
                <button type="button" class="button_export button_spacing"  style="float: right;margin-top: 8px"  @click="$parent.$parent.AllPlan()" v-if="!$parent.$parent.checkplan">计划生成</button>
                <button class="button_search button_spacing" style="float: right;margin-top: 8px"  @click="search()">查询</button>
              </div>
            </div>
            <div class="row" v-show="$parent.$parent.criteriaShow">
              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">楼栋号码</label>
                <input type="text" class="input_search" v-model="model.f_building" id="f_building" style="width:60%"
                       placeholder="楼栋号码" condition="f_building = '{}'">
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2" >
                <label class="font_normal_body">单元号码</label>
                <input type="text" class="input_search" v-model="model.f_unit" id="f_unit" style="width:60%"
                       placeholder="单元号码" condition="f_unit = '{}'">
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">楼层号码</label>
                <input type="text" class="input_search" v-model="model.f_floor" id="f_floor" style="width:60%"
                       placeholder="楼层号码" condition="f_floor = '{}'">
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">片区名称</label>
                <v-select style="width:60% "
                          class="select select_list"
                          :value.sync="model.f_area"
                          v-model="model.f_area"
                          :options='$parent.$parent.areas'
                          placeholder='片区/管理站'
                          condition="f_slice_area like '%{}%'"
                          close-on-select>
                </v-select>
              </div>

              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">用户类型</label>
                <v-select id="f_usertype"
                          placeholder='用户类型' style="width:60% "
                          class="select select_list"
                          :value.sync="model.f_usertype"
                          :value-single="true"
                          :options='$parent.$parent.usertypes'
                          v-model="model.f_usertype"
                          condition="f_user_type = '{}'"
                          close-on-select clear-button> </v-select>
              </div>

              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">安检状态</label>
                <v-select :value.sync="model.f_last_check_state" :options='$parent.$parent.checkStates' placeholder='上次安检状态' close-on-select
                          style="width:60% "   class="select select_list"  v-model='model.f_last_check_state'   condition="f_last_check_state = '{}'">
                </v-select>
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2  " >
                <label class="font_normal_body">调&nbsp;&nbsp;压&nbsp;箱</label>
                <v-select :value.sync="model.f_adjustable_id" :options='$parent.$parent.adjustables' placeholder='调压箱' close-on-select
                          style="width:60% "   class="select select_list"  v-model='model.f_adjustable_id'   condition="f_adjustable_id = '{}'" :search='true'></v-select>
              </div>
              <div class="col-sm-4" style="width: 20%" v-if="$parent.$parent.criteriaShow">
                <label class="font_normal_body">开始时间</label>
                <datepicker
                  class="select_list select"
                  :value.sync="model.f_check_start"
                  placeholder='开始时间' style="width: 60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_check_start"
                  condition="f_createfile_date > ='{} 00:00:00'">
                </datepicker>
              </div>
              <div class="col-sm-4" style="width: 20%" v-if="$parent.$parent.criteriaShow">
                <label class="font_normal_body">结束时间</label>
                <datepicker
                  :value.sync="model.f_check_end"
                  placeholder='结束时间' style="width: 60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_check_end"
                  condition="f_createfile_date <=  '{} 23:59:59'">
                </datepicker>
              </div>
              <div class="col-sm-2">
                <label class="font_normal_body">&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp;</label>
                <right-tree @re-res="$parent.$parent.getRes" :initresid="$login.f.orgid">
                </right-tree>
              </div>
              <div class="col-sm-2 col-md-2 col-lg-2" style="width: 25%" >
                <label class="font_normal_body">距离上次安检</label>
                <input type="text" class="input_search" v-model="model.f_start_time" style="width:18%"
                       value="12"
                       placeholder="" condition="f_time >= '{}'">
                到
                <input type="text" class="input_search" v-model="model.f_end_time" style="width:18%"
                       placeholder="" condition="f_time <= '{}'">
                <v-select
                  placeholder=''
                  :width="'20%'"
                  class="select select_list"
                  :value.sync="$parent.$parent.model.timetype"
                  :value-single="true"
                  :options='$parent.$parent.timetypes'
                  close-on-select clear-button> </v-select>
              </div>
              <!--              <div class="col-sm-2 col-md-2 col-lg-2" >-->
              <!--                <label class="font_normal_body">是否包含新开户</label>-->
              <!--                <v-select -->
              <!--                          placeholder='用户类型' style="width:60% "-->
              <!--                          class="select select_list"-->
              <!--                          :value.sync="$parent.$parent.model.includeNew"-->
              <!--                          :value-single="true"-->
              <!--                          :options='$parent.$parent.includeNews'-->
              <!--                          v-model="model.includeNew"-->
              <!--                          condition="f_last_check_date = '{}'"-->
              <!--                          close-on-select clear-button> </v-select>-->
              <!--              </div>-->
            </div>
            <modal :show.sync="$parent.$parent.showModal" v-ref:modal  backdrop="false">
              <div slot="modal-header" class="modal-header">
                <h4 class="modal-title">
                  生成计划
                </h4>
              </div>
              <div slot="modal-body" class="modal-body">
                <div class="row">
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">计划名称</label>
                    <input class="input_search" style="width: 60%" type="text" v-model="$parent.$parent.checkboxModel.f_plan_name"  placeholder="计划名称(必填)">
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">是否共享</label>
                    <v-select :value="$parent.$parent.IsShare" style="width:60% "
                              class="select select_list"
                              v-on:change="$parent.$parent.IsShareschange" :value-single="true"
                              :options='$parent.$parent.IsShares' close-on-select clear-button> </v-select>
                  </div>
                  <div class="col-sm-6  form-group form-fix-width" v-if="$parent.$parent.IsShare == '否'">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">安&nbsp;检&nbsp;员&nbsp;</label>
                    <v-select id="checker" :value="$parent.$parent.checker" style="width:60% "
                              class="select select_list"
                              v-on:change="$parent.$parent.checkerchange" :value-single="true"
                              :options='$parent.$parent.checkers' close-on-select clear-button> </v-select>
                  </div>
                  <!--<div class="col-sm-6  form-group form-fix-width" v-if="$parent.$parent.IsShare == '是'">-->
                  <!--<label class="control-label">站点</label>-->
                  <!--<v-select id="checker" :value="$parent.$parent. "-->
                  <!--v-on:change="$parent.$parent." :value-single="true"-->
                  <!--:options='$parent.$parent.' close-on-select clear-button> </v-select>-->
                  <!--</div>-->
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">计划年份</label>
                    <v-select id="c_plan_year" :value.sync="$parent.$parent.f_plan_year" style="width:60% "
                              class="select select_list"
                              :options='$parent.$parent.c_year_list' close-on-select clear-button> </v-select>
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">计划月份</label>
                    <v-select id="c_plan_month" :value.sync="$parent.$parent.f_plan_month" style="width:60% "
                              class="select select_list"
                              :options='$parent.$parent.c_month_list' close-on-select clear-button> </v-select>
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">安检类型</label>
                    <v-select id="f_safecheck_type" :value.sync="$parent.$parent.checkboxModel.f_safecheck_type" style="width:60% "
                              class="select select_list"
                              :value-single="true"
                              :options='$parent.$parent.c_type_list' close-on-select clear-button> </v-select>
                  </div>
                  <data-grid :model="$parent.$parent.model2" partial='list' v-ref:grid2 class="list_area table_sy">
                    <template partial='head'>
                      <tr>
                        <th>安检员</th>
                        <th>计划数</th>
                        <th>已检</th>
                        <th>未检</th>
                      </tr>
                    </template>
                    <template partial='body'>
                      <td style="text-align: center">{{row.f_checker}}</td>
                      <td style="text-align: center">{{row.c}}</td>
                      <td style="text-align: center">{{row.checked}}</td>
                      <td style="text-align: center">{{row.unchecked}}</td>
                    </template>
                    <template partial="foot">
                    </template>
                  </data-grid>
                  <!--<div class="col-sm-6  form-group form-fix-width">-->
                  <!--<label class="control-label">有效起始</label>-->
                  <!--<datepicker-->
                  <!--:value.sync="$parent.$parent.f_effective_start_time"-->
                  <!--placeholder='最后安检时间起始'-->
                  <!--:disabled-days-of-week="[]"-->
                  <!--:format="'yyyy-MM-dd'"-->
                  <!--:show-rest-button="reset"-->
                  <!--v-model = "$parent.$parent.f_effective_start_time">-->
                  <!--</datepicker>-->
                  <!--</div>-->
                  <!--<div class="col-sm-6  form-group form-fix-width">-->
                  <!--<label class="control-label">有效结束</label>-->
                  <!--<datepicker-->
                  <!--:value.sync="$parent.$parent.f_effective_end_time"-->
                  <!--placeholder='最后安检时间起始'-->
                  <!--:disabled-days-of-week="[]"-->
                  <!--:format="'yyyy-MM-dd'"-->
                  <!--:show-rest-button="reset"-->
                  <!--v-model = "$parent.$parent.f_effective_end_time">-->
                  <!--</datepicker>-->
                  <!--</div>-->
                </div>
              </div>
              <div slot="modal-footer" class="modal-footer">
                <span style="color:red;float: left" v-show = "$parent.$parent.repeated > 0">此次下发共有{{$parent.$parent.repeated}}单重复</span>
                <button type="button" v-show="$parent.$parent.repeated > 0" class="button_spacing button_search" @click="$parent.$parent.confirm('override')">覆盖下发</button>
                <button type="button" v-show="$parent.$parent.repeated > 0" class="button_spacing button_search" @click="$parent.$parent.confirm('skip')">跳过下发</button>
                <button type="button" v-show="$parent.$parent.repeated == 0" class="button_spacing button_search" @click="$parent.$parent.confirm('create')">生成</button>
                <button type="button" class="button_spacing button_clear" @click="$parent.$parent.cancel">取消</button>
              </div>
            </modal>
            <modal :show.sync="$parent.$parent.showAddModal" v-ref:addModal backdrop="false">
              <div slot="modal-header" class="modal-header">
                <h4 class="modal-title">
                  添加计划
                </h4>
              </div>
              <div slot="modal-body" class="modal-body">
                <div class="row">
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">计划名称</label>
                    <input class="input_search" style="width: 60%;" disabled="true" type="text" v-model="$parent.$parent.checkboxModel.f_plan_name">
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">是否共享</label>
                    <input class="input_search" style="width: 60%;" disabled="true" type="text" v-model="$parent.$parent.checkboxModel.IsShare">
                  </div>
                  <div class="col-sm-6  form-group form-fix-width" v-if="$parent.$parent.checkboxModel.IsShare == '否'">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">安&nbsp;检&nbsp;员</label>
                    <input class="input_search" style="width: 60%;" disabled="true" type="text" v-model="$parent.$parent.checkboxModel.checker">
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">计划年份</label>
                    <input class="input_search" style="width: 60%;" disabled="true" type="text" v-model="$parent.$parent.checkboxModel.f_plan_year">
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">计划月份</label>
                    <input class="input_search" style="width: 60%;" disabled="true" type="text" v-model="$parent.$parent.checkboxModel.f_plan_month">
                  </div>
                  <div class="col-sm-6  form-group form-fix-width">
                    <label class="font_normal_body" style="padding-top: 0;line-height: 39px;">安检类型</label>
                    <input class="input_search" style="width: 60%;" disabled="true" type="text" v-model="$parent.$parent.checkboxModel.f_safecheck_type">
                  </div>
                </div>
              </div>
              <div slot="modal-footer" class="modal-footer">
                <span style="color:red;float: left" v-show = "$parent.$parent.repeated > 0">此次下发共有{{$parent.$parent.repeated}}单重复</span>
                <button type="button" v-show="$parent.$parent.repeated > 0" class="button_spacing button_search" @click="$parent.$parent.confirm('override')">覆盖下发</button>
                <button type="button" v-show="$parent.$parent.repeated > 0" class="button_spacing button_search" @click="$parent.$parent.confirm('skip')">跳过下发</button>
                <button type="button" v-show="$parent.$parent.repeated == 0" class="button_spacing button_search" @click="$parent.$parent.confirm('create')">添加</button>
                <button type="button" class="button_spacing button_clear" @click="$parent.$parent.cancel">取消</button>
              </div>
            </modal>
          </div>
        </criteria>
        <data-grid :model="model" class="list_area table_sy" style="overflow: hidden" partial='list'>
          <template partial='head'>
            <tr>
              <!--<th><nobr>全选<input type="checkbox" @click='$parent.$parent.$parent.changeStates'  v-model='$parent.$parent.$parent.checkedAll'></nobr></th>-->
              <!--            <th><nobr></nobr></th>-->
              <th><nobr>用户编号</nobr></th>
              <th><nobr>用户名称</nobr></th>
              <th><nobr>用户电话</nobr></th>
              <th><nobr>用户类型</nobr></th>
              <th><nobr>小区名称</nobr></th>
              <th><nobr>楼号</nobr></th>
              <th><nobr>单元</nobr></th>
              <th><nobr>楼层</nobr></th>
              <th><nobr>门牌号</nobr></th>
              <th><nobr>地址</nobr></th>
              <th><nobr>隐患情况</nobr></th>
              <!--            <th><nobr>用户家庭情况备注</nobr></th>-->
              <th><nobr>最后安检日期</nobr></th>
              <th><nobr>最后安检状态</nobr></th>
            </tr>
          </template>
          <template partial='body' >
            <!--          <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center"><nobr><input type="checkbox" v-model="row.checked" v-bind:checked="row.checked"  @click="$parent.$parent.$parent.addChecked(row)"></nobr></td>-->
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center;">{{row.f_userinfo_code}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center;">{{row.f_user_name}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center;">{{row.f_user_phone}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_user_type}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_residential_area}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_building}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_unit}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_floor}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_room}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_address}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">
              <div v-if="row.danger"><div v-if="row.danger.result=='有隐患'"><a @click="$parent.$parent.$parent.gotodanger(row.danger)">{{row.danger.result}}</a></div>
                <div v-if="row.danger.result!='有隐患'"></div></div>
            </td>
            <!--          <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_user_family_remarks}}</td>-->
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_last_check_date}}</td>
            <td :class="[ row.color ==='red' ? 'p1' : row.color === 'yellow'? 'p':''  ]" style="text-align: center">{{row.f_last_check_state}}</td>
          </template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>


  <modal :show.sync="showdanger" v-ref:modal  backdrop="false">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="colsedanger"><span>&times;</span></button>
      <h4 style="text-align: center" class="modal-title">隐患内容</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="form-group">
        <center><div v-html="dangerinfo"></div></center>

      </div>
    </article>
    <div slot="modal-footer" class="modal-footer">
      <button type="button" class="button_spacing button_search" @click="colsedanger()">关闭</button>
    </div>
  </modal>


</template>

<script>
  import { PagedList } from 'vue-client'
  import { HttpResetClass } from 'vue-client'
  import co from 'co'
  import * as Util from '../Util'
  import Vue from 'vue'
  import AppData from '../../stores/AppData'

  let AddItem = function * (self) {
    self.checkboxModel.f_filialeid = self.$login.f.orgid
    self.checkboxModel.f_orgstr = self.$login.f.orgpathstr
    self.checkboxModel.planid = self.checkplan.id
    self.checkboxModel.rows = self.checkrows
    self.checkboxModel.model = self.model
    self.checkboxModel.sqlname = self.sqlname
    self.checkboxModel.f_filialeids = self.model.f_filialeids
    self.checkboxModel.createWay = self.createWay
    self.checkboxModel.repeated = self.repeated
    console.log('self.model',self.model)
      self.checkboxModel.planWay = 'add'
      yield self.$resetpost('api/af-safecheck/logic/GenCallInAllPlan', {data: self.checkboxModel},{resolveMsg: null, rejectMsg: null}).then((res)=>{
        console.log(res.data)
        if(res.data.code == 200) {
          self.$showMessage(`添加成功！现在${self.checkplan.f_plan_name}共有${res.data.countItem}个计划！`)
          self.$emit('finish')
        }else {
          self.$showMessage("计划添加失败!")
        }
        self.isBusy = false
      })
  }

  let saveGen = function * (self) {
    self.checkboxModel.f_create_time = self.$login.toStandardTimeString()
    self.checkboxModel.f_plan_type = '预约计划'
    self.checkboxModel.f_issued = '否'
    self.checkboxModel.f_filialeid = self.$login.f.orgid
    self.checkboxModel.f_filialeids = self.model.f_filialeids
    self.checkboxModel.f_orgstr = self.$login.f.orgpathstr
    self.checkboxModel.f_plan_year = self.f_plan_year[0]
    self.checkboxModel.f_plan_month = self.f_plan_month[0]
    self.checkboxModel.f_subcompany = self.$login.f.f_fengongsi
    self.checkboxModel.f_create_operator = self.$login.f.name
    self.checkboxModel.f_effective_start_time = self.f_effective_start_time
    self.checkboxModel.f_effective_end_time = self.f_effective_end_time
    self.checkboxModel.model = self.model
    self.checkboxModel.sqlname = self.sqlname
    self.checkboxModel.planWay = 'create'
    self.checkboxModel.createWay = self.createWay
    self.checkboxModel.repeated = self.repeated
    yield self.$resetpost('api/af-safecheck/logic/GenCallInAllPlan', {data: self.checkboxModel}, {resolveMsg: null, rejectMsg: null})
      .then((respanse) => {
        console.log(respanse)
        self.showModal = false
        if (respanse.data.code == 200) {
          self.$showMessage("预约计划生成成功!")
          self.checkrows = []
          self.creatPlanWay = ''
          self.checker = {}
          self.IsShare = '否'
          self.checkboxModel = {}
          self.$refs.paged.$refs.criteria.search()
          self.createWay = ''
        } else {
          self.$showMessage("预约计划生成失败!")
        }
        self.isBusy = false
      })
  }

  let checkItems = function * (self) {
    let data = {}
    self.repeated = -1
    let checkIdArray = []
    data = self.model.params
    console.log('data ===> '+JSON.stringify(data))
    yield self.$resetpost('api/af-safecheck/sql/getExistCheckItem/n', {data:data}, {resolveMsg: null, rejectMsg: null})
      .then((res) => {
        console.log(res.data.n)
        self.repeated = res.data.n
      })
  }

  let seachData = function * (self) {
    let getParams = yield self.$resetpost('api/af-safecheck/logic/getSaleInitData', {data: {f_orgid: self.$login.f.orgid}}, {
      resolveMsg: null,
      rejectMsg: '获取营收参数出错！！！'
    })
    console.log('获取营收参数', getParams)
    if (getParams.data) {
      let arr = []
      getParams.data.adjustable.forEach((item, index) => {
        let temp = {}
        temp.label = `[${item.f_adjustable_id}]-${item.f_adjustable_name}`
        temp.value = `${item.id}`
        arr.push(temp)
      })
      self.adjustables = [{label: '全部', value: ''}, ...arr]
    }
  }
    let select = function* (self) {
      console.log(3)
      let http = new HttpResetClass()
      let getGasman = yield http.load('POST', '/rs/search', {
        source: 'this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))',
        userid: self.$login.f.id
      }, {resolveMsg: null, rejectMsg: null})
      console.log(4)
      console.log('获取安检员', getGasman)
      getGasman.data.forEach((checker) => {
        self.checkers.push({label: checker.name, value: checker})
      })
    }

    let searchSafeCycle= function* (self){
      console.log('获取民用非民用安检周期')
      let http = new HttpResetClass()
      yield http.load('POST', 'api/af-safecheck/logic/getSafeCheckCycle', {},{resolveMsg: null, rejectMsg: null}).then((res)=>{
        console.log('安检周期',res.data)
        if(res.data.code == 200){
          self.civil = res.data.civil.value
          self.civilian = res.data.civilian.value
          self.model.safeCycle = self.civil
        }else{
          self.$showMessage(res.data.msg)
        }
      })
  }
    export default {
      title: '计划生成',
      data() {
        let options = []
        let year = (new Date()).getFullYear()
        for (let i = year; i <= year + 3; i++)
          options.push({label: i + '', value: i + ''})
        let moptions = []
        for (let i = 1; i <= 12; i++)
          moptions.push({label: i + '', value: i + ''})
        let model1 = new PagedList('api/af-safecheck/sql/FetchUserFileByOrgChecker', 50,null)
        let model = new PagedList('api/af-safecheck/sql/FetchUserFileByOrgChecker', 50, {
          orderitem: '"f_residential_area,f_address"',
          timetype: 'this.timetype',
          f_filialeids: 'this.f_filialeids'
        })
        // 默认搜索12个月
        model.timetype = 'mm'
        model.f_filialeids = '('+this.$login.f.orgid+')'
        let model2 = new PagedList('api/af-safecheck/sql/planCount', 50, {
          f_filialeids: 'this.f_filialeids'
        })
        model2.f_filialeids = '('+this.$login.f.orgid+')'
        return {
          isBusy: false,
          civil:'',   //民用安检周期
          civilian:'',      //非民用安检周期
          SafeCheckXZs: [{label: '未安检', value: '未安检'}, {label: '已安检', value: '已安检'},{label: '应安检', value: '应安检'},{label: '超期安检', value: '超期安检'}], // 安检筛选选项
          SafeCheckSXs: [{label: '是', value: '是'}, {label: '否', value: '否'}, {label: '全部', value: ''},],
          IsShares: [{label: '是', value: '是'}, {label: '否', value: '否'}],
          IsShare:'否',
          checker: {},
          criteriaShow: false,
          model: model,
          model1:model,
          model2:model2,
          checkers: [],
          chosenOrg: {id: 0},
          showModal: false,
          showAddModal: false,
          checkedAll: false,
          checkrows: [],
          adjustables: [],
          checkStates: this.$appdata.getParam('安检状态'),
          f_plan_year: year + '',
          f_plan_month: moptions[new Date().getMonth()].value,
          f_effective_start_time:'',  //计划有效起始时间
          f_effective_end_time:'',    //计划有效结束时间
          checkboxModel: {
            f_plan_name: '',
            f_plan_year: '',
            f_plan_month: '',
            f_safecheck_type:'年度普建',
            IsShare: '',
            checker: ''
          },
          c_year_list: options,
          c_month_list: moptions,
          c_type_list:this.$appdata.getParam('安检类型'),
          areas: this.areas(),
          areaslist: [],
          checkstatus: this.checkstatus(),
          usertypes: this.usertypes(),
          timetypes: this.timetypes(),
          userstates: this.userstates(),
          sqlname:'',
          creatPlanWay:'',
          showdanger:false,
          dangerinfo:null,
          repeated: 0, //下发时重复的安检单条数
          createWay: ''
        }
      },
      props: {
        checkplan: { // 传入一条安检计划信息
          type: Object
        },
        rowsdata:[],
        additemshow:{
          type:Boolean,
          default:false
        }
      },
      ready() {
        this.getAreaList()
        console.log('this.arealist===> ' + JSON.stringify(this.areaslist))
        console.log('CreatePlan启动了')
        if (this.rowsdata){
          console.log('传进数据了')
          this.model.rows = this.rowsdata
        }
        this.model.chosenOrg = {id: 0}
        console.log(this.$login.f)
        this.model.f_filialeid = this.$login.f.orgid
        // this.model.f_filiale = this.$login.f.f_fengongsi
        this.model.f_OrgStr = this.$login.f.orgpathstr
        console.log(0)
        let param = seachData(this)
        console.log(1)
        co(param)
        console.log(2)
        let gen = select(this)
        co(gen)
        let single = searchSafeCycle(this)
        co(single)
        if(this.checkplan){
          console.log('this.checkplan')
          console.log(this.checkplan)
          this.checkboxModel.f_plan_name = this.checkplan.f_plan_name
          this.checkboxModel.f_plan_year = this.checkplan.f_plan_year
          this.checkboxModel.f_plan_month = this.checkplan.f_plan_month
          if(this.checkplan.f_checker){
            this.checkboxModel.IsShare = '否'
            this.checkboxModel.checker = this.checkplan.f_checker
          }else {
            this.checkboxModel.IsShare = '是'
          }
          console.log('this.checkboxModel')
          console.log(this.checkboxModel)
        }
        let f_start_time = this.$appdata.getSingleValue('安检间隔月份')
        if(f_start_time){
          this.$refs.paged.$refs.criteria.model.f_start_time = f_start_time
        }
      },
      methods: {

        itemAddBack(){
          this.$emit('finish')
        },
        itemAdd(val){
          if(this.model.rows.length == 0){
            this.$showMessage("暂无可添加的计划！")
            return
          }
          this.checkExist()
          this.showAddModal = true
        },
        // 选择安检员
        IsShareschange(val){
          this.IsShare = val
          if(val == '是'){
            this.checker = null
          }
        },
        checkerchange(val) {
          this.checkboxModel.f_checker = val.name
          this.checkboxModel.f_checker_id = val.id
        },
        dangerplan(){
          this.$resetpost('api/af-safecheck/sql/getRemind', {data:{condition :` 1 = 1 and f_filialeid in ${this.model.f_filialeids}`}}, {resolveMsg: null, rejectMsg: null}).then((res) => {

            if (res.data.length>0){
              let a =0
              let b =0
              let c =0

              for(let row of res.data){
                let aa=''
                let bb =JSON.parse(row.f_last_check_result)
               /* if (bb.result){
                  aa=aa+bb.result
                  if (bb.data.length>0){
                    aa = aa +'\n隐患为：'
                    for (let key of bb.data){
                      let str = JSON.stringify(key)
                      aa = aa+str+'\n'
                    }
                  }

                }*/
               if (!bb.result){
                 bb.result=''
               }
                row.danger=bb
                if ( row.f_last_check_result.indexOf('A')){
                  a=a+1
                  console.log('包含A')
                  if (row.nocheckday>5&&row.nocheckday<=7){
                    b=b+1
                    row.color='yellow'
                  }
                  else if (row.nocheckday>7){
                    c=c+1
                    row.color='red'
                  }
                  continue
                }
                if ( row.f_last_check_result.indexOf('B')){
                  a=a+1
                  console.log('包含B')
                  if (row.nocheckday>12&&row.nocheckday<=14){
                    b=b+1
                    row.color='yellow'
                  }
                  else if (row.nocheckday>14){
                    c=c+1
                    row.color='red'
                  }
                  continue
                }
                if ( row.f_last_check_result.indexOf('C')){
                  a=a+1
                  console.log('包含C')
                  if (row.nocheckday>28&&row.nocheckday<=30){
                    b=b+1
                    row.color='yellow'
                  }
                  else if (row.nocheckday>30){
                    c=c+1
                    row.color='red'
                  }
                  continue
                }
              }
              console.log('查询到的数据',res)
              this.model.rows = res.data
              this.$showMessage('目前有'+res.data.length+'户存在隐患需要及时安检，其中'+b+'户需要在两天内安检，'
                      +c+'户已超时，未在规定时间内安检')

            }
            else {
              this.$showMessage('当前没有存在隐患需要安检的用户')
            }

          })
        },
       async search(args) {
          console.log('this.areaslist ===>' + JSON.stringify(this.areaslist))
         this.checkrows = []
         this.checkedAll = false
         this.sqlname = 'ByOrgChecker'
         console.log('查询结束')
         this.model.url = 'api/af-safecheck/sql/FetchUserFileByOrgChecker'
          await this.model1.search(args.condition, args.model)

            for (let row of this.model1.rows){
              if (row.f_last_check_result){
                let bb =JSON.parse(row.f_last_check_result)
                row.danger=bb
              }

            }
            this.model.rows=this.model1.rows
            this._isRouterView=false
         this._isRouterView=true
          },
        colsedanger(){
          this.showdanger=false
        },
        gotodanger(val) {
          let a=''
          for (let row of val.data){
            let str = JSON.stringify(row)
            a=a+str+'<br/>'
          }
          this.dangerinfo=a
          this.showdanger=true

        },
        checkstatus() {
          return [{label: '全部', value: ''}, ...this.$appdata.getParam('安检状态')]
        },
        usertypes() {
          return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
        },
        userstates() {
          return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户状态')]
        },
        timetypes() {
          // return [{label: '天', value: 'dd'},{label: '月',value: 'mm'},{label:'年',value:'yy'}]
          return [{label: '天', value: 'dd'},{label: '月',value: 'mm'}]
        },
        async getAreaList() {
          let http = new HttpResetClass()
          this.areaslist = [{label:'全部',value:''}]
          let res = await http.load('POST', '/api/af-safecheck/sql/safe_singleTable_GroupBy', {
            data: {
              items: 'f_residential_area',
              tablename: 't_area',
              condition: `f_filialeid = '${this.$login.f.orgid}'`,
              groupitem: 'f_residential_area'
            }
          }, {resolveMsg: null, rejectMsg: null})
          if (res.data) {
            res.data.forEach(item => this.areaslist.push({label: item.f_residential_area, value: item.f_residential_area}))
          }
        },
        show() {
          this.criteriaShow = true
        },
        hidden() {
          this.criteriaShow = !this.criteriaShow
        },
        confirm(createWay) {
          if (!this.checkboxModel.f_plan_name) {
            this.$showMessage('请填写安检计划名称!')
            return
          }
          // if(this.f_effective_start_time == ''){
          //   this.$showMessage('请选择有效起始日期！')
          //   return
          // }
          // if(this.f_effective_end_time == ''){
          //   this.$showMessage('请选择有效结束日期！')
          //   return
          // }
          if(this.IsShare == '否'&&!this.checkplan){
            if (!this.checkboxModel.f_checker_id) {
              this.$showMessage('请选择安检员！')
              return
            }
          }
          this.createWay = createWay
          if(this.isBusy === false){
            this.isBusy = true
            this.$showMessage('正在下发中，请稍等...')
            if(!this.checkplan){
              let gen = saveGen(this)
              co(gen)
            }else {
              let _gen = AddItem(this)
              co(_gen)
            }
            this.isBusy = false
          }else {
            this.$showMessage('正在下发中，请稍等...')
          }

        },
        changeStates() {
          if (this.checkedAll) {//实现反选/
            this.checkrows = [];
            for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
              this.$set('$refs.paged.model.rows[' + i + '].checked', false)
            }
          } else {//实现全选
            for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
              this.$set('$refs.paged.model.rows[' + i + '].checked', true)
            }
            let _this = this
            let condition = this.$refs.paged.$refs.criteria.condition
            let url = this.model.url
            this.$resetpost(`${url}?pageSize=9999999`, {
              data: {
                condition: condition,
                groupitem: '',
                orderitem: 'f_residential_area',
                f_usertype:this.model.f_usertype,
                f_filialeids: this.model.f_filialeids,
                month: this.model.month,
                safeCycle:this.model.safeCycle,
                f_checktime_start:this.model.f_checktime_start,
                f_checktime_end:this.model.f_checktime_end,
                SafeCheckSX: this.model.SafeCheckSX,
                f_last_check_state: this.model.f_last_check_state
              },
            },{resolveMsg: null, rejectMsg: null}, -1).then(
              (respanse) => {
              if (respanse.data.length >= 5000) {
              this.$showMessage("数据量太大，请重新选择")
            } else {
              _this.checkrows = []
              for (let j = 0; j < respanse.data.length; j++) {
                _this.checkrows.push(respanse.data[j])
              }
              console.log(_this.checkrows);
            }
              })
          }
        },
        addChecked(item) {
          console.log(item);
          if (!item.checked) {
            this.checkrows.push(item);
            console.log(this.checkrows);
          } else {
            for (let i = 0; i < this.checkrows.length; i++) {
              if (item.f_userinfo_id == this.checkrows[i].f_userinfo_id) {
                this.checkrows.splice(i, 1)
              }
            }
          }
        },
        ToshowModal() {
          this.creatPlanWay = 'single'
          if (this.checkrows.length > 0) {
            this.checkExist()
            this.showModal = true
          } else {
            this.$showMessage("请选择需要安检的用户!")
          }
        },
        FiltrationPlan(){
          if(this.model.rows.length == 0){
            this.$showMessage("暂无可生成的计划！")
            return
          }
          this.creatPlanWay = 'filtration'
          this.checkExist()
          this.showModal = true
        },
        AllPlan(orgs) {
          if(this.model.rows.length == 0){
            this.$showMessage("暂无可生成的计划！")
            return
          }
          this.creatPlanWay = 'all'
          this.checkExist()
          this.showModal = true
          this.getCheckerInfo(orgs)
        },
        // 安检员任务完成情况
        getCheckerInfo(orgs){
          this.model2.search(`1=1`)
        },
        cancel() {
          this.showModal = false
          this.showAddModal = false
        },
        areas() {
          let rs = []
          for (let i = 0; i < this.$login.f.f_allArea.length; i++) {
            let temp = {
              label: this.$login.f.f_allArea[i].label,
              value: this.$login.f.f_allArea[i].label
            }
            rs.push(temp)
          }
          return [{label: '全部', value: ''}, ...rs]
        },
        initAdjustables() {
          console.log('初始化调压箱', this.$GetSaleParam)
          let arr = []
          let filter = this.$login.f.orgid
          this.$GetSaleParam.getAdjustable(filter).forEach((item) => {
            let temp = {}
            temp.label = `[${item.value.f_adjustable_id}]-${item.label}`
            temp.value = `${item.value.id}`
            arr.push(temp)
          })
          this.adjustables = [{label: '全部', value: ''}, ...arr]
        },
        getRes(obj){
          this.model.f_filialeids = this.$login.convertToIn(obj.resids);
          console.log(this.model.f_filialeids)
        },
        checkExist(){
          co(checkItems(this))
        }
      },

      watch: {
        'model.f_usertype'(val){
          if(val == '民用'){
            this.model.safeCycle = this.civil
          }else if(val == '非民用'){
            this.model.safeCycle = this.civilian
          }
        },
        'model.SafeCheckXZ'(val) {
          if (val == '已安检') {
            this.model.SafeCheckCQ = ''
            this.model.SafeCheckSX = {
              f_check_end: '', // 安检时间  查询起始条件
              f_check_start: ''// 安检时间  查询终止条件
            }
          } else {
            // 当使用筛选条件时，清空不需要的条件选项
            this.$refs.paged.$refs.criteria.model.f_check_start = ''
            this.$refs.paged.$refs.criteria.model.f_check_end = ''
            this.$refs.paged.$refs.criteria.model.f_last_check_state = ''
            this.model.month = ''
            if(val == '应安检'){
              this.model.f_shouldcheck_start = Util.toStartDateString()
              this.model.f_shouldcheck_end = Util.toStandardDateString()
            }
            // this.model.f_checktime_start = ''
            // this.model.f_checktime_end = ''
          }
        },
        // 'checker'(val){
        //   console.log("checker",this.checker)
        //   if(val){
        //     this.checkboxModel.f_checker = this.checker[0].name
        //     this.checkboxModel.f_checker_id = this.checker[0].id
        //   }
        // },
        'model.rows'() {
          // if (!this.checkedAll) {//实现反选/
          // this.checkrows = [];
          for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
            var k = false
            for (let j = 0; j < this.checkrows.length; j++) {
              if (this.$refs.paged.model.rows[i].f_userinfo_id == this.checkrows[j].f_userinfo_id) {
                this.$set('$refs.paged.model.rows[' + i + '].checked', true)
                k = true
                continue
              }
            }
            if (!k) {
              this.$set('$refs.paged.model.rows[' + i + '].checked', false)
            }
          }
          // } else {//实现全选
          //   // this.checkboxModel = {};
          //   for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
          //     this.$set('$refs.paged.model.rows[' + i + '].checked', true)
          //   }
          // }
        }
      },
      computed: {
        getCondition(){
          console.log('this.orderitem===>',this.model.orderitem)
          return {condition: `${this.$refs.paged.$refs.criteria.condition}`,orderitem:`${this.model.orderitem}`,
            f_filialeid:this.model.f_filialeid,month: this.model.month,SafeCheckSX:this.model.SafeCheckSX,
              f_usertype:this.model.f_usertype,f_last_check_state:this.model.f_last_check_state,f_new_account:this.model.f_new_account,
              safeCycle:this.model.safeCycle,SafeCheckCQ:this.model.SafeCheckCQ,SafeCheckXZ:this.model.SafeCheckXZ,
              f_shouldcheck_start:this.model.f_shouldcheck_start,f_shouldcheck_end:this.model.f_shouldcheck_end}
        },
        getfield(){
          let data = {}
          this.bodyData.forEach((value, index)=>{
            data[this.bodyData[index]] = this.headData[index]
          })
          return data
        },
        selected() {
          return this.$refs.paged.$refs.grid.selected
        }
      }
    }
</script>
<style scoped>
  .newcolor{
    color:red;
  }
  .table th{
    background-color: #f2f6fa;
    color: black;
  }
  .p {
    color: #fff922;
    font-weight:bold
  }
  .p1 {
    color: #ff252f;
  }
</style>
