<template>
  <div class="flex-row">
    <div class="basic-main">
      <criteria-paged :model="model" v-ref:paged @sort="sort">
        <criteria partial='criteria' class="search_area" @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-2"  >
                <label class="font_normal_body">客户编号</label>
                <input type="text" class="input_search" v-model="model.f_userinfo_code"
                       style="width: 60%" placeholder="客户编号" condition="f_userinfo_code='{}'">
              </div>
              <div class="form-group col-sm-2"  >
                <label class="font_normal_body">客户名称</label>
                <input type="text" class="input_search" v-model="model.f_user_name"
                       style="width: 60%" placeholder="客户名称" condition="f_user_name like '%{}%'">
              </div>
              <div class="form-group col-sm-2"  >
                <label class="font_normal_body">客户电话</label>
                <input type="text" class="input_search" v-model="model.f_user_phone"
                       style="width: 60%" placeholder="客户电话" condition="f_user_phone = '{}'">
              </div>
              <div class="form-group col-sm-2"  >
                <label class="font_normal_body">小&emsp;&emsp;区</label>
                <input type="text" class="input_search" v-model="model.f_residential_area"
                       style="width: 60%" placeholder="小区" condition="f_residential_area like '%{}%'">
              </div>
              <div class="form-group col-sm-2"  >
                <label class="font_normal_body">详细地址</label>
                <input type="text" class="input_search" v-model="model.f_address"
                       style="width: 60%" placeholder="详细地址" condition="f_address like '%{}%'">
              </div>
              <div class="form-group col-sm-2" style="width: auto;margin-top:8px;float: right">

                <export-excel-safe :data="$parent.$parent.searchData"
                                   :field="$parent.$parent.excelHeaders"
                                   :choose-col="true"
                                   sqlurl="rs/logic/SafeExportExcel" sql-name="ReportCheckItemList" template-name='安检结果明细导出'></export-excel-safe>

                <button class="button_spacing button_search" @click="search()">查询</button>
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()"></div>
              </div>
              <div class="form-group col-sm-2"  v-show="$parent.$parent.criteriaShow" >
                <label class="font_normal_body">开始时间</label>
                <datepicker
                  :value.sync="model.f_check_start"
                  placeholder='开始时间' style="width: 60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_check_start"
                  condition="f_offsite_time > ='{} 00:00:00'">
                </datepicker>
              </div>
              <div class="form-group col-sm-2"  v-show="$parent.$parent.criteriaShow" >
                <label class="font_normal_body">结束时间</label>
                <datepicker
                  :value.sync="model.f_check_end"
                  placeholder='结束时间' style="width: 60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_check_end"
                  condition="f_offsite_time <=  '{} 23:59:59'">
                </datepicker>
              </div>
              <div class="form-group col-sm-2"   v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">安检状态</label>
                <v-select id="f_entry"
                          class="select_list select"
                          placeholder='安检状态' style="width: 60%"
                          v-model='model.f_entry'
                          :value.sync="$parent.$parent.model.f_entry"
                          :options='$parent.$parent.f_entry_status'
                          condition="f_entry_status = '{}'"
                          :value-single="true"
                          close-on-select clear-button></v-select>
              </div>
              <div class="form-group col-sm-2"  v-show="$parent.$parent.criteriaShow" >
                <label class="font_normal_body">开户时间</label>
                <datepicker
                  :value.sync="model.f_check_gasstart"
                  placeholder='开始时间' style="width: 60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_check_gasstart"
                  condition="f_gas_date > ='{} 00:00:00'">
                </datepicker>
              </div>
              <div class="form-group col-sm-2"  v-show="$parent.$parent.criteriaShow" >
                <label class="font_normal_body">开户时间</label>
                <datepicker
                  :value.sync="model.f_check_gasend"
                  placeholder='结束时间' style="width: 60%"
                  :disabled-days-of-week="[]"
                  :format="'yyyy-MM-dd'"
                  :show-rest-button="reset"
                  v-model="model.f_check_gasend"
                  condition="f_gas_date <=  '{} 23:59:59'">
                </datepicker>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow" >
                <label class="font_normal_body">气表品牌</label>
                <v-select
                  class="select_list select"
                  placeholder='气表品牌' style="width: 60%"
                  v-model='model.tableBrand'
                  :value.sync="model.tableBrand"
                  :options='$parent.$parent.tableBrands'
                  condition="f_rqb_qbpp = '{}'"
                  :value-single="true"
                  close-on-select clear-button></v-select>
              </div>
              <div class="col-sm-4 form-group" v-show="$parent.$parent.criteriaShow" style="margin: 0">
                <role-selector-safe
                  role-name="安检员"
                  role-lable="安&nbsp;检&nbsp;员&nbsp;"
                  @re-res="$parent.$parent.getRes"
                  :value.sync="model.f_checker_id"
                  v-model="model.f_checker_id"
                  condition="f_checker_name = '{}'">
                </role-selector-safe>
              </div>
              <div class="form-group col-sm-2"   v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">未用燃气</label>
                <v-select class="select_list select"
                          placeholder='请选择' style="width: 60%"
                          :value.sync="model.f_usegas" v-model="model.f_usegas"
                          :options='$parent.$parent.usegas'
                          filer-key="name"
                          close-on-select clear-button
                          condition="f_usegas='{}'"
                          :value-single="true">
                </v-select>
              </div>
              <div class="form-group col-sm-2"   v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">导&ensp;出&ensp;项</label>
                <v-select class="select_list select"
                          placeholder='请选择' style="width: 60%"
                          :value.sync="$parent.$parent.f_excel_items" v-model="$parent.$parent.f_excel_items"
                          :options='$parent.$parent.excelitems'
                          :multiple="true"
                          filer-key="name"
                          clear-button>
                </v-select>
              </div>
              <modal :show.sync="$parent.$parent.showModal" v-ref:modal backdrop="false">
                <div slot="modal-header" class="modal-header">
                  <h4 class="modal-title">
                    选择计划
                  </h4>
                </div>
                <div slot="modal-body" class="modal-body">
                  <plan-chooser :f.sync="$parent.$parent.fun" v-ref:chooser></plan-chooser>
                </div>
                <div slot="modal-footer" class="modal-footer" style="text-align: center">
                  <button class="button_search" @click="$parent.$parent.ok">确认</button>
                  <button class="button_search" @click="$parent.$parent.cancel">取消</button>
                </div>
              </modal>
            </div>
          </div>
        </criteria>
        <data-grid class="list_area table_sy" style="overflow: hidden" :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>
              <th>
                <nobr>客户类型</nobr>
              </th>
              <th>
                <nobr>客户地址</nobr>
              </th>
              <th>
                <nobr>安检状态</nobr>
              </th>
              <th>
                <nobr>用气状态</nobr>
              </th>
              <th>
                <data-order field="f_offsite_time" name="安检时间"></data-order>
              </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_userinfo_code}}</td>
            <td style="text-align: center">{{row.f_user_name}}</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_phone}}</td>
            <td style="text-align: center">{{row.f_check_type}}</td>
            <td style="text-align: center">{{ row.f_address }}</td>
            <td style="text-align: center">{{ row.f_entry_status }}</td>
            <td style="text-align: center">{{ row.f_usegas }}</td>
            <td style="text-align: center">{{ row.f_offsite_time }}</td>
            <td style="text-align: center">{{ row.f_checker_name }}</td>
            <td style="text-align: center">{{ row.f_repair_approved_note }}</td>
            <td style="text-align: center">{{ row.f_gas_date }}</td>
          </template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>
