<template>
  <div class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="form-group col-sm-4">
              <label class="font_normal_body">客户编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_userinfo_code"
                     placeholder='客户编号'
                     condition="f_userinfo_code = '{}'" @keyup.enter="search()">
            </div>
            <div class="form-group col-sm-4">
              <label class="font_normal_body">安检起始</label>
              <datepicker
                :value.sync="$parent.$parent.f_start_time"
                v-model="$parent.$parent.f_start_time"
                placeholder='安检起始' style="width:60%"
                :show-rest-button="reset"
                :format="'yyyy-MM-dd'"
              >
              </datepicker>
            </div>
            <div class="form-group col-sm-4">
              <label class="font_normal_body">安检截止</label>
              <datepicker
                :value.sync="$parent.$parent.f_end_time"
                placeholder='安检截止' style="width:60%"
                v-model="$parent.$parent.f_end_time"
                :format="'yyyy-MM-dd'"
                :show-rest-button="reset">
              </datepicker>
            </div>
          </div>
          <div class="row">
            <div class="form-group col-sm-4">
              <label class="font_normal_body">安检状态</label>
              <v-select style="width:60% "
                        class="select select_list"
                        :value.sync="$parent.$parent.f_state"
                        v-model="$parent.$parent.f_state"
                        :options='$parent.$parent.checkState'
                        placeholder='安检状态'
                        close-on-select
                        value-single
              >
              </v-select>
            </div>
            <div class="form-group col-sm-4">
              <v-select style="width:60% "
                        v-if="$parent.$parent.f_state=='未入户'"
                        class="select select_list"
                        :value.sync="$parent.$parent.f_noruhu"
                        v-model="$parent.$parent.f_noruhu"
                        :options='$parent.$parent.noChecks'
                        placeholder='请选择状态'
                        :multiple="true"
              >
              </v-select>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="form-group col-sm-4">
              <label class="font_normal_body">气表编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_meternumber" placeholder='气表编号'
                     condition="f_meternumber = '{}'" @keyup.enter="search()">
            </div>
            <div class="form-group col-sm-4">
              <label class="font_normal_body">气表品牌</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_meter_brand" placeholder='气表品牌'
                     condition="f_meter_brand like '%{}%'" @keyup.enter="search()">
            </div>
          </div>
          <div class="row">
            <div class="form-group col-sm-2 button-range">
              <button class="button_search" style="margin-right: 10px" @click="search()" v-el:cba>查询</button>
              <export-excel-safe :data="$parent.$parent.exportParameter"
                                 :field="$parent.$parent.excelHeaders"
                                 sqlurl="rs/logic/SafeExportExcel" sql-name="getCheckPlanUserList"
                                 template-name='安检情况明细' :choose-col="true">
              </export-excel-safe>
              <!--展示下方条件或隐藏-->
              <div class="span" style="float:right;">
                <div class="button_spacing" style="float: right;margin-left:10px"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()">
                </div>
              </div>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" class="table_sy" 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>
            <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'>
          <tr>
            <td style="text-align:center;">{{ $index + 1 }}</td>
            <td style="text-align:center"><span>{{ row.f_userinfo_code }}</span></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_meternumber }}</td>
            <td style="text-align:center">{{ row.f_meter_brand }}</td>
            <td style="text-align:center">{{ row.f_user_type }}</td>
            <td style="text-align:center;white-space: nowrap;">{{ row.f_address }}</td>
            <td style="text-align:center">{{ row.f_state }}</td>
            <td style="text-align:center">{{ row.f_last_check_state }}</td>
            <td style="text-align:center">{{ row.f_last_check_date }}</td>
