<template>
  <div>
    <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">
            <div class="row">
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患处理状态</label>
                <v-select placeholder='隐患处理状态'
                          :value.sync="model.f_is_eliminate"
                          :value-single="true"
                          :options='$parent.$parent.f_is_eliminates'
                          v-model="model.f_is_eliminate"
                          close-on-select clear-button
                          condition="dc.f_is_eliminate  = '{}'">
                </v-select>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患整改方式</label>
                <v-select placeholder='隐患整改方式'
                          :value.sync="model.f_rectify_mode"
                          :value-single="true"
                          :options='$parent.$parent.f_rectify_modes'
                          v-model="model.f_rectify_mode"
                          close-on-select clear-button
                          condition="dc.f_rectify_mode  = '{}'">
                </v-select>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">起始时间</label>
                <datepicker
                  :value.sync="model.f_start_discover_time"
                  placeholder='隐患发现时间' style="width:60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_start_discover_time"
                  condition="dc.f_discover_time  >= '{} 00:00:00'">
                </datepicker>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">截止时间</label>
                <datepicker
                  :value.sync="model.f_end_discover_time"
                  placeholder='隐患发现时间' style="width:60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_end_discover_time"
                  condition="dc.f_discover_time  <='{} 23:59:59'">
                </datepicker>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患来源</label>
                <v-select placeholder='隐患来源'
                          :value.sync="model.f_data_sources"
                          :value-single="true"
                          :options='$parent.$parent.sources'
                          v-model="model.f_data_sources"
                          close-on-select clear-button
                          condition="dc.f_data_sources  = '{}'">
                </v-select>
              </div>
            </div>
            <div class="row">
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患整改人</label>
                <input type="text" class="form-control" v-model="model.f_task_man"
                       style="height:34px;margin-top: 8px" placeholder="隐患整改人"
                       condition="dc.f_task_man like '%{}%'">
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患上报人</label>
                <input type="text" class="form-control" v-model="model.f_report_man"
                       style="height:34px;margin-top: 8px" placeholder="隐患上报人"
                       condition="dc.f_report_man like '%{}%'">
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">跟进时间(起)</label>
                <datepicker
                  :value.sync="model.f_last_start_defect_task_time"
                  placeholder='最后一次跟进时间范围' style="width:60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_last_start_defect_task_time"
                  condition="dc.f_last_defect_task_time  >= '{} 00:00:00'">
                </datepicker>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">跟进时间(止)</label>
                <datepicker
                  :value.sync="model.f_last_end_defect_task_time"
                  placeholder='最后一次跟进时间范围' style="width:60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_last_end_defect_task_time"
                  condition="dc.f_last_defect_task_time  <='{} 23:59:59'">
                </datepicker>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">整改时间(起)</label>
                <datepicker
                  :value.sync="model.f_start_rectify_time"
                  placeholder='最后一次整改时间范围' style="width:60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_start_rectify_time"
                  condition="dc.f_rectify_time  >= '{} 00:00:00'">
                </datepicker>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">整改时间(止)</label>
                <datepicker
                  :value.sync="model.f_end_rectify_time"
                  placeholder='最后一次整改时间范围' style="width:60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_end_rectify_time"
                  condition="dc.f_rectify_time  <='{} 23:59:59'">
                </datepicker>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患类型</label>
                <v-select placeholder='隐患类型'
                          :value.sync="model.f_defect_type"
                          :options='$parent.$parent.f_defect_types'
                          v-model="model.f_defect_type"
                          @change="$parent.$parent.onChangeConfig"
                          value-single
                          close-on-select>
                </v-select>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患名称</label>
                <v-select placeholder='隐患名称'
                          :value.sync="model.f_defect_name"
                          :options='$parent.$parent.f_defect_names'
                          v-model="model.f_defect_name"
                          value-single
                          close-on-select>
                </v-select>
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患类型</label>
                <input type="text" class="form-control" v-model="model.defect_type_1"
                       style="height:34px;margin-top: 8px" placeholder="隐患类型"
                       condition="dc.f_defect_type like '%{}%'">
              </div>
              <div class="col-sm-4 form-group form-input-group" style="width: 20%">
                <label class="control-label">隐患名称</label>
                <input type="text" class="form-control" v-model="model.defect_name_1"
                       style="height:34px;margin-top: 8px" placeholder="隐患名称"
                       condition="dc.f_defect_name like '%{}%'">
              </div>
            </div>
            <div class="row">
              <div class="col-sm-4 form-group form-input-group">
                <button class="button_search button_spacing" @click="search()">查询</button>
                <button class="btn btn-default" @click="$parent.$parent.clear()">清空</button>
                <export-excel :data="$parent.$parent.getCondition"
                              :field="$parent.$parent.getfield"
                              sqlurl="rs/logic/SafeExportExcel"
                              sql-name="getDefectPitfallCenter"
                              template-name='隐患详情导出'
                              :choose-col="true"></export-excel>
              </div>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <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">{{ row.f_defect_type }}</td>
          <td style="text-align: center">{{ row.f_defect_name }}</td>
          <td style="text-align: center">{{ row.f_defect_value }}</td>
          <td style="text-align: center">{{ row.f_defect_level }}</td>
          <td style="text-align: center">{{ row.f_data_sources }}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

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