</template>

<script>
import {PagedList} from 'vue-client'
import Vue from 'vue'
import co from 'co'
import AppData from '../../../stores/AppData'
import * as Util from '../../Util'
import getNowDate from "../../../utils/getNowDate";
import HttpResetClass from "vue-client/src/plugins/HttpResetClass";
import BenzAMRRecorder from 'benz-amr-recorder'

const Excelconfig = {
  '燃气表': {
    'f_rqb_jbbh':'基表编号',
    'f_rqb_qbpp': '气表品牌',
    'f_rqb_qpxh': '气表型号',
    'f_rqb_jqfx': '进气方向',
    'f_rqb_lgql': '累购气量',
    'f_rqb_lgje': '累购金额',
    'f_rqb_yyql': '已用气量',
    'f_rqb_yyje': '已用金额',
    'f_rqb_syql': '剩余气量',
    'f_rqb_syje': '剩余金额',
    'f_rqb_qlyccz': '气量异常差值',
    'f_rqb_jeyccz': '金额异常差值',
    'f_rqb_bxh': '表箱号',
    'f_rqb_scrq': '生产日期',
    'f_rqb_kssyrq': '开始使用日期',
    'f_rqb_lq': '表漏气',
    'f_rqb_ztb': '直通表',
    'f_rqb_sb': '死表',
    'f_rqb_fs': '表腐蚀',
    'f_rqb_qbgb': '气不过表',
    'f_rqb_kbbh': '卡表表号',
    'f_rqb_bsh': '表损坏',
    'f_rqb_jbds':'机械读数'
  },
  '立管': {
    'f_lg_fs': '立管腐蚀',
    'f_lg_bg': '立管包裹',
    'f_lg_yxylsfzc': '立管运行压力是否正常',
    'f_lg_jglq':'立管接口漏气'
  },
  '阀门': {
    'f_fm_bqfsflq': '表前阀是否漏气',
    'f_fm_bqfsfbg': '表前阀是否包裹',
    'f_fm_zbfsflq': '自闭阀是否漏气',
    'f_fm_zbfsfbg':'自闭阀是否包裹',
    'f_fm_zbfsfsh': '自闭阀是否损坏',
    'f_fm_zqfsflq': '灶前阀是否漏气',
    'f_fm_fmxs':'阀门形式'
  },
  '管道': {
    'f_gd_lq': '管道漏气',
    'f_gd_ckt': '管道穿客厅',
    'f_gd_cws': '管道穿卧室',
    'f_gd_ydqgj':'管道与电气过近',
    'f_gd_af': '管道暗封',
    'f_gd_sg': '管道私改',
    'f_gd_cfty':'管道厨房他用',
    'f_gd_gdk': '管道固定卡',
    'f_gd_rg':'管道软管'
  },
  '报警器': {
    'f_bjq_sfaz':'报警器是否安装'
  },
  '灶具': {
    'f_zj_sbpp': '灶具设备品牌',
    'f_zj_sbxh': '灶具设备型号',
    'f_zj_scrq': '灶具生产日期',
    'f_zj_kssyrq':'灶具开始使用日期',
    'f_zj_rgcd': '灶具软管长度(米)',
    'f_zj_qysfpp': '灶具气源是否匹配',
    'f_zj_sfsh':'灶具是否损坏',
    'f_zj_sfbf': '灶具是否报废',
    'f_zj_yqmjlbz':'灶具与墙面距离不足',
    'f_zj_xhbhsfzc': '灶具熄火保护是否正常',
    'f_zj_rgcz': '灶具软管材质',
    'f_zj_zjxs':'灶具灶具形成',
    'f_zj_yhdabh': '灶具用户档案编号',
    'f_zj_ygnx':'灶具已过年限'
  },
  '热水器': {
    'f_rsq_sbpp': '热水器设备品牌',
    'f_rsq_sbxh': '热水器设备型号',
    'f_rsq_kssyrq': '热水器开始使用日期',
    'f_rsq_rgcd':'热水器软管长度(米)',
    'f_rsq_lq': '热水器漏气',
    'f_rsq_yd': '热水器烟道',
    'f_rsq_rgcz':'热水器软管材质',
    'f_rsq_azwz': '热水器安装位置'
  },
  '壁挂炉': {
    'f_bgl_sbpp': '壁挂炉设备品牌',
    'f_bgl_sbxh': '壁挂炉设备型号',
    'f_bgl_kssyrq': '壁挂炉开始使用日期',
    'f_bgl_rgcd':'壁挂炉软管长度(米)',
    'f_bgl_yd': '壁挂炉烟道',
    'f_bgl_czxx': '壁挂炉材质选项',
    'f_bgl_azwz':'壁挂炉安装位置'
  },
  '隐患': {
    'f_yh_tfbl': '通风不良',
    'f_yh_rqssazzws': '燃气设施安装在卧室/卫生间',
    'f_yh_rgcg': '软管超过两米',
    'f_yh_rgrh':'软管老化',
    'f_yh_dewy': '第二火源',
    'f_yh_dfyrybp': '堆放易燃易爆品',
    'f_yh_rgcq':'软管穿墙/棚顶',
    'f_yh_rgyjt': '软管有接头',
    'f_yh_rglhygj': '软管离火源过近',
    'f_yh_kfscf': '开放式厨房',
    'f_yh_rqgddzzw':'燃气管道/燃气表搭挂重物',
    'f_yh_rqssgdblg': '燃气设施固定不牢固',
    'f_yh_zdyh': '重点用户',
    'f_yh_dxcr':'电线缠绕',
    'f_yh_ddbgf': '断电不关阀',
    'f_yh_ddbg': '吊顶包管',
    'f_yh_sjrrq': '私接热水器或壁挂炉',
    'f_yh_sflq':'是否漏气',
    'f_yh_lqwz': '漏气位置',
    'f_yh_qlyc': '气量异常',
    'f_yh_sjst':'私接三通',
    'f_yh_sjsyz':'私接商用灶'
  }
}

