<template>
  <div class="flex">
    <section>
      <div class="basic-main row" style="height: 100%">
        <criteria-paged :model="model" v-ref:paged>
          <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
            <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
              <div class="row">
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">计划名称</label>
                  <input type="text" class="input_search" v-model="model.f_plan_name" style="width:60%;"
                         placeholder="计划名称"  condition="cp.f_plan_name like '%{}%'" />
                </div>
                <div class="col-sm-5">
                  <role-selector
                    :left-width="'55%'"
                    :right-width="'55%'"
                    role-name="安检员"
                    role-lable="安&nbsp;检&nbsp;员"
                    @re-res="$parent.$parent.getRes"
                    :value.sync="model.f_checker_id"
                    v-model="model.f_checker_id"
                    condition="cp.f_checker = '{}'">
                  </role-selector>
                </div>
                <div class="col-sm-3" style="width: 20%">
                  <label class="font_normal_body">安检状态</label>
                  <v-select
                    placeholder='安检类型'
                    class="select select_list"
                    style="width: 60%"
                    :value.sync="model.f_state"
                    :value-single="true"
                    :options='$parent.$parent.f_states'
                    v-model="model.f_state"
                    condition="cpi.f_state = '{}'"
                    close-on-select clear-button> </v-select>
                </div>
                <div class="col-sm-4" style="width: 18%;float: right;">
                  <div class="span" style="text-align: center;">
                    <button class="button_search" @click="search()">查询</button>
                    <export-excel   :data="$parent.$parent.searchData"
                                    :field="$parent.$parent.excelHeaders"
                                    sqlurl="api/af-safecheck/logic/exportfile" sql-name="safecheckResponseTimeList"
                                    :template-name='$parent.$parent.templateName'></export-excel>
                    <div class="button_spacing"  style="margin-right: 10px;float: right;"
                         :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-show="$parent.$parent.criteriaShow">
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">用户编号</label>
                  <input type="text" class="input_search" v-model="model.f_userinfo_code" style="width:60%;"
                         placeholder="用户编号"    condition="cpi.f_userinfo_code = '{}'" />
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">用户名称</label>
                  <input type="text" class="input_search" v-model="model.f_user_name" style="width:60%;"
                         placeholder="用户名称"    condition="cpi.f_user_name = '{}'" />
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">用户类型</label>
                  <v-select
                            placeholder='用户类型'
                            class="select select_list"
                            style="width: 60%"
                            :value.sync="model.f_user_type"
                            :value-single="true"
                            :options='$parent.$parent.f_user_types'
                            v-model="model.f_user_type"
                            condition="cpi.f_user_type = '{}'"
                            close-on-select clear-button> </v-select>
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">安检起始</label>
                  <datepicker
                    :value.sync="model.f_issue_time_start"
                    placeholder='安检下发开始时间'
                    :disabled-days-of-week="[]"
                    style="width: 60%"
                    :format="'yyyy-MM-dd 00:00:00'"
                    condition="cp.f_issue_time >= '{}'"
                    :show-rest-button="reset"
                    v-model = "model.f_issue_time_start">
                  </datepicker>
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">安检结束</label>
                  <datepicker
                    :value.sync="model.f_issue_time_end"
                    placeholder='安检下发结束时间'
                    style="width: 60%"
                    :disabled-days-of-week="[]"
                    condition="cp.f_issue_time  <= '{}'"
                    :format="'yyyy-MM-dd 23:59:59'"
                    :show-rest-button="reset"
                    v-model = "model.f_issue_time_end">
                  </datepicker>
                </div>
              </div>
              <div class="row" v-show="$parent.$parent.criteriaShow">
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">响应起始</label>
                  <input type="text" class="input_search" v-model="$parent.$parent.response_hour_start" style="width:60%;"
                         placeholder="响应起始（小时）"   />
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">响应结束</label>
                  <input type="text" class="input_search" v-model="$parent.$parent.response_hour_end" style="width:60%;"
                         placeholder="响应结束（小时）"   />
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">详细地址</label>
                  <input type="text" class="input_search" v-model="model.f_address" style="width:60%;"
                         placeholder="详细地址"    condition="cpi.f_address like '%{}%'" />
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">安检类型</label>
                  <v-select
                    placeholder='安检类型'
                    class="select select_list"
                    style="width: 60%"
                    :value.sync="model.f_safecheck_type"
                    :value-single="true"
                    :options='$parent.$parent.f_safecheck_types'
                    v-model="model.f_safecheck_type"
                    condition="cpp.f_safecheck_type = '{}'"
                    close-on-select clear-button> </v-select>
                </div>
                <div class="col-sm-4" style="width: 20%">
                  <label class="font_normal_body">入户状态</label>
                  <v-select
                    placeholder='入户状态'
                    class="select select_list"
                    style="width: 60%"
                    :value.sync="model.f_entry_status"
                    :value-single="true"
                    :options='$parent.$parent.f_entry_statuss'
                    v-model="model.f_entry_status"
                    condition="cpp.f_entry_status = '{}'"
                    close-on-select clear-button> </v-select>
                </div>

            </div>
            </div>
          </criteria>
          <data-grid :model="model" partial='list' class="list_area table_sy" 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>
                <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" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_userinfo_code }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_user_name }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_user_phone }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_safecheck_type }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_entry_status }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_state }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_plan_name }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_plan_type }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_create_operator }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_create_time }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_send_operator }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_issue_time }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_checker }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_address }}</nobr>
                </td>

                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_upload_date }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_onsite_time }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}">
                  <nobr>{{ row.f_offsite_time }}</nobr>
                </td>
                <td style="text-align:center" :style="{'color': (row.overdue_hh > $parent.$parent.$parent.safecheckOverdue?'red':'')}"  title="如果单子是未检但是已经下发了,此处计算的就是从下发时间到当前时间的时间间隔！">
                  <nobr >{{ row.response_minute }}</nobr>
                </td>
              </tr>
            </template>
            <template partial="foot">
              <td colspan="2" style="text-align:center">总计划数</td>
              <td colspan="2" style="text-align:center"> <nobr>{{ $parent.$parent.$parent.countRes.count_end }}</nobr> </td>

              <td colspan="2" style="text-align:center">响应总时长</td>
              <td colspan="2" style="text-align:center"> <nobr>{{ $parent.$parent.$parent.countRes.times }}/{{ $parent.$parent.$parent.countRes.danwei }}</nobr> </td>

              <td colspan="2" style="text-align:center">平均响应时长</td>
              <td colspan="2" style="text-align:center"> <nobr>{{ $parent.$parent.$parent.countRes.avg_count }}/{{ $parent.$parent.$parent.countRes.danwei }}</nobr> </td>

              <td colspan="2" style="text-align:center">超时总计划数</td>
              <td colspan="2" style="text-align:center"> <nobr>{{ $parent.$parent.$parent.countRes.overdue_count }}</nobr> </td>
            </template>
          </data-grid>
        </criteria-paged>
      </div>
    </section>
  </div>
