<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="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_card_id"
                     style="height:34px;margin-top: 8px" placeholder="卡号/出厂编号"
                     condition="dc.f_card_id = '{}'">
            </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_meternumber"
                     style="height:34px;margin-top: 8px" placeholder="表号"
                     condition="uf.f_meternumber = '{}'">
            </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_user_name"
                     style="height:34px;margin-top: 8px" placeholder="单位名称"
                     condition="ui.f_user_name = '{}'">
            </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_user_phone"
                     style="height:34px;margin-top: 8px" placeholder="用户电话"
                     condition="ui.f_user_phone = '{}'">
            </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_address"
                     style="height:34px;margin-top: 8px" placeholder="档案地址"
                     condition="uad.f_address like '%{}%'">
            </div>
          </div>
          <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_table_state"
                        :value-single="true"
                        :options='$parent.$parent.f_table_states'
                        v-model="model.f_table_state"
                        close-on-select clear-button
                        condition="uf.f_table_state  = '{}'">
              </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_user_state"
                        :value-single="true"
                        :options='$parent.$parent.f_user_states'
                        v-model="model.f_user_state"
                        close-on-select clear-button
                        condition="ui.f_user_state  = '{}'">
              </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_user_type"
                        :value-single="true"
                        :options='$parent.$parent.f_user_types'
                        v-model="model.f_user_type"
                        close-on-select clear-button
                        condition="uf.f_user_type  = '{}'">
              </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-single="true" style="margin-top: 8px"
                        :value.sync="$parent.$parent.is_reuse_state"
                        :options='$parent.$parent.is_reuse_states'
                        v-model="$parent.$parent.is_reuse_state"
                        close-on-select clear-button></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.oldareaid"-->
