<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="communityCode">
              <a-input v-model="model.communityCode" 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="请选择负责人"  readOnly @click="handleAddUserRole" ></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="联系方式" readOnly disabled></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>
  </a-spin>
</template>

<script>
  import Vue from 'vue'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {ajaxGetDictItems,getDictItemsFromCache} from '@/api/api'
  import { USER_INFO } from "@/store/mutation-types"
  import SelectUserModal from './SelectUserModal'

  export default {
    name: 'PropertyChargruleForm',
    components: {SelectUserModal
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          assetName: '',
          assetCode: '',
          assetBrand: '',
          assetModel: '',
          assetStatus: '1',
          firstUseDate: '',
          useLife: '',
          purchasePrice: '',
          assetPosition: '',
          // communityCode: '1242342342',
          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' }],
          headName: [{ required: true, message: '请选择负责人', trigger: 'change' }],
          // communityCode: [{ required: true, message: '请输入所属小区', trigger: 'blur' }]
        },
        url: {
          add: "/property-community/asset/propertyAsset/add",
          edit: "/property-community/asset/propertyAsset/edit",
          queryById: "/property-community/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);
        let data = Vue.ls.get(USER_INFO)
        this.model.assetPosition = data.platformName
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      handleAddUserRole() {
        if (this.currentRoleId == '') {
          this.$message.error('请选择一个角色!')
        } else {
          this.$refs.selectUserModal.visible = true
        }
      },
      selectOK(data) {
        this.model.headName = data.realname
        this.model.headPhone = data.phone
      },
      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>