</template>

<script>
import {PagedList, HttpResetClass} from 'vue-client'
import * as Util from '../Util'
import {toEndDateString, toStartDateString} from "../Util";

export default {
  title: "安检响应时间查询",
  data() {
    let model = new PagedList('api/af-safecheck/sql/safecheckResponseTimeList', 20, {
      f_filiale_id: 'this.f_filiale_id'
    })
    model.f_filiale_id = `('${this.$login.f.orgid}')`
    return {
      f_filiale_id: `('${this.$login.f.orgid}')`, //分公司id
      model: model,
      userid: this.$login.f.id,
      source: `tool.getFullTree(this.getRights().where(row.getType() != $zone$).where(row.getType() == $organization$))`,
      f_safecheck_types: [{label:'全部',value:''},{label:'年度普检',value:'年度普检'},{label:'专项计划',value:'专项计划'}],
      f_user_types: [{label:'全部',value:''},{label:'民用',value:'民用'},{label:'非民用',value:'非民用'}],
      f_entry_statuss: [{label:'全部',value:''},{label:'入户',value:'入户'},{label:'拒检',value:'拒检'},{label:'到访不遇',value:'到访不遇'}],
      f_states: [{label:'全部',value:''},{label:'已检',value:'已检'},{label:'未检',value:'未检'}],
      response_hour_start: '', //响应时间开始
      response_hour_end: '', //响应时间结束
      safecheckOverdue: '', //安检响应时长（单位/小时）
      criteriaShow: false,
      countRes: {
        avg_count: 0,
        count_end: 0,
        danwei: '单位',
        times: 0,
        overdue_count: 0
      },
      searchData: {
        condition: "1=1",
        f_filiale_id:  `('${this.$login.f.orgid}')`
      },
      excelHeaders: {
        'f_userinfo_code': '用户编号',
        'f_user_name': '用户名称',
        'f_user_phone': '用户电话',
        'f_safecheck_type': '安检类型',
        'f_entry_status': '入户状态',
        'f_plan_name': '计划名称',
        'f_plan_type': '计划类型',
        'f_create_operator': '计划创建人',
        'f_create_time': '计划创建时间',
        'f_send_operator': '计划下发人',
        'f_issue_time': '下发时间',
        'f_checker': '安检员',
        'f_address': '详细地址',
        'f_upload_date': '上传时间',
        'f_onsite_time': '入户时间',
        'f_offsite_time': '离开时间',
        'response_minute': '响应时间',
      },
      templateName: "安检派单业务处理响应时间导出"
    }
  },
  methods: {
    getRes(obj) {
      this.f_filiale_id = this.$login.convertToIn(obj.resids);
    },
    hidden() {
      this.criteriaShow = !this.criteriaShow;
    },
    selfSearch(args) {
      this.model.f_filiale_id = this.f_filiale_id; //只有选择了
      if (this.response_hour_start) {
        args.condition += ` and CAST(DATEDIFF( hh, cp.f_issue_time, isnull(cpp.f_upload_date,getdate()) ) AS  bigint) >= ${this.response_hour_start}`
      }
      if (this.response_hour_end) {
        args.condition += ` and CAST(DATEDIFF( hh, cp.f_issue_time, isnull(cpp.f_upload_date,getdate()) ) AS  bigint) <= ${this.response_hour_end}`
      }
      this.searchCountResponseStatistics(args.condition)
      this.searchData.condition = args.condition;
      this.searchData.f_filiale_id = this.f_filiale_id;
      return this.model.search(args.condition, args.model)
    },
    searchCountResponseStatistics (condition) {
      if (!this.safecheckOverdue) {
        return  this.$showMessage("未在参数管理配置单值参数【安检响应时长】单位小时,不进行汇总查询。")
      }
      let http = new HttpResetClass()
      let dataVal = {f_filiale_id:this.f_filiale_id,condition:condition,overdue_hh:this.safecheckOverdue}
      http.load("POST","/api/af-safecheck/sql/safecheckResponseStatistics", {data:dataVal},{resolveMsg: null, rejectMsg: null}).then((res)=>{
        this.countRes = res.data[0]
      })
    }

  },
  ready () {
    this.$refs.paged.$refs.cri.model.f_offsite_time_start = Util.toStartDateString() + ' 00:00:00'
    this.$refs.paged.$refs.cri.model.f_offsite_time_end = Util.toEndDateString() + ' 23:59:59'
    this.$refs.paged.$refs.cri.model.f_entry_status = '入户'
    console.log("安检响应时长单位时："+this.$appdata.getSingleValue("安检响应时长"))
    if (!this.$appdata.getSingleValue("安检响应时长")) {
      this.$showMessage("未在参数管理配置单值参数【安检响应时长】单位小时")
    }else {
      this.safecheckOverdue = this.$appdata.getSingleValue("安检响应时长")
    }
  },
  computed: {},
  watch: {}
}
</script>

<style scoped>

</style>
