<template>
  <div class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria'  @condition-changed='$parent.search' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row" v-if="!$parent.$parent.$parent.showItem">
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">安检计划</label>
              <input type="text" class="input_search" v-model="$parent.$parent.f_check_plan.f_plan_name"
                     @click="$parent.$parent.showChooser"
                     style="width: 60%" placeholder="安检计划" readonly="readonly">
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">存在隐患</label>
              <v-select style="width:60% "
                        class="select select_list"
                        :value.sync="model.f_has_defect"
                        v-model="model.f_has_defect"
                        :options='$parent.$parent.hasDefectOptions'
                        :placeholder='model.f_has_defect'
                        condition="f_has_defect = '{}'"
                        close-on-select>
              </v-select>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">用户编号</label>
              <input type="text" class="input_search" v-model="model.f_userinfo_code"
                     condition="f_userinfo_code = '{}'"
                     style="width: 60%" placeholder="用户编号">
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">结果查询</label>
              <v-select
                placeholder='处理结果查询'
                :value-single="true"
                class="select select_list" style="width: 60%"
                v-model='model.f_repaired'
                :value.sync="model.f_repaired"
                :options='$parent.$parent.repaired'
                close-on-select clear-button> </v-select>
            </div>
            <div class=" col-sm-4 form-group button-range"  >
              <div class="span" style="float: right;margin-top: 8px">
                                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_left':$parent.$parent.$parent.showItem,'button_shrink_right':!$parent.$parent.$parent.showItem}"
                     @click="$parent.$parent.hiddenr()"></div>
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()"></div>
                <button style="float: right" class="button_search button_spacing" @click="search">查询</button>
                <div style="float: right">
                <export-excel-safe   :data="{condition: $parent.$parent.model.condition,f_filialeids:$parent.$parent.model.f_filialeids,f_defect_content:$parent.$parent.model.f_defect_content}"
                                :field="$parent.$parent.getfield"
                                progress="safeGetExportProgress"
                                sqlurl="rs/logic/SafeExportExcel" sql-name="GetCheckPaperByDefect" template-name='安检隐患明细' :choose-col="true"></export-excel-safe>
                </div>
              </div>
            </div>
          </div>
          <div class="row" v-if="$parent.$parent.criteriaShow&&!$parent.$parent.$parent.showItem">
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">片&emsp;&emsp;区</label>
              <v-select class="select_list select"
                        placeholder='片区/管理站' style="width: 60%"
                        :value.sync="model.f_slice_area" v-model="model.f_slice_area"
                        :options='$parent.$parent.sliceArea'
                        filer-key="name"
                        close-on-select clear-button
                        condition="f_slice_area='{}'"
                        @change="$parent.$parent.getmeterbook(model.f_slice_area)"
                        :value-single="true">
              </v-select>
            </div>
            <div :class="$parent.$parent.style2" style="margin: 0">
              <role-selector-safe
                role-name="安检员"
                role-lable="安&nbsp;检&nbsp;员&nbsp;"
                @re-res="$parent.$parent.getRes"
                :value.sync="model.f_checker_id"
                v-model="model.f_checker_id"
                condition="f_checker_name = '{}'">
              </role-selector-safe>
            </div>

            <div :class="$parent.$parent.style">
              <label class="font_normal_body">抄&ensp;表&ensp;册</label>
              <v-select class="select_list select"
                        placeholder='抄表册' style="width: 60%"
                        :value.sync="model.f_meter_book_num" v-model="model.f_meter_book_num"
                        :options='$parent.$parent.cbc'
                        filer-key="name"
                        close-on-select clear-button
                        condition="f_meter_book_num={}"
                        :value-single="true">
              </v-select>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">起始时间</label>
              <datepicker
                placeholder='起始时间' style="width:60%"
                :disabled-days-of-week="[]"
                :format="'yyyy-MM-dd'"
                :show-rest-button="reset"
                :value.sync="model.f_start_time"
                v-model="model.f_start_time"
                condition="f_offsite_time >= '{} 00:00:00'">
              </datepicker>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">结束时间</label>
              <datepicker
                placeholder='结束时间' style="width:60%"
                :disabled-days-of-week="[]"
                :format="'yyyy-MM-dd'"
                :show-rest-button="reset"
                :value.sync="model.f_end_time"
                v-model="model.f_end_time"
                condition="f_offsite_time <= '{} 23:59:59'">
              </datepicker>
            </div>


          </div>
          <div class="row" v-if="$parent.$parent.$parent.showItem">
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">安检计划</label>
              <input type="text" class="input_search" v-model="$parent.$parent.f_check_plan.f_plan_name"
                     @click="$parent.$parent.showChooser"
                     style="width: 60%" placeholder="安检计划" readonly="readonly">
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">存在隐患</label>
              <v-select style="width:60% "
                        class="select select_list"
                        :value.sync="model.f_has_defect"
                        v-model="model.f_has_defect"
                        :options='$parent.$parent.hasDefectOptions'
                        :placeholder='model.f_has_defect'
                        condition="f_has_defect = '{}'"
                        close-on-select>
              </v-select>
            </div>

            <div class=" col-sm-4 form-group button-range">
              <div class="span" style="float: right;margin-top: 8px;margin-right: 50px">
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_left':$parent.$parent.$parent.showItem,'button_shrink_right':!$parent.$parent.$parent.showItem}"
                     @click="$parent.$parent.hiddenr()"></div>
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()"></div>
                <button style="float: right" class="button_search button_spacing" @click="search">查询</button>
                <div style="float: right">
                  <export-excel-safe   :data="{condition: $parent.$parent.model.condition,f_filialeids:$parent.$parent.model.f_filialeids}"
                                  :field="$parent.$parent.getfield"
                                  progress="safeGetExportProgress"
                                  sqlurl="rs/logic/SafeExportExcel" sql-name="GetCheckPaperByDefect" template-name='安检计划明细' :choose-col="true"></export-excel-safe>
                </div>
              </div>
            </div>



          </div>
          <div class="row" v-if="$parent.$parent.criteriaShow&&$parent.$parent.$parent.showItem">
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">用户编号</label>
              <input type="text" class="input_search" v-model="model.f_userinfo_code"
                     condition="f_userinfo_code = '{}'"
                     style="width: 60%" placeholder="用户编号">
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">结果查询</label>
              <v-select
                placeholder='处理结果查询'
                :value-single="true"
                class="select select_list" style="width: 60%"
                v-model='model.f_repaired'
                :value.sync="model.f_repaired"
                :options='$parent.$parent.repaired'
                close-on-select clear-button> </v-select>
            </div>

            <div :class="$parent.$parent.style2" style="margin: 0">
              <role-selector-safe
                role-name="安检员"
                role-lable="安&nbsp;检&nbsp;员&nbsp;"
                @re-res="$parent.$parent.getRes"
                :value.sync="model.f_checker_id"
                v-model="model.f_checker_id"
                condition="f_checker_name = '{}'">
              </role-selector-safe>
            </div>
          </div>
          <div class="row" v-if="$parent.$parent.criteriaShow && $parent.$parent.$parent.showItem">
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">片&emsp;&emsp;区</label>
              <v-select class="select_list select"
                        placeholder='片区/管理站' style="width: 60%"
                        :value.sync="model.f_slice_area" v-model="model.f_slice_area"
                        :options='$parent.$parent.sliceArea'
                        filer-key="name"
                        close-on-select clear-button
                        condition="f_slice_area='{}'"
                        @change="$parent.$parent.getmeterbook(model.f_slice_area)"
                        :value-single="true">
              </v-select>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">抄&ensp;表&ensp;册</label>
              <v-select class="select_list select"
                        placeholder='抄表册' style="width: 60%"
                        :value.sync="model.f_meter_book_num" v-model="model.f_meter_book_num"
                        :options='$parent.$parent.cbc'
                        filer-key="name"
                        close-on-select clear-button
                        condition="f_meter_book_num={}"
                        :value-single="true">
              </v-select>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">起始时间</label>
              <datepicker
                placeholder='起始时间' style="width:60%"
                :disabled-days-of-week="[]"
                :format="'yyyy-MM-dd'"
                :show-rest-button="reset"
                :value.sync="model.f_start_time"
                v-model="model.f_start_time"
                condition="f_offsite_time >= '{} 00:00:00'">
              </datepicker>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">结束时间</label>
              <datepicker
                placeholder='结束时间' style="width:60%"
                :disabled-days-of-week="[]"
                :format="'yyyy-MM-dd'"
                :show-rest-button="reset"
                :value.sync="model.f_end_time"
                v-model="model.f_end_time"
                condition="f_offsite_time <= '{} 23:59:59'">
              </datepicker>
            </div>


          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid class="list_area table_sy">
        <template partial='head'>
          <tr>
            <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 style="text-align: center;white-space:nowrap;">{{row.f_entry_status}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_userinfo_code}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_user_name}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_user_phone}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_residential_area}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_address}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_offsite_time}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_checker_name}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_plan_name}}</td>
          <td style="text-align: center;white-space:nowrap;">{{row.f_has_defect}}</td>
          <td style="text-align: center;white-space:nowrap;" >{{row.f_defect_count}}</td>
          <td style="text-align: center;white-space:nowrap;" >{{row.f_is_repaired}}</td>
