<template>
  <div class="auto">
    <disable-record partial='list'  :searchdata="row" @updatedata="handleChildStatusUpdate"></disable-Record>
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row">
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">停用时间</label>
            <input type="text" class="input_search" v-model="disable.f_operate_date" style="width:60%" disabled placeholder="上次停用时间"/>
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">操作人</label>
            <input type="text" class="input_search" v-model="disable.f_operator" style="width:60%" disabled placeholder="操作人"/>
          </div>
          <div class="col-sm-12  form-group">
            <label class="font_normal_body">停用原因</label>
            <input type="text" class="input_search" v-model="disable.f_comments" style="width:60%" disabled placeholder="停用原因"/>
          </div>
          <div class="col-sm-12  form-group">
            <label class="font_normal_body">其他原因</label>
            <input type="text" class="input_search" v-model="disable.f_othereason" style="width:60%" disabled placeholder="停用原因"/>
          </div>
        </div>
        <div class="row">
          <div class="col-sm-4 form-group">
            <label for="f_reason" class="font_normal_body">启用原因</label>
            <v-select v-model="model.f_comments" style="width:60%"
                      placeholder='启用原因'
                      :value.sync="model.f_comments"
                      :options='reason'
                      v-ref:reason
                      close-on-select ></v-select>
          </div>
          <div class="checkbox col-sm-4" style="margin-left:5%" v-if="!kaifa&&row.f_meter_type ==='物联网表'">
            <label>
              <input style="zoom: 200%;top: 1px;left: 14px;" @click="changeisuse(true)" type="checkbox" name="optionsRadios" :id="'optionsRadios'+index" value="option1"  :checked="kaifa?'checked':''">
              开阀
            </label>
          </div>
          <div class="checkbox col-sm-4" style="margin-left:5%" v-if="kaifa&&row.f_meter_type ==='物联网表'">
            <label>
              <input style="zoom: 200%;top: 1px;left: 14px;" @click="changeisuse(false)" type="checkbox" name="optionsRadios" :id="'optionsRadios'+index" value="option1"  :checked="kaifa?'checked':''">
              开阀
            </label>
          </div>
        </div>
        <div class="row" style="margin-top:1px;">
          <div class="col-sm-8 form-group">
            <label for="f_reason" class="font_normal_body">备&emsp;&emsp;注</label>
            <input class="input_search" style="width:80%" v-model="model.f_othereason"  placeholder="备注"/>
          </div>
        </div>
      </form>
      <div style="text-align:right;">
        <button :class="['btn-green', { disabled: !authArr.includes('强制启用') }]" @click="forceConfirm()" :disabled="!authArr.includes('强制启用')">强制启用</button>
        <button class="button_search button_spacing" @click="confirm()"  :disabled="!isEnabled">确认</button>
        <button class="button_clear" @click="clean()" >取消</button>
      </div>
      <upload :blodid="model.f_userinfo_id"  :businessid="randomBusinessId" isremark="true" :deleteimg="true" fusetype="表具启用"></upload>
    </validator>
  </div>
</template>
<script>
/**
*综合业务
*表具启用组件
*/

let meterEnable = async function (self, model, row) {
  let data = {
    f_userfiles_id: {
      f_userfiles_id: row.f_userfiles_id,
      f_table_state: '正常',
      version: row.version
    },
    f_user_id: row.f_user_id,
    record_userinfo: row.f_userinfo_id,
    f_userinfo_id: row.f_userinfo_id,
    f_user_name: row.f_user_name,
    f_user_phone: row.f_user_phone,
    f_state: '有效',
    f_comments: model.f_comments[0],
    f_othereason: model.f_othereason,
    f_operat_type: '启用',
    f_describe: `${self.$login.f.name}对客户${row.f_user_name}进行表具启用操作`,
    f_operator: self.$login.f.name,
    f_operatorid: self.$login.f.id,
    f_orgid: self.$login.f.orgid,
    f_orgname: self.$login.f.orgs,
    f_depid: self.$login.f.depids,
    f_depname: self.$login.f.deps,
    f_zoneid: self.$login.f.zoneid,
    f_zones: self.$login.f.zones
  }
  let returnData = await self.$resetpost('rs/entity/t_enable', data, {resolveMsg: '表具启用成功', rejectMsg: '表具启用失败'})
  console.log('1111111111111')
  console.log(returnData)
  console.log('111111111111')
  let param1 = []
  for (let row of self.resid) {
    param1.push({id: row.id})
  }
  let param = {
    param: param1,
    f_blobid: returnData.data.id
  }
  await self.$resetpost('rs/logic/updatefiles', param)
  if (self.kaifa) {
    let array = [{
      f_user_id: self.row.f_userfiles_id,
      f_alias: self.row.f_alias,
      f_userinfo_id: self.row.f_userinfo_id,
      f_orgname: self.$login.f.orgs,
      f_orgid: self.$login.f.orgid,
      f_meternumber: self.row.f_meternumber
    }]
    await self.$resetpost('rs/logic/iot_instructUpdateAndDelect',
      {
        data: {
          data1: {
            f_apply_type: '阀门控制',
            userid: array,
            nowDate: new Date(),
            f_apply_value: '开阀',
            f_apply_beizhu: '表具停用自动开阀',
            f_apply_operator: self.$login.f.name
          },
          data2: {
            userid: array,
            instructtype: '阀门控制',
            isOpen: '开阀',
            instructTitle: '手动开阀',
            f_instruct_state: '待发送',
            inputtor: self.$login.f.name
          }
        }
      })
  }
  self.$dispatch('success')
}

