<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:criteria>
        <div partial>
          <!--          <p class="text-center" style="padding: 7px;"><b>安检计划项明细</b></p>-->
          <ul class="nav nav-tabs">
            <li role="presentation" class="active"><a style="	color: #5ac0d9 !important;">无计划安检单明细</a></li>
          </ul>
          <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 class="form-group col-sm-4 button-range">
              <button class="button_search" style="margin-right: 10px" @click="search()">查询</button>
              <export-excel-safe :data="$parent.$parent.exportParameter"
                                 :field="$parent.$parent.excelHeaders"
                                 sqlurl="rs/logic/SafeExportExcel" sql-name="noplanItem"
                                 template-name='无计划安检单明细' :choose-col="true">
              </export-excel-safe>
              <!--<export-excel-safe-->
              <!--:data="{condition: $parent.$parent.condition}"-->
              <!--:field="$parent.$parent.getfield"-->
              <!--sqlurl="rs/logic/saleExport" sql-name="getGasUser" :template-name="$parent.$parent.pricename+'气价使用人'"-->
              <!--:choose-col="true"></export-excel-safe>-->

            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' class="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">
            <nobr>{{ row.f_userinfo_code }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_user_name }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_user_phone }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_residential_area }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_corporate_name }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_address }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_entry_status }}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{ row.f_state }}</nobr>
          </td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

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

  export default {
    title: '无计划安检单列表',
    data() {
      return {
        model: new PagedList('rs/sql/noplanItem', 20),
        checkState: [{label: '全部', value: ''}, {label: '入户', value: '入户'}, {label: '未入户', value: '未入户'}],
        noChecks: [{label: '未检', value: '未检'}, {label: '到访不遇', value: '到访不遇'}, {
          label: '拒检',
          value: '拒检'
        }, {label: '复访不遇', value: '复访不遇'}],
        excelHeaders: {
          //用户编号	用户名称	用户电话	安检次数	用户类型	小区名称	地址	安检日期	安检状态	安检结果

          'f_userinfo_code': '用户编号',
          'f_user_name': '用户名称',
          'f_user_phone': '用户电话',
          //'count_item':'安检次数',
          'f_residential_area': '小区名称',
          'f_corporate_name': '单位名称',
          'f_address': '地址',
          'f_state': '是否已检',
          'f_entry_status': '安检状态'
        },
        f_state: '',
        f_noruhu: ''
      }
    },
    props: ['check', 'checkstatus', 'checkstart', 'checkend'],
    watch: {
      'check'(val) {
        let condition
        //tag

        condition = `f_checker_name='${this.check.f_checker_name}' and f_state!='作废'`

        if (this.checkstatus) {
          condition += ` and f_entry_status = '${this.f_entry_status}'`
        }
        if (this.checkstart) {
          condition += ` AND f_offsite_time >= '${this.checkstart} 00:00:00' `
        }
        if (this.checkend) {
          condition += ` AND f_offsite_time <= '${this.checkend} 23:59:59' `
        }
        this.f_noruhu = []
        this.f_state = ''
        this.model.search(condition, {})

      }
    },

    ready() {
      //
      //tag
      if (this.check) {
        let condition
        //tag
        condition = `f_checker_name='${this.check.f_checker_name}' and f_state!='作废'`

        if (this.checkstatus) {
          condition += ` and cp.f_entry_status = '${this.f_entry_status}'`
        }
        if (this.checkstart) {
          condition += ` AND f_offsite_time >= '${this.checkstart} 00:00:00' `
        }
        if (this.checkend) {
          condition += ` AND f_offsite_time <= '${this.checkend} 23:59:59' `
        }

        this.model.search(condition, {})
      }
    },
    methods: {
      selfSearch(args) {
        //
        if (this.check) {

          args.condition = `f_checker_name='${this.check.f_checker_name}' and f_state!='作废'`
          if (this.checkstatus) {
            args.condition += ` and f_entry_status = '${this.checkstatus}'`
          }
          if (this.checkstart) {
            args.condition += ` AND f_offsite_time >= '${this.checkstart} 00:00:00' `
          }
          if (this.checkend) {
            args.condition += ` AND f_offsite_time <= '${this.checkend} 23:59:59' `
          }
          if (this.f_state) {
            if (this.f_state == '入户') {
              args.condition = args.condition + " and f_state='已检' and f_entry_status='入户'"
            } 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_entry_status='到访不遇' and f_check_version > 1 "
                } else {
                  args.condition += this.f_noruhu[0] == '未检' ? " and f_state='未检'" : ` and f_state='已检' and f_entry_status='${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_entry_status='到访不遇'))"
                  } else if (this.f_noruhu.indexOf('复访不遇') != -1) {
                    args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and f_entry_status = '到访不遇' AND f_check_version >2))"
                  } else {
                    args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and f_entry_status='拒检'))"
                  }
                } else {
                  if (this.f_noruhu.indexOf('复访不遇') != -1) {
                    if (this.f_noruhu.indexOf('到访不遇') != -1) {
                      args.condition = args.condition + "  and (f_state='已检' and (f_entry_status='到访不遇'))"
                    } else {
                      args.condition = args.condition + " and f_state='已检' and ((f_entry_status='到访不遇' and  f_check_version >2) or f_entry_status='拒检' )"
                    }
                  } else {
                    args.condition = args.condition + " and (f_state='已检' and (f_entry_status='到访不遇' or f_entry_status='拒检'))"
                  }
                }
              } else {
                if (this.f_noruhu.indexOf('未检') != -1) {
                  args.condition = args.condition + " and (f_state='未检' or (f_state='已检' and (f_entry_status='到访不遇' or f_entry_status='拒检')))"
                } else {
                  args.condition = args.condition + " and (f_state='已检' and (f_entry_status='到访不遇' or f_entry_status='拒检'))"
                }
              }
            }
          }
          this.model.search(args.condition, args.model)
        }
      }
    },
    computed: {
      // 导出到Excel的条件构造
      exportParameter() {
        return {
          condition: this.$refs.paged.model.condition
        }
      }
    },
  }
</script>