<!--            <td style="text-align:center">{{ row.f_defect_content }}</td>-->
            <td style="text-align:center">{{ row.f_check_number }}</td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'

  export default {
    title: '安检情况明细',
    data() {
      return {
        model: new PagedList('rs/sql/getCheckPlanUserList', 50, {}),
        condition: '',
        getfield: {},
        f_state:'',
        f_noruhu:'',
        userType: [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')],
        entry_status: [{label: '全部', value: ''}, {label: '安检入户', value: "f_entry_status = '入户'"}, {
          label: '拒绝安检',
          value: "f_entry_status = '拒检'"
        }, {label: '到访不遇', value: "f_entry_status = '到访不遇'"}, {
          label: '尚未使用',
          value: "f_entry_status = '未使用'"
        }, {label: '复访不遇', value: "f_entry_status ='到访不遇' and f_check_version >2 "}],
        checkState: [{label: '全部', value: ''}, {label: '入户', value: '入户'}, {label: '未入户', value: '未入户'}],
        noChecks: [{label: '未检', value: '未检'}, {label: '到访不遇', value: '到访不遇'}, {
          label: '拒检',
          value: '拒检'
        }, {label: '复访不遇', value: '复访不遇'}],
        config: {
          excelHeaders: {
            'f_price_name': '气价名称', 'f_userinfo_code': '客户编号', 'f_user_name': '客户姓名', 'f_address': '客户地址',
            'f_orgname': '组织机构'
          }
        },
        criteriaShow: false,
        excelHeaders: {
          'f_userinfo_code': '用户编号',
          'f_user_name': '用户姓名',
          'f_user_phone': '用户电话',
          'f_meternumber': '气表编号',
          'f_meter_brand': '气表品牌',
          'f_user_type': '用户类型',
          'f_residential_area': '小区名称',
          'f_address': '用户地址',
          'f_state': '是否已检',
          'f_last_check_state': '安检状态',
          'f_last_check_date': '安检时间',
          // 'f_defect_content': '隐患状态',
          'f_check_number': '上报条数',
          'openstate': '开户状态',
          'f_checker_namehp': '协助人员'
        }
      }
    },
    props: ['checkrow','f_start_time','f_end_time'],
    ready() {

      this.search()
      this.getfield = this.config.excelHeaders
    },
    methods: {

      search() {
        this.$refs.paged.$refs.criteria.search()
      },
      selfSearch(args) {
        if (!this.checkrow) {
          return
        }
        // args.condition = `${args.condition} and f_price_id = '${this.priceid}'`
        // case when cpf_last_check_state='到访不遇' and cpf_check_version >2  then 1 else 0 end f_fufang,
        //tag
        var cc = this.$parent.$refs.list.model.condition
        //tag
        let tempArr = cc.split("and");
        let tempStr = ""
        for (let tempArrKey in tempArr) {
          if (tempArr[tempArrKey].indexOf("time") != -1) {
            continue
          }
          tempStr += " and " + tempArr[tempArrKey]
        }
        if (this.f_state) {
          if (this.f_state == '入户') {
            args.condition = args.condition + " and f_state='已检' and f_last_check_state='入户'"
          } else if (this.f_state == '未入户') {
            if (this.f_noruhu && this.f_noruhu.length == 1) {
              if (this.f_noruhu[0] == '复访不遇') {
                args.condition += "and f_state='已检' and f_last_check_state='到访不遇' and f_check_version > 1 "
              } else  if(this.f_noruhu[0]=='到访不遇'){
                args.condition += "and f_state='已检' and f_last_check_state='到访不遇' and f_check_version = 1 "
              }else{
                args.condition += this.f_noruhu[0] == '未检' ? " and f_state='未检'" : ` and f_state='已检' and f_last_check_state='${this.f_noruhu[0]}'`
              }
            } else if (this.f_noruhu && this.f_noruhu.length == 2) {
              if (this.f_noruhu.indexOf('未检') != -1) {
                //
                if (this.f_noruhu.indexOf('到访不遇') != -1) {
                  args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and f_last_check_state='到访不遇'))"
                } else if (this.f_noruhu.indexOf('复访不遇') != -1) {
                  args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and f_last_check_state = '到访不遇' AND f_check_version >2))"
                } else {
                  args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and f_last_check_state='拒检'))"
                }
              } else {
                if (this.f_noruhu.indexOf('复访不遇') != -1) {
                  if (this.f_noruhu.indexOf('到访不遇') != -1) {
                    args.condition = args.condition + "  and (f_state='已检' and (f_last_check_state='到访不遇'))"
                  } else {
                    args.condition = args.condition + " and f_state='已检' and ((f_last_check_state='到访不遇' and  f_check_version >2) or f_last_check_state='拒检' )"
                  }
                } else {
                  args.condition = args.condition + " and (f_state='已检' and (f_last_check_state='到访不遇' or f_last_check_state='拒检'))"
                }
              }
            } else {
              if (this.f_noruhu.indexOf('未检') != -1) {
                args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and (f_last_check_state='到访不遇' or f_last_check_state='拒检')))"
              } else {
                args.condition = args.condition + " and (f_state='已检' and (f_last_check_state='到访不遇' or f_last_check_state='拒检'))"
              }
            }
          }
        }
        if(this.f_end_time) {
          args.condition = args.condition + ` and f_last_check_date < '${this.f_end_time} 23:59:59'`
        }
        if(this.f_start_time) {
          args.condition = args.condition + ` and f_last_check_date > '${this.f_start_time} 00:00:00'`
        }
        this.condition = args.condition
        args.condition = args.condition + tempStr
        //tag
        var self = this
        this.$parent.$refs.list.nameForSql.forEach(function (s) {

          if (s != 'f_user_number' && self.checkrow[s] != 'undefined' && self.checkrow[s]) {
            var rowsname = self.checkrow[s].replace('*&', '')
            args.condition = args.condition + " and  s." + s + "=\'" + rowsname + "\'"
          }
        })
        if ((!this.checkrow.f_residential_area || args.condition.indexOf("s.f_residential_area='null'") != -1) && this.$parent.$refs.list.nameForSql.includes('f_residential_area')) {
          if (!this.checkrow.f_residential_area) {
            args.condition += " and s.f_residential_area IS NULL"
          } else if (args.condition.indexOf("s.f_residential_area='null'") != -1) {
            args.condition = args.condition.replace("s.f_residential_area='null'", "s.f_residential_area IS NULL")
          }
        } else if (this) {
          args.condition
        }
        /* else {
          args.condition += " and s.f_table_state='正常'"
            }    */
        //tag
        this.model.search(args.condition, args.model)
      },
      dealmsg(val) {
        //tag
        this.$parent.f_userinfo_id = val.f_userinfoid
        this.$parent.listpage = false
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
    },
    watch: {
      'checkrow'() {
        this.search()
      }
    }, computed: {
      // 导出到Excel的条件构造
      exportParameter() {
        return {
          condition: this.$refs.paged.model.condition,
          startyear: this.$parent.$refs.list.model.params.startyear,
          startmon: this.$parent.$refs.list.model.params.startmon,
          endyear: this.$parent.$refs.list.model.params.endyear,
          endmon: this.$parent.$refs.list.model.params.endmon,
          f_user_type: this.$parent.$refs.list.model.params.f_user_type
        }
      }
    }
  }
</script>
