<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetName">
              <a-input v-model="model.assetName" placeholder="请输入设备名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetCode">
              <a-input v-model="model.assetCode" placeholder="请输入设备编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetBrand">
              <a-input v-model="model.assetBrand" placeholder="请输入设备品牌"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetModel">
              <a-input v-model="model.assetModel" placeholder="请输入设备型号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetStatus">
              <a-select style="width: 100%" v-model="model.assetStatus" placeholder="请选择设备状态">
                <a-select-option v-for="item in dictOptions" :key="item.value" :value="item.value">{{item.label}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="首次使用时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstUseDate">
              <j-date placeholder="请选择首次使用时间" v-model="model.firstUseDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="使用年限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useLife">
              <a-input type="number" v-model="model.useLife" placeholder="请输入使用年限" addon-after="年" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="采购价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchasePrice">
              <a-input v-model="model.purchasePrice" placeholder="请输入采购价格" addon-after="元" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所在位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetPosition">
              <a-input v-model="model.assetPosition" placeholder="请输入所在位置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属小区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="platformName">
              <a-input v-model="model.platformName" placeholder="请输入所属小区"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="headName">
              <a-input v-model="model.headName" placeholder="请输入负责人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="headPhone">
              <a-input v-model="model.headPhone" placeholder="请输入联系方式"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {ajaxGetDictItems,getDictItemsFromCache} from '@/api/api'

  export default {
    name: 'PropertyChargruleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          assetName: '',
          assetCode: '',
          assetBrand: '',
          assetModel: '',
          assetStatus: '1',
          firstUseDate: '',
          useLife: '',
          purchasePrice: '',
          assetPosition: '',
          communityCode: 'P00039',
          headName: '',
          headPhone: ''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          assetName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
          assetCode: [{ required: true, message: '请输入设备编码', trigger: 'change' }],
          assetPosition: [{ required: true, message: '请输入所在位置', trigger: 'blur' }],
          communityCode: [{ required: true, message: '请输入所属小区', trigger: 'blur' }]
        },
        url: {
          add: "/property-company/asset/propertyAsset/add",
          edit: "/property-company/asset/propertyAsset/edit",
          queryById: "/property-company/asset/propertyAsset/queryById"
        },
        dictOptions: []
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictData()
    },
    methods: {
      initDictData() {
        //优先从缓存中读取字典配置
        if(getDictItemsFromCache('device_status')){
          this.dictOptions = getDictItemsFromCache('device_status');
          return
        }
        // //根据字典Code, 初始化字典数组
        ajaxGetDictItems('device_status', null).then((res) => {
          if (res.success) {
            this.dictOptions = res.result;
          }
        })
      },
      ruleNumberInput(event) {
        let rateValue = event.target.value.replace(/[^\d]/g,"");//清除"数字"和"."和"-"以外的字符
        this.model['empowerDays'] = rateValue
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        console.log(record)
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>