<template >
  <div style="height: 100%;overflow-y: scroll">
    <div class="panel panel-default auto repair-info-content">
      <div class="panel-body" >
        <div class="panel panel-default well" v-for="row in model">
          <div class="bg-info">
            <div class="row form-group">
              <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="row">
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">设备类型:</labe>
                    <!--<input type="text" style="width: 75%;float:right" class="form-control"  v-model="row.f_devices_type"  />-->
                    <v-select  style="float: right;width: 60%;"  :value.sync="row.f_devices_type"
                               v-model="row.f_devices_type"
                               placeholder='请选择'
                               :width='wid'
                               :options="devices_types"
                               close-on-select value-single></v-select>
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">设备编号:</labe><input type="text" style="width: 60%;float:right" class="form-control" v-model="row.f_devices_no"  />
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">设备品牌:</labe><input type="text" style="width: 60%;float:right" class="form-control" v-model="row.f_brand"  />
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">设备型号:</labe><input type="text" style="width: 60%;float:right" class="form-control" v-model="row.f_devices_model"  />
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">安装日期:</labe><input type="text" style="width: 60%;float:right" class="form-control" v-model="row.f_input_date"  />
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">设备数量:</labe><input type="text" style="width: 60%;float:right" class="form-control" v-model="row.f_devices_num"  />
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">管道类型:</labe>
                    <!--<input type="text" style="width: 75%;float:right" class="form-control" v-model="row.f_pipe_type"  />-->
                    <v-select  style="width: 60%;float: right"  :value.sync="row.f_pipe_type"
                               v-model="row.f_pipe_type"
                               placeholder='请选择'
                               :width="wid"
                               :options="pipe_types"
                               close-on-select value-single></v-select>
                  </div>
                  <div class="row auto" style="margin: 5px 0">
                    <labe style="line-height: 35px;">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</labe><input type="text" style="width: 60%;float:right" class="form-control" v-model="row.f_comments"  />
                  </div>
                  <div>
                    <button class="button_search button_spacing" style="margin-left: 40%;margin-top: 10px;" @click="delDevices($index)">删除该设备</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <p v-show="model.length == 0 ">此用户暂无设备</p>
        <div style="margin: 0 auto">
          <button class="button_search button_spacing" @click="addDevices" style="margin-left: 15%;margin-right: 15px;">新增设备</button>
          <button class="button_search button_spacing" @click="save">保存设备信息</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { PagedList,HttpResetClass } from 'vue-client'
  import * as Util from "../Util";
  import Vue from 'vue'
  import co from 'co'
  export default {
    title: '设备查看',
    data () {
      return {
        model: [],
        show: false,
        row: Object,
        userinfo:Object,
        wid:"60%",
        devices_types:Vue.$appdata.getParam('设备类型')?Vue.$appdata.getParam('设备类型'):[{label:'热水器',value:'热水器'},{label:'壁挂炉',value:'壁挂炉'},{label:'灶具',value:'灶具'}],
        // devices_types:[{label:'热水器',value:'热水器'},{label:'壁挂炉',value:'壁挂炉'},{label:'灶具',value:'灶具'}],
        pipe_types:[{label:'橡胶管',value:'橡胶管'},{label:'波纹管',value:'波纹管'},{label:'钢管',value:'钢管'}],
        update:false
      }
    },
    props: {
      user: {
        type: Object
      },
      f_user_type:'',
      f_userinfo_id:''
    },
    methods: {
      addDevices(){
        //Vue.user.name
        let obj={
          "f_input_person":'hwq',
          "f_picture":null,
          "f_pipe_type":null,
          "f_pipeinstall_date":null,
          "f_pipe_connection":null,
          "f_expire_date":null,
          "f_pipeexpire_date":null,
          "f_comments":null,
          "f_operate_date":Util.toStandardTimeString(),
          "f_devices_num":null,
          "f_userfiles_id":this.userinfo.baseinfo.base.f_userfiles_id,
          "f_process_id":null,
          "f_devices_type":null,
          "f_make_date":null,
          "f_userinfo_id":null,
          "f_input_date":Util.toStandardTimeString(),
          "f_state":"有效",
          "id":null,
          "f_brand":null,
          "f_devices_model":null,
          "f_devices_no":null
        }
        this.model.push(obj)
      },
      delDevices(idx){
        this.$showMessage('删除后不可恢复，确认删除吗？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.model.splice(idx,1)
          }
        })
      },
      getmodel () {
        let userinfoid
        if(this.f_userinfo_id){
          userinfoid = this.f_userinfo_id
        }else{
          userinfoid = this.user.f_userinfo_id
          //tag
        }
        //tag
        /*new HttpResetClass().load('post',`${this.$androidUtil.getProxyUrl()}/rs/logic/commonSearchByUser`, {data: {items: '*', tablename: 't_devices', orderitem: 'f_input_date desc', condition:`f_userinfo_id = '${userinfoid}'`}}, {resolveMsg: null, rejectMsg: null}).then((row) => {
          this.model = row.data
        })*/
        new HttpResetClass().load('post',`${this.$androidUtil.getProxyUrl()}/rs/logic/filemanage_getFilesDetail`, {
            //f_userinfo_code:'12381344',
            f_userinfo_id:userinfoid
          }, {resolveMsg: null, rejectMsg: null}).then((res) => {
            //tag
            this.userinfo = res.data
            this.model = this.userinfo.meterinfo[0].devicesinfo
        })
      },
      back(){

      },
      save(){
        if (this.f_user_type == "非民用") {
          let hasAlarmOrValve = false;
          for (let i = 0; i < this.model.length; i++) {
            if (this.model[i].f_devices_type === '报警器' || this.model[i].f_devices_type === '切断阀') {
              hasAlarmOrValve = true;
              break;
            }
          }
          if (!hasAlarmOrValve) {
            this.$showMessage("请添加报警器或切断阀！  ")
            return;
          }
        }
        if(this.model.length==0){
          this.$showMessage("请添加设备信息后再点击保存！")
          return
        }
        if(this.update){
          this.$showMessage("正在保存设备中请稍等...")
          return
        }
        this.update=true
        let userinfo={}
        userinfo=this.userinfo.baseinfo.base
        userinfo=Object.assign(userinfo,{f_operator_record:Vue.user.name,userfiles:this.userinfo.meterinfo})
        userinfo.userfiles[0].devicesinfo.forEach((r)=>{
          r.img={content: null, filename: null}
        })
        let response=new HttpResetClass().load("POST", `${this.$androidUtil.getProxyUrl()}/rs/logic/filemanage_fileSave`, {
          data:{
            userinfo:userinfo,
            address:{id:this.userinfo.addressinfo.id,oldid:this.userinfo.addressinfo.id}
          }
        }, {resolveMsg: "修改成功！", rejectMsg: "失败，请联系管理员"}).then((res)=>{
          //tag)
          this.update=false
          if(res.data.status==200){
            //tag
            //tag
            this.userinfo.baseinfo.base.version=res.data.result[0].version
            this.$showMessage("修改设备信息成功！")
          }else if(res.data.status==505){
            this.$showMessage("修改设备信息失败！请重新加载数据")
          }else{
            this.$showMessage("修改设备信息失败!")
          }
        }).catch((mes)=>{
          this.update=true
          if(mes.status==505){
            this.$showMessage(mes.data+'请重新进入当前页面进行操作！')
          }else{
            this.$showMessage('网络异常,请重新进入当前页面进行操作！')
          }
        })
      }
    },
    /*watch: {
      'user' (val) {
        // this.model.search(`card_id = ${this.user.card_id}`)
        if(this.user){
          if (!(this.user.isNuN)) {
            this.model= []
            this.getmodel()
          }
        }
      },
      'f_userinfo_id'(){
        if(this.f_userinfo_id){
          this.model= []
          this.getmodel()
        }
      }
    },*/
    ready () {
      this.getmodel()
      /*let response=new HttpResetClass().load("POST", "/rs/logic/getSaleInitData", {
        f_orgid: '305'
      }, {resolveMsg: null, rejectMsg: null})*/
    }
  }
</script>
<style>
  .app-botton {
    position:fixed;
    background: #87b2dd;
    color: #FFF;
    padding: 8px;
    text-align: center;
    font-size: 1.2em;
    z-index: 10;
    opacity:0.5;
    bottom: 50px;
    margin-top: -10px;
    height: 40px;
  }
</style>
