<template>
    <div>
        <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-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
                            <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="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
                            <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':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}" v-if="!$parent.$parent.$parent.showItem">
                            <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="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
                            <div class="span" style="float: right;margin-top: 8px">
                              <export-excel-safe   :data="{condition: $parent.$parent.model.condition,f_filialeids:$parent.$parent.model.f_filialeids}"
                                              :field="$parent.$parent.getfield"
                                              sqlurl="rs/logic/SafeExportExcel" sql-name="GetCheckPaperByDefect" template-name='安检计划明细' :choose-col="true"></export-excel-safe>

                              <button class="button_search button_spacing" @click="search">查询</button>
                                <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>
                            </div>
                        </div>
                    </div>
                    <div class="row" v-if="$parent.$parent.criteriaShow">
                        <div :class="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}" v-if="$parent.$parent.$parent.showItem">
                            <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="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
                            <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 :class="{'col-sm-8':$parent.$parent.$parent.showItem,'col-sm-6':!$parent.$parent.$parent.showItem}">
                            <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>
            </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>
                    </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_user_name}}</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>
                </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'

  export default {
    title: '隐患查询',
    data() {
      let model = new PagedList('rs/sql/GetCheckPaperByDefect', 20, {
        f_filialeids: 'this.f_filialeids'
      })
      model.f_filialeids = '(' + this.$login.f.orgid + ')'
      return {
        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: [],
        thead: '',
        headData: ['入户状态','用户名','用户地址','安检时间','安检员','计划名','是否有隐患','隐患数','隐患情况'],
        bodyData: ['f_entry_status','f_user_name','f_address','f_offsite_time','f_checker_name','f_plan_name','f_has_defect','f_defect_count','f_defect_content']
      }
    },
    methods: {
      hiddenr() {
        this.$parent.showItem = !this.$parent.showItem
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      search(args) {
        if (this.f_check_plan.f_plan_name) {
          args.condition += ` and f_check_plan_id = '${this.f_check_plan.id}'`
        }
        this.model.search(args.condition, args.model)
      },
      getRes(obj) {
        this.model.f_filialeids = this.$login.convertToIn(obj.resids);
        //tag
      },
      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 = '有隐患'
    },
    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>
