<template>
  <modal :show.sync="$parent.showModal" v-ref:modal>
    <div slot="modal-header" class="modal-header">
      <h4 class="modal-title">
        添加安检设备
      </h4>
    </div>
    <div slot="modal-body" class="modal-body">
      <div class="row auto">
        <div class="form-group">
          <label class=" col-sm-3 control-label">设备名称:</label>
          <div class="col-sm-3">
            <input class="form-control" type="text" v-model="model.f_defect" placeholder='设备名称'>
          </div>
          <label  class="col-sm-3 control-label">用户类型:</label>
          <div class="col-sm-3">
            <v-select id="f_user_type" :value.sync="model.f_user_type"
                      :options='user_type'
                      close-on-select clear-button></v-select>
          </div>
        </div>
        <div class="form-group">
          <label  class=" col-sm-3 control-label">设备保存字段:</label>
          <div class="col-sm-3">
            <input class="form-control" type="text" v-model="model.f_device_field" placeholder='例如：f_device_field'>
          </div>

          <label class="col-sm-3 control-label">是否必检:</label>
          <div class="col-sm-3">
            <input class="form-control" type="text" v-model="model.f_write_must" placeholder='是否必检'>
          </div>
        </div>
    </div>
    </div>
    <div slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-primary" @click="addDevice" >保存</button>
      <button type="button" class="btn btn-default" @click="$parent.undo()">取消</button>
    </div>
  </modal>
</template>

<script>
  import Vue from 'vue'


  export default {
    title: '添加设备信息',
    data() {
      return {
        model:{},
        devices:[]
      }
    },
    methods: {
      addDevice() {
        let errorring = true
        if(this.model.f_defect_field)
          if(this.model.f_defect_field.endsWith("_path")){
            errorring = false
            alert("隐患字段不能以 _path结尾")
          }
        if(errorring) {
          if (this.model.f_defect_path) {
            if (!this.model.f_defect_path.endsWith("_path")) {
              errorring = false
              alert("图片路径字段必须以 _path结尾")
            }
          }
        }
        if(errorring) {
          this.$resetpost('rs/entity/t_device_info',
            {data: this.model},
            {resolveMsg: null, rejectMsg: null}).then(
            (response) => {
              window.alert('添加隐患信息成功')
            }
          ).catch(
            (response) => {
              window.alert('添加隐患信息失败')
            }
          )
        }
        this.$parent.undo()
      },
    },
    ready(){
      let that = this
      this.$resetpost("rs/logic/获取安检设备",{condition: '1 =1'}).then((respanse) => {
        respanse.data.data.forEach((item)=>{
          that.devices.push({label:item.f_device_name,value:item.f_device_name})
        })
      })
    }
  }
</script>