<!--                     style="height:34px;margin-top: 8px" placeholder="小区编号"-->
<!--                     condition="uad.oldareaid = '{}'">-->
<!--            </div>-->
          </div>
          <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_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="隐患类型"
                     >
            </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="隐患名称"
                     >
            </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_value_1"
                     style="height:34px;margin-top: 8px" placeholder="隐患名称"
              >
            </div>
          </div>
          <div class="row">
            <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">
              </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">
              </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">
              </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">
              </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_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">
              </datepicker>
            </div>
          </div>
          <div class="row">
            <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">
              </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_distinct"
                        :value-single="true"
                        :options='$parent.$parent.distincts'
                        v-model="model.f_distinct"
                        close-on-select clear-button
                        condition="ui.f_distinct  = '{}'">
              </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_distinct"
                        :value-single="true"
                        :options='$parent.$parent.distincts'
                        v-model="model.f_distinct"
                        close-on-select clear-button
                        condition="ui.f_distinct  = '{}'">
              </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_second_state"
                        :value-single="true"
                        :options='$parent.$parent.is_to_userinfos'
                        v-model="model.f_second_state"
                        close-on-select clear-button
                        condition="ui.f_second_state {}">
              </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_opera_area"
                        :value-single="true"
                        :options='$parent.$parent.f_opera_areas'
                        v-model="model.f_opera_area"
                        close-on-select clear-button
                        condition="ui.f_opera_area = '{}'">
              </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_gasproperties"
                        :value-single="true"
                        :options='$parent.$parent.f_gaspropertiess'
                        v-model="model.f_gasproperties"
                        close-on-select clear-button
                        condition="uf.f_gasproperties = '{}'">
              </v-select>
            </div>
            <div class="col-sm-4 form-group form-input-group" style="width: 20%"  v-if="$parent.$parent.use=='工商'">
              <label class="control-label">逾期状态</label>
              <v-select placeholder='逾期状态'
                        :value.sync="model.f_revise"
                        :value-single="true"
                        :options='$parent.$parent.f_revises'
                        @change="model.f_revise_day=null"
                        v-model="model.f_revise"
                        close-on-select clear-button
                        condition="f_revise_date {}">
              </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.f_checker_name"
                     style="height:34px;margin-top: 8px" placeholder="安检人"
                     condition="cp.f_checker_name 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_plan_type"
                     style="height:34px;margin-top: 8px" placeholder="计划类型"
                     condition="cpl.f_plan_type like '%{}%'">
            </div>
            <div class="col-sm-4 form-group form-input-group" style="width: 20%" v-if="model.f_revise == '>0'">
              <label class="control-label">剩余整改天数</label>
              <input type="number" class="form-control" v-model="model.f_revise_day"
                     style="height:34px;margin-top: 8px" placeholder="逾期天数" condition="f_revise_date >= 0 and f_revise_date <= {}"
              >
            </div>
          </div>
          <div class="col-sm-4 form-group form-input-group">
            <button class="button_search button_spacing" @click="search()">查询</button>
            <export-excel :data="$parent.$parent.getCondition"
                          :field="$parent.$parent.getfield"
                          sqlurl="rs/logic/SafeExportExcel"
                          sql-name="getDefectCenter"
                          template-name='隐患中心导出'
                          :choose-col="true"></export-excel>
            <export-excel :data="$parent.$parent.getCondition"
                          :field="$parent.$parent.excelColumn"
                          btn-name="隐患中心综合导出"
                          sqlurl="rs/logic/SafeExportExcel"
                          sql-name="getDefectCenterComprehensive"
                          template-name='隐患中心综合导出'
                          :choose-col="true"></export-excel>
          </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>&ensp;&ensp;档&ensp;案&ensp;地&ensp;址&ensp;&ensp;</nobr>
            </th>
            <th>
              <nobr>小区编号</nobr>
            </th>
            <th>
              <nobr>&ensp;&ensp;小&ensp;区&ensp;地&ensp;址&ensp;&ensp;</nobr>
            </th>
            <th>
              <nobr>行政区域</nobr>
            </th>
            <th>
              <nobr>运营区域</nobr>
            </th>
            <th>
              <nobr>用气性质</nobr>
            </th>
            <th>
              <nobr>用户状态</nobr>
            </th>
            <th>
              <nobr>气表状态</nobr>
            </th>
            <th v-if="$parent.$parent.$parent.use=='工商'">
              <nobr>剩余整改天数</nobr>
            </th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center">{{ row.f_card_id }}</td>
          <td style="text-align: center">{{ row.f_meternumber }}</td>
          <td style="text-align: center">{{ row.f_user_name }}</td>
          <td style="text-align: center">{{ row.f_user_phone }}</td>
          <td style="text-align: center">{{ row.f_address }}</td>
          <td style="text-align: center">{{ row.oldareaid }}</td>
          <td style="text-align: center">{{ row.f_residential_area }}</td>
          <td style="text-align: center">{{ row.f_distinct }}</td>
          <td style="text-align: center">{{ row.f_opera_area }}</td>
          <td style="text-align: center">{{ row.f_gasproperties }}</td>
          <td style="text-align: center">{{ row.f_user_state }}</td>
          <td style="text-align: center">{{ row.f_table_state }}</td>
          <td style="text-align: center"  v-if="$parent.$parent.$parent.use=='工商'">{{ row.f_revise_date }}</td>
        </template>
      </data-grid>
    </criteria-paged>


  </div>
</template>

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