export default {
  title: '启用管理',
  props: ['row'],
  data () {
    return {
      model: {
      },
      select: {
        f_date: '',
        f_reason: ''
      },
      resid: [],
      isEnabled: false,
      randomBusinessId: null,
      reason: this.$appdata.getParam('启用原因'),
      disable: {},
      kaifa: false
    }
  },
  ready () {
    this.deactivatedReason()
    this.getRandomId()
  },
  events: {
    // 删除Resid数组元素
    'delResid' (val) {
      this.resid.$remove({id: val, f_biobid: ''})
      // this.resid.splice(this.resid.indexOf({id:val,f_biobid:''}),1);
    },
    // 增加Resid数组元素
    'resid' (val) {
      console.log('-=', val)
      this.resid.push({id: val, f_biobid: ''})
    }
  },
  methods: {
    handleChildStatusUpdate (status) {
      // 根据子组件的状态更新父组件逻辑
      console.log('子组件状态更新为：', status)
      this.isEnabled = status
    },
    getRandomId () {
      this.randomBusinessId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let res = Math.random() * 16 | 0
        let v = c == 'x' ? res : (res & 0x3 | 0x8)
        return v.toString(16)
      })
    },
    changeisuse (val) {
      if (val) {
        this.kaifa = true
      } else {
        this.kaifa = false
      }
    },
    // 查询停用原因
    async deactivatedReason () {
      let data = {
        tablename: 't_disable',
        condition: `id = (select max(id) from t_disable where f_userfiles_id = '${this.row.f_userfiles_id}')`
      }
      let res = await this.$resetpost('rs/sql/saleSingleTable', {data: data}, {resolveMsg: null, rejectMsg: '获取上次停用原因失败'})
      this.disable = res.data[0]
    },
    forceConfirm () {
      meterEnable(this, this.model, this.row)
    },
    async confirm () {
      try {
        // 查询是否存在未处理的隐患
        const data = {
          condition: `f_userinfo_id = '${this.row.f_userinfo_id}' AND f_is_deal = '未处理'`
        }

        const response = await this.$resetpost(
          'rs/sql/HiddenDanger', // 假设这是查询接口
          { data },
          { resolveMsg: null, rejectMsg: '查询隐患失败' }
        )
        console.log('124565', response.data)
        console.log('124565', response.data.length)
        // 检查查询结果是否有未处理的隐患
        if (response.data && response.data.length > 0) {
          this.$showAlert('还有未处理的隐患，无法启用！', 'warning', 3000)
          return
        }

        // 如果没有未处理的隐患，则执行启用逻辑
        await meterEnable(this, this.model, this.row)
      } catch (error) {
        console.error('查询或启用操作失败：', error)
        this.$error('操作失败，请重试！')
      }
    },
    clean () {
      this.$dispatch('refresh')
    }
  },
  computed: {
    authArr () {
      return this.$login.r ? this.$login.r : []
    }
  }
}
</script>

<style>
.btn-green {
  background-color: green;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
}

.btn-green.disabled {
  background-color: gray;
  color: #ccc;
  cursor: not-allowed; /* 禁止鼠标点击的样式 */
}
</style>

