<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="communityName">
              <a-input v-model="model.communityName" placeholder="请输入小区名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="用途" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useTo">
              <a-select style="width: 100%" v-model="model.useTo" 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="communityArea">
              <a-input v-model="model.communityArea" placeholder="请输入所属地区"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addressInfo">
              <a-input v-model="model.addressInfo" placeholder="请输入详细地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="建成日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="completedDate">
              <j-date placeholder="请选择建成日期" v-model="model.completedDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="占地面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coverArea">
              <a-input v-model="model.coverArea" 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="buildingNum">
              <a-input v-model="model.buildingNum" 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="houseNum">
              <a-input v-model="model.houseNum" 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="parkingSpace">
              <a-input v-model="model.parkingSpace" 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="chargingStation">
              <a-input type="number" v-model="model.chargingStation" placeholder="请输入充电桩" addon-after="个" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="小区图片" :labelCol="{ xs:{ span: 24 }, sm:{ span: 3 } }" :wrapperCol="wrapperCol" prop="communityImage">
              <j-image-upload :isMultiple="true" text="点击上传" bizPath="scott/pic" v-model="model.communityImage"></j-image-upload>
            <p style="line-height:1;margin:0">建议尺寸640*320，小于10M的JPG、PNG格式图片，可上传多张</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="小区简介" :labelCol="{ xs:{ span: 24 }, sm:{ span: 3 } }" :wrapperCol="wrapperCol" prop="communityIntro">
              <a-textarea placeholder="小区简介" v-model="model.communityIntro" allow-clear />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="小区管理员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adminName">
              <a-input v-model="model.adminName" placeholder="请输入小区管理员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管理员联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adminPhone">
              <a-input v-model="model.adminPhone" placeholder="请输入管理员联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管理员登录密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adminPassword">
              <a-input v-model="model.adminPassword" placeholder="请输入管理员登录密码"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="管理员头像" :labelCol="{ xs:{ span: 24 }, sm:{ span: 3 } }" :wrapperCol="wrapperCol" prop="adminAvatar">
              <j-image-upload :isMultiple="false" text="点击上传" bizPath="scott/pic" v-model="model.adminAvatar"></j-image-upload>
            </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'
  import { isMobile } from '@/utils/validate'

  export default {
    name: 'PropertyChargruleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      const validateToNextPhone = (rule, value, callback)=> {
        if (value && isMobile(value)) {
          callback();
        } else {
          callback('请输入正确的联系人电话！');
        }
      }
      return {
        model:{
          communityName: '',
          useTo: '',
          communityArea: '',
          addressInfo: '',
          completedDate: '',
          coverArea: '',
          buildingNum: '',
          houseNum: '',
          parkingSpace: '',
          chargingStation: '',
          communityImage: '',
          communityIntro: '',
          adminName: '',
          adminPhone: '',
          adminPassword: '',
          adminAvatar: '',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          communityName: [{ required: true, message: '请输入小区名称', trigger: 'blur' }],
          useTo: [{ required: true, message: '请选择用途', trigger: 'change' }],
          communityArea: [{ required: true, message: '请输入所属地区', trigger: 'blur' }],
          coverArea: [{ required: true, message: '请输入占地面积', trigger: 'blur' }],
          buildingNum: [{ required: true, message: '请输入楼栋数量', trigger: 'blur' }],
          houseNum: [{ required: true, message: '请输入房屋数量', trigger: 'blur' }],
          communityImage: [{ required: true, message: '请至少上传一张小区图片', trigger: 'change' }],
          adminName: [{ required: true, message: '请输入小区管理员', trigger: 'blur' }],
          adminPhone: [
            { required: true, message: '请输入管理员联系电话', trigger: 'blur' },
            { validator: validateToNextPhone }
          ],
          adminPassword: [{ required: true, message: '请输入管理员登录密码', trigger: 'blur' }],
          adminAvatar: [{ required: true, message: '请上传管理员头像', trigger: 'blur' }]
        },
        url: {
          add: "/property-company/community/companyCommunity/add",
          edit: "/property-company/community/companyCommunity/edit",
          queryById: "/property-company/community/companyCommunity/queryById"
        },
        dictOptions: [
          {label: '商用', value: 1},
          {label: '普通住宅', value: 2},
          {label: '商住合一', value: 3}
        ]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      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>