export default {
  title: '安检项查询',
  props:['use'],
  data () {
    let model = new PagedList('rs/sql/getDefectCenter', 20,{con2:'this.con2'})
    return {
      show: true,
      fun: this.$login.f,
      f_check_result: '',
      checkersid: [],
      model: model,
      showModal: false,
      eliminates: [{label: '全部', value: ''}, {label: '已处理', value: '已处理'}, {label: '未处理', value: '未处理'}],
      distincts: [
        {label: '全部', value: ''},
        {label: '航天', value: '航天'},
        {label: '曲江', value: '曲江'},
        {label: '高新', value: '高新'},
        {label: '长安', value: '长安'},
        {label: '沣东', value: '沣东'},
        {label: '雁塔', value: '雁塔'},
        {label: '沣西', value: '沣西'},
        {label: '浐灞生态区', value: '浐灞生态区'}
      ],
      f_revises:[{label: '全部', value: ''},{label: '已逾期', value: '<0'},{label: '未逾期', value: '>0'}],
      f_table_states: [{label: '全部', value: ''}, {label: '正常', value: '正常'}, {
        label: '有误',
        value: '有误'
      }, {label: '拆除', value: '拆除'}, {label: '停用', value: '停用'}, {label: '弃用', value: '弃用'}, {
        label: '废除',
        value: '废除'
      }],
      f_user_states: [{label: '全部', value: ''}, {label: '正常', value: '正常'}, {label: '隐患', value: '隐患'}],
      f_user_types: [{label: '全部', value: ''}, {label: '民用', value: '民用'}, {label: '非民用', value: '非民用'}],
      is_reuse_states: [{label: '全部', value: ''}, {label: '在用', value: '在用'}],
      is_to_userinfos: [{label: '全部', value: ''}, {label: '否', value: 'is null'}, {
        label: '是',
        value: 'is not null'
      }],
      f_opera_areas: [
        {label: '全部', value: ''},
        {label: '航天', value: '航天'},
        {label: '韦曲', value: '韦曲'},
        {label: '高新', value: '高新'},
        {label: '沣东', value: '沣东'},
        {label: '中心站', value: '中心站'}
      ],
      f_gaspropertiess: [
        {label: '全部', value: ''},
        {label: '沣东小区', value: '沣东小区'},
        {label: '合表用户', value: '合表用户'},
        {label: '家庭', value: '家庭'},
        {label: '商业', value: '商业'},
        {label: '分销商', value: '分销商'},
        {label: '学校', value: '学校'},
        {label: '医院', value: '医院'},
        {label: '工业', value: '工业'},
        {label: '公福', value: '公福'},
        {label: '自用气', value: '自用气'},
        {label: '长安公司', value: '长安公司'},
        {label: '公共福利院', value: '公共福利院'},
        {label: '集中供热站', value: '集中供热站'},
        {label: '其他', value: '其他'}
      ],
      is_reuse_state: '在用',
      f_defect_types: [{label:'全部',value:''}],
      f_defect_names: [],
      // 隐患类型
      // 隐患名称
      DevicesConfig: null,
      defect_images: [],
      excelColumn: {
        "f_card_id": "卡号",
        "f_meternumber": "表号",
        "f_user_name": "用户名称",
        "f_user_phone": "用户电话",
        "f_address": "用户地址",
        "f_residential_area": "小区地址",
        "oldareaid": "小区编号",
        "f_defect_address": "隐患地址",
        "f_distinct": "行政区域",
        "f_user_state": "用户状态",
        "f_opera_area": "运营区域",
        "f_gasproperties": "用气性质",
        "f_table_state": "气表状态",
        "f_user_type": "用户类型",
        "f_defect_level": "隐患等级",
        "f_defect_type": "隐患类型",
        "f_defect_name": "隐患名称",
        "f_defect_value": "隐患描述",
        "f_data_sources": "隐患来源",
        "f_discover_time": "上报时间",
        "f_report_man": "隐患上报人",
        "f_is_eliminate": "隐患是否处理",
        "f_rectify_mode": "隐患整改方式",
        "f_rectify_time": "隐患整改时间",
        "f_task_man": "隐患整改人",
        "f_last_defect_task_time": "最后一次跟进时间",
        "f_last_defect_f_task_man": "最后一次跟进人",
      }
    }
  },
  ready () {
    if(this.use == '工商' ){
      this.$refs.paged.$refs.criteria.model.f_user_type = '非民用'
    }else {
      this.$refs.paged.$refs.criteria.model.f_user_type = '民用'
    }
    this.getSafeVueConfig()
  },
  methods: {
    onChangeConfig (val) {
      let defectTypeSet = new Set()
      this.f_defect_names = []
      this.$refs.paged.$refs.criteria.model.f_defect_name=''
      // this.$refs.paged.$refs.criteria.model
      console.log('val',val)
      console.log('this.$refs.paged.$refs.criteria.model',this.$refs.paged.$refs.criteria.model)
      if(!val){
        return
      }
      console.log('this.$refs.paged.$refs.criteria.model2222',this.$refs.paged.$refs.criteria.model)
      for (const ke in this.DevicesConfig[val]['items']) {
        defectTypeSet.add(ke)
      }
      defectTypeSet.forEach((item) => {
        this.f_defect_names.push({label: item, value: item})
      })
    },
    async getSafeVueConfig () {
      Vue.$login.jwt = 775
      let res = await Vue.resetget(`/phone/rs/vue`, {data: {}}, {resolveMsg: null, rejectMsg: null})
      let defectTypeSet = new Set()
      res.data.safecheck.DevicesSetConfig = Object.assign({}, res.data.safecheck.DevicesSetConfig, res.data.safecheck.DevicesConfig)
      if (res.data.safecheck.DevicesSetConfig) {
        this.DevicesConfig = res.data.safecheck.DevicesSetConfig
        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})
        })
      }
    },
    cancel () {
      this.showModal = false
    },
    search (args) {
      if (this.is_reuse_state === '在用') {
        args.condition += ` and (( ui.f_second_state is null and uf.f_table_state != '弃用')  or (ui.f_second_state is not null and uf.f_meternumber is not null)) `
      }
      console.log('this.$refs.paged.$refs.criteria.model', this.$refs.paged.$refs.criteria.model)
      let con2=' 1=1'
      if (this.$refs.paged.$refs.criteria.model.f_defect_name) {
        con2 += ` and dc.f_defect_name = '${this.$refs.paged.$refs.criteria.model.f_defect_name}'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_defect_type) {
        con2 += ` and dc.f_defect_type = '${this.$refs.paged.$refs.criteria.model.f_defect_type}'`
      }
      if (this.$refs.paged.$refs.criteria.model.defect_name_1) {
        con2 += ` and dc.f_defect_name like '%${this.$refs.paged.$refs.criteria.model.defect_name_1}%'`
      }
      if (this.$refs.paged.$refs.criteria.model.defect_type_1) {
        con2 += ` and dc.f_defect_type like '%${this.$refs.paged.$refs.criteria.model.defect_type_1}%'`
      }
      if (this.$refs.paged.$refs.criteria.model.defect_value_1) {
        con2 += ` and dc.f_defect_value like '%${this.$refs.paged.$refs.criteria.model.defect_value_1}%'`
      }
      //时间条件
      if (this.$refs.paged.$refs.criteria.model.f_start_discover_time) {
        con2 += ` and f_discover_time  >= '${this.$refs.paged.$refs.criteria.model.f_start_discover_time} 00:00:00'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_end_discover_time) {
        con2 += ` and f_discover_time  <= '${this.$refs.paged.$refs.criteria.model.f_end_discover_time} 23:59:59'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_start_rectify_time) {
        con2 += ` and f_rectify_time  >= '${this.$refs.paged.$refs.criteria.model.f_start_rectify_time} 00:00:00'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_end_rectify_time) {
        con2 += ` and f_rectify_time  <= '${this.$refs.paged.$refs.criteria.model.f_end_rectify_time} 23:59:59'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_last_start_defect_task_time) {
        con2 += ` and f_last_defect_task_time  >= '${this.$refs.paged.$refs.criteria.model.f_last_start_defect_task_time} 00:00:00'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_last_end_defect_task_time) {
        con2 += ` and f_last_defect_task_time  <= '${this.$refs.paged.$refs.criteria.model.f_last_end_defect_task_time} 23:59:59'`
      }

      //this.model.con2=con2
      this.$set('model.con2',con2)
      this.model.search(args.condition, args.model)
      console.log('this.$refs.paged.$refs.criteria', this.$refs.paged.$refs.criteria)
      console.log('this.model', this.model)
    },


  },
  computed: {
    getCondition () {
      return {
        condition: this.model.condition,
        con2: this.model.con2
      }
    },
    getfield () {
      let Config = {
        'f_card_id': '卡号',
        'f_meternumber': '表号',
        'f_user_name': '用户名称',
        'f_user_phone': '用户电话',
        'f_address': '档案地址',
        'oldareaid': '小区编号',
        'f_residential_area': '小区地址',
        'f_distinct': '行政区域',
        'f_user_state': '用户状态',
        'f_table_state': '气表状态',
        'f_user_type': '用户类型',
        "f_opera_area": "运营区域",
        "f_gasproperties": "用气性质",
      }
      return Config
    }
  }

}
</script>