<!--          <td style="text-align: center;white-space:nowrap;">{{row.f_repair_count}}</td>-->

        </template>
      </data-grid>
    </criteria-paged>

    <modal :show.sync="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="this.$login.f" v-ref:chooser></plan-chooser>
      </div>
      <div slot="modal-footer" class="modal-footer" style="text-align: center">
        <button class="button_search" @click="ok">确认</button>
        <button class="button_search" @click="cancel">取消</button>
      </div>
    </modal>
  </div>
</template>
<script>
  import {PagedList} from 'vue-client'
  import HttpResetClass from "vue-client/src/plugins/HttpResetClass";
  import co from 'co'

  let select = function* (self) {
    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})
    //tag
    self.checkersid.push({label: '全部', value: ''})
    getGasman.data.forEach((checker) => {
      self.checkersid.push({label: checker.name, value: checker.name})
    })
  }
  export default {
    title: '隐患查询',
    data() {
      let model = new PagedList('rs/sql/GetCheckPaperByDefect', 20, {
        f_filialeids: 'this.f_filialeids',f_defect_content: 'this.f_defect_content'
      })
      model.f_filialeids = '(' + this.$login.f.orgid + ')'
      model.f_defect_content="CASE WHEN CHARINDEX( '\"result\":\"正常\"', f_defect_content ) > 0 THEN '无隐患' ELSE replace(replace(replace( replace( RIGHT ( f_defect_content, len( f_defect_content ) - 26 ), substring('{}',2,1), '' ), substring('{}',1,1), '' ),']',''),'\"','') END"
      return {
        repaired:[{label: '未处理', value: '未修'}, {label: '已处理', value: '已处理'}],
        hasDefectOptions: [{label: '全部', value: ''}, {label: '有隐患', value: '有隐患'}, {label: '无隐患', value: '无隐患'}],
        f: this.$login.f,
        model: model,
        criteriaShow: false,
        showModal: false,
        f_check_plan: {f_plan_name: '', id: ''},
        sumsmodel: {},
        all: false,
        fields: {},
        modelval: [],
        checkersid: [],
        sliceArea: [],
        cbc:[],
        cbcs:[],
        thead: '',
        headData: ['入户状态','用户编号','用户名','用户电话','用户小区','用户地址','安检时间','安检员','计划名','是否有隐患','隐患数','隐患情况'],
        bodyData: ['f_entry_status','f_userinfo_code','f_user_name','f_user_phone','f_residential_area','f_address','f_offsite_time','f_checker_name','f_plan_name','f_has_defect','f_defect_count','f_defect_content']
      }
    },
    props:{
      style: {
        type: String,
        default: 'col-sm-2 form-group'
      },
      style2: {
        type: String,
        default: 'col-sm-4 form-group'
      }

    },

    methods: {
      getmeterbook(val){
        this.cbc=[]
        this.cbc.push({
          label:'全部',
          value:''
        })
        if (val==''){

          this.cbcs.forEach(res=>{
            this.cbc.push({
              label:res.f_book_name,
              value:res.id
            })
          })
        }else{
          this.cbcs.forEach(res=>{
            if (res.f_book_slice_area==val){
              this.cbc.push({
                label:res.f_book_name,
                value:res.id
              })
            }
          })
        }
      },

      Mreadibook(val){
        //tag
        //tag
        this.sliceArea=[]
        if (val){
          new HttpResetClass().load('POST',`/rs/sql/tel_singleTable_OrderBy`, {data: {
              items: 'name',
              tablename: 't_zone',
              condition: `parentid  in ${val}`,
              orderitem: 'id'
            }}, {resolveMsg: null, rejectMsg: null}).then((res) => {
            this.sliceArea.push({
              label:'全部',
              value:''
            })
            res.data.forEach(ress=>{
              this.sliceArea.push({
                label:ress.name,
                value:ress.name
              })
            })

          })
        }
      },
      getAllMeterBook(val){
        //tag
        //tag
        this.cbcs=[]
        if (val){
          new HttpResetClass().load('POST',`/rs/sql/tel_singleTable_OrderBy`, {data: {
              items: 'f_book_name,f_book_slice_area,id',
              tablename: 't_meter_book',
              condition: `f_filiale_id  in ${val}`,
              orderitem: 'id'
            }}, {resolveMsg: null, rejectMsg: null}).then((res) => {
            res.data.forEach(ress=>{
              this.cbcs.push({
                id:ress.id,
                f_book_name:ress.f_book_name,
                f_book_slice_area:ress.f_book_slice_area
              })
            })
            this.cbc=[]
            this.cbc.push({
              label:'全部',
              value:''
            })
            this.cbcs.forEach(res=>{
              this.cbc.push({
                label:res.f_book_name,
                value:res.id
              })
            })
          })
        }
      },
      hiddenr() {
        this.$parent.showItem = !this.$parent.showItem
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      search(args) {
        //tag
        if (this.f_check_plan.f_plan_name) {
          args.condition += ` and f_check_plan_id = '${this.f_check_plan.id}'`
        }
        if(args.model.f_repaired == '未修'){
          args.condition += ` and f_defect_count != f_is_repaired `
        }
        if(args.model.f_repaired == '已处理'){
          args.condition += ` and f_defect_count = f_is_repaired `
        }
        this.model.search(args.condition, args.model)
      },
      getRes(obj) {
        this.model.f_filialeids = this.$login.convertToIn(obj.resids);
        this.Mreadibook(this.model.f_filialeids)
        this.getAllMeterBook(this.model.f_filialeids)
      },
      showChooser() {
        this.showModal = true
      },
      ok() {
        this.showModal = false
        if (this.$refs.modal.$children[0].selectedRow.id) {
          this.f_check_plan = this.$refs.modal.$children[0].selectedRow
        }
      },
      cancel() {
        this.showModal = false
        this.f_check_plan = {f_plan_name: '', id: ''}
      }
    },
    ready() {
      this.$refs.paged.$refs.criteria.model.f_has_defect = '有隐患'
      let gen = select(this)
      // this.Mreadibook(`('${this.$login.f.orgid}')`)
      co(gen)
    },
    computed: {
      selected() {
        return this.$refs.paged.$refs.grid.selected
      },
      getfield(){
        //tag
        let data = {}
        this.bodyData.forEach((value, index)=>{
          data[this.bodyData[index]] = this.headData[index]
        })
        //合计字段打印
        this.tfoot = `<tr><th colspan=${this.modelval.length}>全表汇总信息: `
        if(this.sumsmodel){
          Object.keys(this.sumsmodel).forEach((key) => {
            this.tfoot += this.fields[key] + '合计: ' + `<font color="blue">${this.sumsmodel[key]} </font>`
          })
        }else{
          this.tfoot += '暂无'
        }
        this.tfoot += '</th></tr>'
        return data
      }
    }
  }
</script>
