<template>
  <div class="auto">
    <modal :show.sync="show_devices" width="80%" title="表具设备信息" v-ref:modal large backdrop="false">
      <article slot="modal-body" class="modal-body" style="height: auto">
        <div class="flex-row flex-deviceinfo">
          <div class="flex-row flex-two-info panel panel-default" v-for="row in devicesinfo">
            <avatar-upload :img-src.sync="row.img.content" :filename.sync="row.img.filename"></avatar-upload>
            <form class="form-horizontal select-overspread " style="flex: 1;">
              <div class="row auto" style="margin-top: 10px;margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">设备类型</label>
                  <v-select :value.sync="row.f_devices_type" v-model="row.f_devices_type"
                            :options='devicetypes' placeholder='设备类型' :value-single="true"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class=" font_normal_body">设备编号</label>
                  <input type="text" class="input_search" style="width:60%" v-model="row.f_devices_no"
                         placeholder='设备编号' v-next-el="{id: $index+'brand'}">
                </div>
              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">设备品牌</label>
                  <input type="text" class="input_search" style="width:60%" v-model="row.f_brand" placeholder='设备品牌'
                         v-next-el="{id: $index+'devicesmodel'}" :id="$index+'brand'">
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">安装人&nbsp;&nbsp;&nbsp;</label>
                  <input type="text" class="input_search" style="width:60%" v-model="row.f_input_person"
                         placeholder='安装人' v-next-el="{id: $index+'devicesmodel'}" :id="$index+'brand'">
                </div>
              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">设备型号</label>
                  <input type="text" class="input_search" style="width:60%" v-model="row.f_devices_model"
                         placeholder='设备型号' :id="$index+'devicesmodel'">
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">安装日期</label>
                  <datepicker placeholder="安装日期" style="width: 60%"
                              v-model="row.f_input_date"
                              :value.sync="row.f_input_date"
                              :format="'yyyy-MM-dd HH:mm:ss'">
                  </datepicker>
                </div>
              </div>
              <div class="row auto" style="margin-left: 10px;">

                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">生产日期</label>
                  <datepicker placeholder="生产日期" style="width: 60%"
                              v-model="row.f_make_date"
                              :value.sync="row.f_make_date"
                              :onchange="expireDate($index)"
                              :format="'yyyy-MM-dd'">
                  </datepicker>
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">使用年限</label>
                  <input type="number" class="input_search" style="width: 60%" v-model="row.f_service_life"
                         :value.sync="row.f_service_life"
                          :onchange="expireDate($index)" >
                </div>
              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;" v-if="devicesinfoShow">
                  <label class="font_normal_body">到期日期</label>
                  <input type="test" class="input_search" style="width: 60%" v-model="row.f_expire_date"
                         :value.sync="row.f_expire_date"
                         placeholder="到期日期"  disabled></input>
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">设备数量</label>
                  <input type="text" class="input_search" style="width:60%" v-model="row.f_devices_num"
                         :value.sync="row.f_devices_num"
                         placeholder='设备数量' :id="$index+'devicesmodel'">
                </div>

              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">安装日期</label>
                  <datepicker placeholder="连接管安装日期" style="width: 60%"
                              v-model="row.f_pipeinstall_date"
                              :value.sync="row.f_pipeinstall_date"
                              :format="'yyyy-MM-dd HH:mm:ss'">
                  </datepicker>
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">管道类型</label>

                  <v-select :value.sync="row.f_pipe_type" v-model="row.f_pipe_type"
                            :options='pipetypes' placeholder='管道类型' :value-single="true"
                            close-on-select></v-select>
                </div>

              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">到期日期</label>
                  <datepicker placeholder="连接管到期日期" style="width: 60%"
                              v-model="row.f_pipeexpire_date"
                              :value.sync="row.f_pipeexpire_date"
                              :format="'yyyy-MM-dd HH:mm:ss'">
                  </datepicker>
                </div>
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body" title="用户设备状态">设备状态</label>
                  <v-select :value.sync="row.f_userinfodevices_state" v-model="row.f_userinfodevices_state"
                            :options='userdevicestate' placeholder='设备状态' :value-single="true"
                            close-on-select></v-select>
                </div>
              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div class="col-sm-6 form-group" style="padding-right: 5px;">
                  <label class="font_normal_body">购买方式</label>
                  <v-select :value.sync="row.WatchPurchase" v-model="row.WatchPurchase"
                            :options='WatchPurchases' placeholder='购买方式' :value-single="true"
                            close-on-select></v-select>
                </div>
              </div>
              <div class="row auto" style="margin-left: 10px;">
                <div style="" class="col-sm-8 form-group">
                  <label for="f_comments" class="font_normal_body ">&nbsp;&nbsp;&nbsp;备&nbsp;&nbsp;&nbsp;注</label>
                  <input class="input_search" style="width:100%" v-model="row.f_comments"/>
                </div>
              </div>
            </form>
            <img :src="imgdelete" alt="图片加载失败" class="img-rounded"
                 style="width: 60px;padding: 20px;margin-left: -15px;cursor: pointer;"
                 @click="deleteDevice($index, row)" v-if="row.f_state !== '有效'">
          </div>
          <div class="panel panel-default flex-two-info text-center" style="line-height: 164px;">
            <img :src="imgadd" alt="图片加载失败1" class="img-rounded" style="width: 60px;padding: 10px;cursor: pointer;"
                 @click="addDevice()">
          </div>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
      </footer>
    </modal>
  </div>

</template>

<script>
import moment from 'moment'
export default {
  title: '表具设备信息',
  data () {
    return {
      imgdelete: '/images/mainicon/deletedevice.png',
      imgadd: '/images/mainicon/adddevice.png',
      devicetypes: this.$appdata.getParam('设备类型'),
      pipetypes: this.$appdata.getParam('管道类型'),
      WatchPurchases: this.$appdata.getParam('购买方式'),
      userdevicestate: this.$appdata.getParam('用户设备状态'),
      f_make_date: '',
      f_expire_date: '',
      f_service_life: 0,
      devicesinfoShow: true
    }
  },
  props: {
    devicesinfo: {
      type: Array
    },
    show_devices: {
      type: Boolean,
      default: false
    }
  },
  computed: {
  },
  methods: {
    expireDate (index) {
      if (this.devicesinfo[index].f_make_date === '' || this.devicesinfo[index].f_make_date === undefined || this.devicesinfo[index].f_service_life === 0 || this.devicesinfo[index].f_service_life === undefined) {

      } else {
        var b = new Date(this.devicesinfo[index].f_make_date)
        var year = (b.getFullYear() - 0) + (this.devicesinfo[index].f_service_life - 0)
        var month = (b.getMonth() - 0) + 1
        var day = b.getDate()
        // this.$set(this.devicesinfo, index, {f_expire_date: year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)})
        this.devicesinfo[index].f_expire_date = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
        this.devicesinfoShow = false
        this.$nextTick(() => {
          this.devicesinfoShow = true
        })
      }
    },
    deleteDevice (index, row) {
      this.devicesinfo.splice(index, 1)
    },
    addDevice () {
      console.log('设备信息', this.devicesinfo)
      this.devicesinfo.push({
        f_devices_type: '家用灶具',
        f_input_date: this.$login.toStandardTimeString()
      })
    }
  }
}
</script>
<style lang="less">
.flex-deviceinfo {
  padding: 0px 20px;
  justify-content: space-between;
  height: auto;
  flex-wrap: wrap;
}

.flex-two-info {
  margin-bottom: 10px;
  align-items: center;
  width: 48%;
}
</style>