export default {
  title: '安检项查询',
  data () {
    return {
      model: new PagedList('rs/sql/getDefectPitfallCenter', 20),
      eliminates: [{label: '全部', value: ''}, {label: '已处理', value: '已处理'}, {label: '未处理', value: '未处理'}],
      DevicesConfig: null,
      f_is_eliminates: [{label: '全部', value: ''}, {label: '未处理', value: '未处理'}, {
        label: '已上传',
        value: '已上传'
      }, {label: '已处理', value: '已处理'}],
      f_rectify_modes: [{label: '全部', value: ''}, {label: '巡检整改', value: '巡检整改'}, {
        label: '安检',
        value: '安检'
      }, {label: '用户自行整改', value: '用户自行整改'}, {label: '惠众外包', value: '惠众外包'}, {
        label: '未整改',
        value: '未整改'
      }],
      sources: [{label: '全部', value: ''}, {label: '安检', value: '安检'}, {
        label: '巡检',
        value: '巡检'
      }, {label: '工商安检', value: '工商安检'}, {label: '调压箱', value: '调压箱'}],
      f_defect_types: [],
      f_defect_names: []
    }
  },
  ready () {
    this.getSafeVueConfig()
  },
  methods: {
    search (args) {
      if (this.$refs.paged.$refs.criteria.model.f_defect_name) {
        args.condition += ` and dc.f_defect_name = '${this.$refs.paged.$refs.criteria.model.f_defect_name}'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_defect_type) {
        args.condition += ` and dc.f_defect_type = '${this.$refs.paged.$refs.criteria.model.f_defect_type}'`
      }
      this.model.search(args.condition, args.model)
    },
    clear () {
      Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
        const inputModel = this.$refs.paged.$refs.criteria.model[key]
        if (typeof inputModel == 'string') {
          this.$refs.paged.$refs.criteria.model[key] = ''
        } else if (inputModel instanceof Array) {
          this.$refs.paged.$refs.criteria.model[key] = []
        } else if (inputModel instanceof Object) {
          this.$refs.paged.$refs.criteria.model[key] = {}
        } else {
          this.$refs.paged.$refs.criteria.model[key] = ''
        }
      })
    },
    async getSafeVueConfig () {
      Vue.$login.jwt = 775
      let res = await Vue.resetget(`/phone/rs/vue`, {data: {}}, {resolveMsg: null, rejectMsg: null})
      let defectTypeSet = new Set()
      this.f_defect_types=[{label:'全部',value:''}]
      this.f_defect_names=[{label:'全部',value:''}]
      if (res.data.safecheck.DevicesConfig) {
        this.DevicesConfig = res.data.safecheck.DevicesConfig
        for (const key in this.DevicesConfig) {
          if (Object.hasOwnProperty.call(this.DevicesConfig, key)) {
            this.f_defect_types.push({label: key, value: key})
            for (const ke in this.DevicesConfig[key]['items']) {
              defectTypeSet.add(ke)
            }
          }
        }
        defectTypeSet.forEach((item) => {
          this.f_defect_names.push({label: item, value: item})
        })
      }
    },
    onChangeConfig (val) {
      if(val){
        let defectTypeSet = new Set()
        this.f_defect_names=[{label:'全部',value:''}]
        for (const ke in this.DevicesConfig[val]['items']) {
          defectTypeSet.add(ke)
        }
        defectTypeSet.forEach((item) => {
          this.f_defect_names.push({label: item, value: item})
        })
      }
    }
  },
  computed: {
    getCondition () {
      return {
        condition: `${this.$refs.paged.$refs.criteria.condition} `
      }
    },
    getfield () {
      let Config = {
        'f_defect_level': '隐患等级',
        'f_defect_type': '隐患类型',
        'f_defect_name': '隐患名称',
        'f_defect_value': '隐患描述',
        'f_data_sources': '隐患来源'
      }
      return Config
    }
  }

}
</script>
