<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="24">
            <a-form-model-item label="收费名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargingName">
              <a-input v-model="model.chargingName" placeholder="请输入收费名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="收费方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargingCycle">
              <a-select style="width: 100%" v-model="model.chargingCycle" 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="24" v-if="model.chargingCycle !== '4' && model.chargingCycle !== '5'">
            <a-form-model-item label="授权天数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="empowerDays">
              <a-input :disabled="model.chargingCycle !== '4' && model.chargingCycle !== '5'" v-model="model.empowerDays" @blur="ruleNumberInput" placeholder="请输入授权天数">
                <div slot="addonAfter">天</div>
              </a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="授权小区数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="empowerNum">
              <a-input-number v-model="model.empowerNum" :min="1" :precision="0" placeholder="请输入授权小区数量" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="收费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargingMoney">
              <a-input-number v-model="model.chargingMoney" :min="0.01" :precision="2" placeholder="请输入收费金额" style="width: 100%" />
            </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:{
          chargingName: '',
          chargingMoney: '',
          chargingCycle: '',
          // empowerNum: '',
          empowerDays: ''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          chargingName: [{ required: true, message: '请输入收费名称', trigger: 'blur' }],
          chargingCycle: [{ required: true, message: '请选择收费方式', trigger: 'change' }],
          // empowerDays: [{ required: true, message: '请输入授权日期', trigger: 'blur' }],
          // empowerNum: [{ required: true, message: '请输入授权小区数量', trigger: 'blur' }],
          chargingMoney: [{ required: true, message: '请输入收费金额', trigger: 'blur' }]
        },
        url: {
          add: "/property-central/property/propertyChargrule/add",
          edit: "/property-central/property/propertyChargrule/edit",
          queryById: "/property-central/property/propertyChargrule/queryById"
        },
        dictOptions: []
      }
    },
    watch: {
      'model.chargingCycle': {
        handler(newV, oldV) {
          switch(newV) {
            case '1':
              this.model.empowerDays = '30'
              break
            case '2':
              this.model.empowerDays = '90'
              break
            case '3':
              this.model.empowerDays = '365'
              break
            default:
              if(oldV) {
                this.model.empowerDays = ''
              }
          }
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictData()
    },
    methods: {
      initDictData() {
        //优先从缓存中读取字典配置
        if(getDictItemsFromCache('chargingCycle')){
          this.dictOptions = getDictItemsFromCache('chargingCycle');
          return
        }
        // //根据字典Code, 初始化字典数组
        ajaxGetDictItems('chargingCycle', 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>