<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">
        <validator name='v'>

      <div class="form-group">
      <label for="f_defect" class=" col-sm-3 control-label">隐患:</label>
      <div class="col-sm-3">
        <input class="form-control" type="text" v-model="model.f_defect" placeholder='隐患' v-validate:f_defect='{ required: true }'>
      </div>

      <label for="f_defect_level"  class="col-sm-3 control-label">隐患级别:</label>
      <div class="col-sm-3">
        <input class="form-control" type="text" v-model="model.f_defect_level" placeholder='例如：A1' v-validate:f_defect_level='{ required: true }'>
      </div>

    <div class="form-group">
      <label for="f_defect_field"  class=" col-sm-3 control-label">隐患字段:</label>
      <div class="col-sm-3">
        <input class="form-control" type="text" v-model="model.f_defect_field" placeholder='例如：f_defect_field' v-validate:f_defect_field='{ required: true }'>
      </div>

      <label for="f_defect_path" class="col-sm-3 control-label">隐患照片路径字段:</label>
      <div class="col-sm-3">
        <input class="form-control" type="text" v-model="model.f_defect_path" placeholder='例如：f_defect_path' v-validate:f_defect_path='{ required: true }'>
      </div>

    </div>
    <div class="form-group">
      <label  class=" col-sm-3 control-label">设备类别:</label>
      <div class="col-sm-3">
        <v-select id="f_state" :value.sync="model.f_device_type"
                  :options='devices'
                  close-on-select clear-button></v-select>
      </div>

      <label  class="col-sm-3 control-label">设备:</label>
      <div class="col-sm-3">
        <input class="form-control" type="text" v-model="model.f_device_id" placeholder='设备' >
      </div>

    </div>
    <div class="form-group">
      <label  for="f_remark" class=" col-sm-3 control-label">备注:</label>
      <div class="col-sm-3">
        <input class="form-control" type="text" v-model="model.f_remark" placeholder='备注'>
      </div>
    </div>
        </validator>
      </div>
    </div>
    <div slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-primary" @click="addDefect" >保存</button>
      <button type="button" class="btn btn-default" @click="$parent.undo()">取消</button>
    </div>
  </modal>
</template>

<script>
  import Vue from 'vue'


  export default {
    title: '添加隐患信息',
    props:['showModal'],
    data() {
      return {
        model:{},
        devices:[]
      }
    },
    methods: {
      addDefect() {
        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(`api/af-safecheck/logic/添加总隐患信息`,
              {data: this.model},
              {resolveMsg: null, rejectMsg: null}).then(
              (response) => {
                window.alert('添加隐患信息成功')
              }
            ).catch(
              (response) => {
                window.alert('添加隐患信息失败')
              }
            )
          }
        this.$parent.undo()
      },
    },
    ready(){
      let that = this
      this.$resetpost("api/af-safecheck/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>
