<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 like '%{}%'">
              </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-4" style="display: flex; gap: 15px">
                <export-excel-safe :data="$parent.$parent.searchData"
                              :field="$parent.$parent.excelHeaders"
                              :choose-col="true"
                              sqlurl="rs/logic/SafeExportExcel" sql-name="defectControlList" template-name='安检隐患管控明细'></export-excel-safe>
                <button class="button_search" @click="search()">查询</button>
                <div class="button_spacing"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()"></div>
              </div>
            </div>
            <div class="row">
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">是否管控</label>
                <v-select :value.sync="model.f_is_controlled"
                          v-model='model.f_is_controlled'
                          style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.controlStateOptions'
                          placeholder='请选择'
                          close-on-select
                          condition="f_is_controlled = '{}'"></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"
                  :value-multiple="true"
                  :value-close="false"
                  condition="f_checker_name in {}">
                </role-selector-safe>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">检&ensp;查&ensp;项</label>
                <v-select :value.sync="model.f_device_type"
                          v-model="model.f_device_type"
                          style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.device_types'
                          placeholder='检查项'
                          :multiple="true"
                          condition="f_device_type in {}"></v-select>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">隐患名称</label>
                <v-select :value.sync="model.f_item_name"
                          v-model='model.f_item_name'
                          style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.DefectConfigs'
                          placeholder='隐患名称'
                          :multiple="true"
                          condition="f_item_name in {}"></v-select>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">隐患状态</label>
                <v-select :value.sync="model.f_is_repaired"
                          v-model='model.f_is_repaired'
                          style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.repairStateOptions'
                          placeholder='请选择'
                          :multiple="true"
                          condition="f_is_repaired in {}"></v-select>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">安检起始</label>
                <datepicker
                  class="select_list select"
                  :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
                  class="select_list select"
                  :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 :value.sync="model.f_check_type" v-model='model.f_check_type'
                          :value-single="true" style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.f_check_type' placeholder='请选择'
                          close-on-select
                          condition="f_check_type = '{}'"></v-select>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">处理方式</label>
                <v-select :value.sync="model.f_deal_dispose"
                          v-model='model.f_deal_dispose'
                          style="width: 60%"
                          class="select_list select"
                          :options='$parent.$parent.deal_dispose' placeholder='请选择'
                          :multiple="true"
                          condition="f_deal_dispose in {}"></v-select>
              </div>
              <div class="form-group col-sm-2" v-show="$parent.$parent.criteriaShow">
                <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" v-show="$parent.$parent.criteriaShow">
                <label class="font_normal_body">处理人</label>
                <input type="text" class="input_search" v-model="model.f_repairman"
                       style="width: 60%" placeholder="处理人" condition="f_repairman = '{}'">
              </div>
            </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><data-order field="f_address" name="客户地址"></data-order></th>
              <th><data-order field="f_offsite_time" name="安检时间"></data-order></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">
              <span v-if="row.f_is_controlled=='已管控'" style="color: green; font-weight: bold">已管控</span>
              <span v-else style="color: #f0ad4e">未处理</span>
            </td>
            <td style="text-align: center">{{row.f_device_type}}</td>
            <td style="text-align: center">{{row.f_item_name}}/{{row.f_item_value}}</td>
            <td style="text-align: center">{{row.f_defect_level}}</td>
            <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_user_phone}}</td>
            <td style="text-align: center">{{row.f_residential_area}}</td>
            <td style="text-align: center">{{row.f_address}}</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_is_repaired}}</td>
            <td style="text-align: center">{{row.f_repairman}}</td>
            <td style="text-align: center">{{row.f_deal_dispose}}</td>
            <td style="text-align: center">{{row.f_repair_date}}</td>
            <td style="text-align: center">
              <button v-if="row.f_is_controlled == '已管控'"
                      class="button_search"
                      style="padding: 2px 10px; font-size: 12px"
                      @click="$parent.$parent.$parent.openViewModal(row)">
                查看管控
              </button>
              <button v-else
                      class="button_search"
                      style="padding: 2px 10px; font-size: 12px"
                      @click="$parent.$parent.$parent.openControlModal(row)">
                管控处理
              </button>
            </td>
          </template>
        </data-grid>
      </criteria-paged>

      <!-- 管控处理 Modal -->
      <modal :show.sync="showControlModal" :backdrop="false">
        <div slot="modal-header" class="modal-header">
          <h4 class="modal-title">隐患管控处理</h4>
        </div>
        <div slot="modal-body" class="modal-body">
          <!-- 处理前隐患信息 -->
          <div class="panel panel-default">
            <div class="panel-heading">
              <h5 class="panel-title">处理前隐患信息</h5>
            </div>
            <div class="panel-body">
              <div class="row">
                <div class="col-sm-4"><label>检查项：</label>{{controlData.f_device_type}}</div>
                <div class="col-sm-4"><label>隐患名称：</label>{{controlData.f_item_name}}</div>
                <div class="col-sm-4"><label>隐患级别：</label>{{controlData.f_defect_level}}</div>
              </div>
              <div class="row" style="margin-top:5px">
                <div class="col-sm-4"><label>客户编号：</label>{{controlData.f_userinfo_code}}</div>
                <div class="col-sm-4"><label>客户名称：</label>{{controlData.f_user_name}}</div>
                <div class="col-sm-4"><label>客户电话：</label>{{controlData.f_user_phone}}</div>
              </div>
              <div class="row" style="margin-top:5px">
                <div class="col-sm-4"><label>客户地址：</label>{{controlData.f_address}}</div>
                <div class="col-sm-4"><label>安检时间：</label>{{controlData.f_offsite_time}}</div>
                <div class="col-sm-4"><label>安检员：</label>{{controlData.f_checker_name}}</div>
              </div>
            </div>
          </div>

          <!-- 管控后信息录入 -->
          <div class="panel panel-default">
            <div class="panel-heading">
              <h5 class="panel-title">管控后信息录入</h5>
            </div>
            <div class="panel-body">
              <div class="row" style="margin-top: 10px">
                <label class="control-label col-sm-3 text-right" style="margin-top:5px">管控方式</label>
                <div class="col-sm-8">
                  <v-select :value.sync="controlData.f_control_method"
                            :options='controlMethodOptions'
                            placeholder='请选择管控方式（可多选）'
                            :multiple="true"
                            close-on-select>
                  </v-select>
                </div>
              </div>
              <div class="row" style="margin-top: 10px">
                <label class="control-label col-sm-3 text-right" style="margin-top:5px">处理人</label>
                <div class="col-sm-8">
                  <input type="text" class="input_search" v-model="controlData.f_handler"
                         style="width: 60%" placeholder="处理人">
                </div>
              </div>
              <div class="row" style="margin-top: 10px">
                <label class="control-label col-sm-3 text-right" style="margin-top:5px">处理时间</label>
                <div class="col-sm-8">
                  <datepicker
                    placeholder='处理时间'
                    style="width:60%;text-align: center"
                    :disabled-days-of-week="[]"
                    :format="'yyyy-MM-dd HH:mm:ss'"
                    :show-rest-button="reset"
                    :value.sync="controlData.f_control_time"
                    v-model="controlData.f_control_time">
                  </datepicker>
                </div>
              </div>
              <div class="row" style="margin-top: 10px">
                <label class="control-label col-sm-3 text-right" style="margin-top:5px">
                  <span style="color: red">*</span>处理照片
                </label>
                <div class="col-sm-8">
                  <file-upload style="margin-top: -8px"
                               :name="'defectControl_' + controlData.f_defect_id"
                               action="rs/file/uploadFile"
                               tagname="上传照片"
                               :headers="uploadHeaders"
                               v-ref:controlFiles>
                  </file-upload>
                  <!-- 已上传照片（仅一张） -->
                  <div v-if="controlPhotoFiles.path" style="margin-top: 8px">
                    <div style="font-size: 12px; color: green; margin-top: 3px">
                      <span>已上传: {{controlPhotoFiles.name}}</span>
                      <span style="color: #999; cursor: pointer; margin-left: 8px"
                            @click="controlPhotoFiles = {}">删除</span>
                    </div>
                  </div>
                </div>
              </div>
              <div class="row" style="margin-top: 10px">
                <label class="control-label col-sm-3 text-right" style="margin-top:5px">管控后隐患级别</label>
                <div class="col-sm-8">
                  <v-select :value.sync="controlData.f_control_level"
                            :value-single="true"
                            :options='defectLevelOptions'
                            placeholder='请选择管控后隐患级别'
                            close-on-select>
                  </v-select>
                </div>
              </div>
              <div class="row" style="margin-top: 10px">
                <label class="control-label col-sm-3 text-right" style="margin-top:5px">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注</label>
                <div class="col-sm-8">
                  <textarea style="width: 60%; height: 80px" v-model="controlData.f_remark"
                            placeholder="请输入备注信息"></textarea>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div slot="modal-footer" class="modal-footer" style="text-align: center">
          <button class="button_search" @click="submitControl">确认管控</button>
          <button class="button_search" @click="cancelControl">取消</button>
        </div>
      </modal>

      <!-- 查看管控详情 Modal -->
      <modal :show.sync="showViewModal" :backdrop="false">
        <div slot="modal-header" class="modal-header">
          <h4 class="modal-title">管控详情</h4>
        </div>
        <div slot="modal-body" class="modal-body">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h5 class="panel-title">管控信息</h5>
            </div>
            <div class="panel-body">
              <div class="row">
                <div class="col-sm-4"><label>管控方式：</label>{{viewData.f_control_method}}</div>
                <div class="col-sm-4"><label>处理人：</label>{{viewData.f_control_handler}}</div>
                <div class="col-sm-4"><label>处理时间：</label>{{viewData.f_control_time}}</div>
              </div>
              <div class="row" style="margin-top:5px">
                <div class="col-sm-4"><label>管控后级别：</label>{{viewData.f_control_level}}</div>
                <div class="col-sm-8"><label>备注：</label>{{viewData.f_control_remark}}</div>
              </div>
              <div class="row" style="margin-top:10px" v-if="viewData.f_control_photos">
                <div class="col-sm-12">
                  <label>管控照片：</label>
                  <img :src="'rs/image/file/' + viewData.f_control_photos"
                       style="max-width: 400px; max-height: 300px; display: block; margin-top: 5px;"
                       :key="viewData.f_control_photos" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div slot="modal-footer" class="modal-footer" style="text-align: center">
          <button class="button_search" @click="showViewModal = false">关闭</button>
        </div>
      </modal>

    </div>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  import Vue from 'vue'
  import co from 'co'
  import {toStandardTimeString} from '../../../components/Util'
  import HttpResetClass from "vue-client/src/plugins/HttpResetClass";

  let asyncReady = async function (self) {
    try {
      let res = await Vue.resetget(`/phone/rs/vue`, {data: {}}, {resolveMsg: null, rejectMsg: null})
      Vue.config = res.data
      self.getDefectInfoConfig(res.data)
    } catch (error) {
      if (error.status !== 704) {
        throw error
      }
    }
  }

  let select = function* (self) {
    let http = new HttpResetClass()
    let getGasman = yield http.load('POST', '/rs/search', {
      source: 'this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))',
      userid: self.$login.f.id
    }, {resolveMsg: null, rejectMsg: null})
    self.checkersid.push({label: '全部', value: ''})
    getGasman.data.forEach((checker) => {
      self.checkersid.push({label: checker.name, value: checker.id})
    })
  }

  export default {
    title: '隐患管控',
    data() {
      let model = new PagedList('rs/sql/defectControlList', 20, {
        f_filialeid: 'this.f_filialeid'
      })
      model.f_plan_id = ''
      model.f_approved = ''
      model.f_entry = ''
      model.f_no_checkplan = ''
      model.f_check_type = ''
      model.f_filialeid = `('${this.$login.f.orgid}')`
      return {
        model: model,
        deal_dispose: [
          {label: '全部', value: ''},
          {label: '报修', value: '报修'},
          {label: '现场整改', value: '现场整改'},
          {label: '自行处理', value: '自行处理'},
          {label: '未处理', value: '未处理'},
          {label: '已处理', value: '已处理'}
        ],
        repairStateOptions: this.getSafeParam('隐患状态'),
        uploadHeaders: {
          'username': this.$login.f.name,
          'blodid': '',
          'businessid': '',
          'fremarks': '',
          'defname': '',
          'fusetype': ''
        },
        controlStateOptions: [
          {label: '全部', value: ''},
          {label: '已管控', value: '已管控'},
          {label: '未处理', value: '未处理'}
        ],
        controlMethodOptions: this.getSafeParam('管控方式'),
        defectLevelOptions: [
          {label: '一级', value: '一级'},
          {label: '二级', value: '二级'},
          {label: '三级', value: '三级'}
        ],
        excelHeaders: {
          'f_is_controlled': '是否管控',
          'f_device_type': '检查项',
          'f_item_name': '隐患名称',
          'f_item_value': '隐患值',
          'f_userinfo_code': '客户编号',
          'f_user_name': '客户姓名',
          'f_user_phone': '客户电话',
          'f_residential_area': '小区',
          'f_address': '客户地址',
          'f_offsite_time': '安检时间',
          'f_checker_name': '安检员',
          'f_deal_dispose': '处理方式',
          'f_is_repaired': '隐患状态',
          'f_repairman': '处理人',
          'f_repair_date': '处理时间'
        },
        fun: this.$login.f,
        checkersid: [],
        safetype: this.getSafeParam('安检类型'),
        criteriaShow: false,
        f_approval_states: this.getSafeParam('安检单审核状态'),
        f_entry_status: this.getSafeParam('安检状态'),
        f_checkplans: this.getSafeParam('有无计划'),
        orderDefault: "'f_offsite_time'",
        orderFields: {
          f_offsite_time: 'no'
        },
        sliceArea: [],
        cbc: [],
        DefectConfig: '',
        DefectConfigs: [
          {label: '全部', value: ''},
          {label: '金额异常差值', value: '金额异常差值'},
          {label: '气量异常差值', value: '气量异常差值'}
        ],
        device_types: [{label: '全部', value: ''}],
        f_check_type: [
          {label: '全部', value: ''},
          {label: '民用', value: '民用'},
          {label: '非民用', value: '非民用'}
        ],
        showControlModal: false,
        controlData: {},
        // 单张管控照片: { path: 'f_downloadpath', name: '文件名' }
        controlPhotoFiles: {},
        showViewPhotoDialog: false,
        showViewModal: false,
        viewData: {},
      }
    },
    computed: {
      searchData() {
        return {condition: this.model.condition}
      }
    },
    methods: {
      esc(str) {
        return str == null ? '' : String(str).replace(/'/g, "''")
      },
      // ===== 管控处理 =====
      openViewModal(row) {
        this.viewData = row
        this.showViewModal = true
      },
      openControlModal(row) {
        this.controlData = {
          f_defect_id: row.f_defect_id,
          f_paper_id: row.f_paper_id,
          f_device_type: row.f_device_type,
          f_item_name: row.f_item_name,
          f_item_value: row.f_item_value,
          f_defect_level: row.f_defect_level,
          f_userinfo_code: row.f_userinfo_code,
          f_user_name: row.f_user_name,
          f_user_phone: row.f_user_phone,
          f_address: row.f_address,
          f_offsite_time: row.f_offsite_time,
          f_checker_name: row.f_checker_name,
          f_control_method: [],
          f_handler: this.$login.f.name,
          f_control_time: toStandardTimeString(),
          f_control_level: '',
          f_remark: ''
        }
        this.controlPhotoFiles = {}
        this.showControlModal = true
      },
      cancelControl() {
        this.showControlModal = false
        this.controlData = {}
        this.controlPhotoFiles = {}
      },
      async submitControl() {
        if (!this.controlData.f_control_method || this.controlData.f_control_method.length === 0) {
          this.$showMessage('请选择管控方式！')
          return
        }
        if (!this.controlData.f_handler) {
          this.$showMessage('请输入处理人！')
          return
        }
        if (!this.controlPhotoFiles.path) {
          this.$showMessage('请上传处理照片！')
          return
        }
        if (!this.controlData.f_control_level) {
          this.$showMessage('请选择管控后隐患级别！')
          return
        }

        try {
          let submitData = {
            f_defect_id: this.controlData.f_defect_id,
            f_paper_id: this.controlData.f_paper_id,
            f_control_method: Array.isArray(this.controlData.f_control_method)
              ? this.controlData.f_control_method.join(',')
              : this.controlData.f_control_method,
            f_handler: this.controlData.f_handler,
            f_control_time: this.controlData.f_control_time,
            f_control_level: this.controlData.f_control_level,
            f_remark: this.controlData.f_remark,
            // 直接存图片路径
            f_photos: this.controlPhotoFiles.name,
            f_photos_id: this.controlPhotoFiles.id,
            f_filiale_id: this.$login.f.orgid
          }

          let http = new HttpResetClass()
          let result = await http.load('POST', 'rs/logic/saveDefectControl', submitData,
            {resolveMsg: null, rejectMsg: null})

          if (result.data.code === 200 || result.data.success) {
            this.$showMessage('管控处理成功！')
            this.cancelControl()
            this.search()
          } else {
            this.$showMessage(result.data.msg || '管控处理失败！')
          }
        } catch (error) {
          console.error('管控处理失败：', error)
          this.$showMessage('网络异常，请稍后再试！')
        }
      },

      // ===== 配置 =====
      getDefectInfoConfig(val) {
        try {
          if (val.safecheck && val.safecheck.DevicesSetConfig != undefined) {
            this.DefectConfig = val.safecheck.DevicesConfig
            for (var Key1 in this.DefectConfig) {
              this.device_types.push({label: Key1, value: Key1})
              for (var Key2 in this.DefectConfig[Key1].items) {
                this.DefectConfigs.push({label: Key2, value: Key2})
              }
            }
          }
        } catch (e) {}
      },
      getSafeParam(val) {
        if (this.$appdata.getParam(val)) {
          return [{label: '全部', value: ''}, ...this.$appdata.getParam(val)]
        } else {
          return [{label: '全部', value: ''}]
        }
      },
      getRes(obj) {
        this.model.f_filialeid = this.$login.convertToIn(obj.resids);
        this.searchData.f_filialeid = this.$login.convertToIn(obj.resids);
        this.Mreadibook(this.model.f_filialeid)
        this.getAllMeterBook(this.model.f_filialeid)
      },
      Mreadibook(val) {
        this.sliceArea = []
        if (val) {
          new HttpResetClass().load('POST', `/rs/sql/safe_singleTable_OrderBy`, {data: {
            items: 'name', tablename: 't_zone',
            condition: `parentid in ${val}`, orderitem: 'id'
          }}, {resolveMsg: null, rejectMsg: null}).then((res) => {
            this.sliceArea.push({label: '全部', value: ''})
            res.data.forEach(ress => {
              this.sliceArea.push({label: ress.name, value: ress.name})
            })
          })
        }
      },
      getAllMeterBook(val) {
        this.cbcs = []
        if (val) {
          new HttpResetClass().load('POST', `/rs/sql/safe_singleTable_OrderBy`, {data: {
            items: 'f_book_name,f_book_slice_area,id', tablename: 't_meter_book',
            condition: `f_filiale_id in ${val}`, orderitem: 'id'
          }}, {resolveMsg: null, rejectMsg: null}).then((res) => {
            res.data.forEach(ress => {
              this.cbcs.push({id: ress.id, f_book_name: ress.f_book_name, f_book_slice_area: ress.f_book_slice_area})
            })
            this.cbc = [{label: '全部', value: ''}]
            this.cbcs.forEach(res => {
              this.cbc.push({label: res.f_book_name, value: res.id})
            })
          })
        }
      },
      readConfig() {
        let gen = asyncReady(this)
        return co(gen)
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      refresh() {
        this.$refs.paged.$refs.criteria.search()
      },
      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) {
        args.condition += ` and f_filialeid in ${this.model.f_filialeid}`
        this.model.search(args.condition, args.model, args.condValue)
      }
    },
    // 文件上传事件：上传成功后查 t_files 拿 f_downloadpath 直接存路径
    events: {
      onFileUpload: function (file, res) {
        if (res.result !== 'success' || !res.id) {
          console.error('照片上传失败', res)
          this.$showMessage('照片上传失败，请重试！')
          return
        }
        // 查询 t_files 获取文件实际路径
        let http = new HttpResetClass()
        http.load('POST', 'rs/sql/safe_singleTable_OrderBy', {
          data: {
            items: ' * ',
            tablename: ' t_files',
            condition: ` id = '${this.esc(res.id)}'`,
            orderitem: ' id desc'
          }
        }, {resolveMsg: null, rejectMsg: '获取文件路径失败'}).then((result) => {
          if (result.data && result.data.length > 0) {
            this.controlPhotoFiles = {
              path: result.data[0].f_filename,
              id: result.data[0].id,
              name: result.data[0].f_filename
            }
            this.$showMessage('照片上传成功！')
          }
        })
      }
    },
    ready() {
      this.readConfig()
      this.function = this.f
      let gen = select(this)
      this.Mreadibook(`('${this.$login.f.orgid}')`)
      co(gen)
    }
  }
</script>