export default {
  title: '安检项明细导出',
  data() {
    let model = new PagedList('rs/sql/ReportCheckItemList', 20)
    return {
      tableBrands: [{label: '全部', value: ''}],
      audioshow: false,
      safeAudit: this.$appdata.getSingleValue('安检审核') == '是' ? true : false,
      searchData: {
        condition: "1=1",
        orderitem: "f_offsite_time desc",
        f_plan_id: "",
        f_approved: '',
        f_entry_status: '',
        f_no_checkplan: '',
        f_filialeid: `('${this.$login.f.orgid}')`
      },
      model: model,
      excelHeadersStr:{
        'f_userinfo_code': '客户编号',
        'f_user_name': '客户姓名',
        'f_meternumber':'客户表号',
        'f_user_phone':'客户电话',
        'f_check_type': '客户类型',
        'f_residential_area': '小区',
        'f_address': '客户地址',
        'f_entry_status': '安检状态',
        'f_usegas': '用气状态',
        'f_offsite_time': '安检时间',
        'f_checker_name': '安检员',
        'f_repair_approved_note': '备注',
        'f_gas_date': '开户时间',
        'f_plan_name': '计划名称'
      },
      excelHeaders: {
        'f_userinfo_code': '客户编号',
        'f_user_name': '客户姓名',
        'f_meternumber':'客户表号',
        'f_user_phone':'客户电话',
        'f_check_type': '客户类型',
        'f_residential_area': '小区',
        'f_address': '客户地址',
        'f_entry_status': '安检状态',
        'f_usegas': '用气状态',
        'f_offsite_time': '安检时间',
        'f_checker_name': '安检员',
        'f_repair_approved_note': '备注',
        'f_gas_date': '开户时间',
        'f_plan_name': '计划名称'
      },
      fun: this.$login.f,
      pcds:[],
      checkersid: [],
      f_excel_items:[],
      excelitems:this.$appdata.getParam('安检结果导出项'),
      safetype: this.$appdata.getParam('安检类型')?[{label: '全部', value: ''}, ...this.$appdata.getParam('安检类型')]:[{label: '全部', value: ''}],
      criteriaShow: false,
      checktype: this.$appdata.getParam('用户类型')?[{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]:[{label: '全部', value: ''}],
      f_check_type:[{label:'全部',value:''},{label:'民用',value:'民用'},{label:'非民用',value:'非民用'}],
      showModal: false,
      safe_types:this.$appdata.getParam('计划类型')?[{label: '全部', value: ''}, ...this.$appdata.getParam('计划类型')]:[{label: '全部', value: ''}],
      f_approval_states: this.$appdata.getParam('安检单审核状态')?[{label: '全部', value: ''}, ...this.$appdata.getParam('安检单审核状态')]:[{label: '全部', value: ''}],
      f_entry_status: this.$appdata.getParam('安检状态')?[{label: '全部', value: ''}, ...this.$appdata.getParam('安检状态')]:[{label: '全部', value: ''}],
      no_checkplans: this.$appdata.getParam('有无计划')?[{label: '全部', value: ''}, ...this.$appdata.getParam('有无计划')]:[{label: '全部', value: ''}],
      usegas: this.$appdata.getParam('用气状态')?[{label: '全部', value: ''}, ...this.$appdata.getParam('用气状态')]:[{label: '全部', value: ''}],
      showcheckmsg: false,
      checkInfo: {},
      orderDefault: "'f_offsite_time'",
      orderFields: {
        f_offsite_time: 'no'
      },
      sliceArea: [],
      cbc:[]
    }
  },
  methods: {
    //气表品牌查询
    getTableBrands() {
      new HttpResetClass().load("POST", `/rs/sql/safe_singleTable_OrderBy`,
        {data: {items: "*", tablename: "t_gasbrand", orderitem: "id", condition: ` 1=1 `}},
        {resolveMsg: null, rejectMsg: null}).then((resp) => {
        resp.data.forEach((result) => {
          this.tableBrands.push({label: result.f_meter_brand, value: result.f_meter_brand})
        })
      })
    },
    getRes(obj) {
      this.model.f_filialeid = this.$login.convertToIn(obj.resids);
      this.searchData.f_filialeid = this.$login.convertToIn(obj.resids);
    },
    showChooser() {
      this.showModal = true
    },
    hidden() {
      this.criteriaShow = !this.criteriaShow
    },
    ok() {
      this.showModal = false
      if (this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id) {
        this.$refs.paged.$refs.criteria.$refs.modal.f_plan_name = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id
        this.$refs.paged.$refs.criteria.$refs.modal.f_check_plan=this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow
      }
    },
    cancel() {
      this.showModal = false
      this.$refs.paged.$refs.criteria.$refs.modal.f_check_plan = {f_plan_name: '', id: ''}
      this.$refs.paged.$refs.criteria.$refs.modal.f_plan_id=''
    },
    sort(field, rule) {
      // 将所有排序方式设为不排序，实现相互排斥
      for (let key in this.orderFields) {
        if (key === field) {
          this.orderFields[key] = rule
        } else {
          this.orderFields[key] = 'no'
        }
      }
      // 如果新规则不排序，还原为默认排序
      if (rule === 'no') {
        this.model.paramSource.orderitem = `'${this.orderDefault}'`
      } else {
        this.model.paramSource.orderitem = `'${field} ${rule}'`
      }
      this.search()
    },
    search() {
      this.$refs.paged.$refs.criteria.search()
    },
    selfSearch(args) {
      ////tag
      //args.condition+=" and f_check_type='民用'"
      if(args.model.f_check_date){
        const daysBefore = Number(args.model.f_check_date);
        if ( daysBefore > 0) {
          // 获取当前日期
          const now = new Date();
          // 计算三天前的日期
          const threeDaysBefore = new Date(now.getTime() - (daysBefore * 24 * 60 * 60 * 1000));
          // 如果需要格式化日期，可以使用Date对象的方法
          var formattedDate = threeDaysBefore.toISOString().split('T')[0];
          args.condition += `  and f_offsite_time <='${formattedDate} 23:59:59'`;
        } else {
          console.error('f_check_date不是一个有效的正整数');
        }
      }
      this.model.search(args.condition, args.model, args.condValue)
    }
  },

  ready() {
    ////tag
    this.function = this.f
    ////tag
    //获取气表品牌
    this.getTableBrands()
    // let gen = select(this)
    // co(gen)
  },
  watch: {
    'model.f_no_checkplan'(val) {
      if (this.model.f_no_checkplan){
        this.searchData.f_no_checkplan = val
      }
      if (this.model.f_no_checkplan == '有计划安检') {

        this.showChooser()
      }
    },
    'model.condition'(val) {
      if (val) {
        this.searchData.condition = val
      }
    },
    'f_excel_items'(val){
      this.excelHeaders = JSON.parse(JSON.stringify(this.excelHeadersStr))
      if(val.length > 0){
        val.forEach((item)=>{
          this.excelHeaders = Object.assign(this.excelHeaders, JSON.parse(JSON.stringify(this.excelHeaders)),Excelconfig[item])
        })
      }
    }
  }

}
</script>
