<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="chargeTypeId">
              <a-select style="width: 100%" v-model="model.chargeTypeId" placeholder="请选择收费类型">
                <a-select-option v-for="item in chargeList" :key="item.id" :value="item.id">{{item.typeName}}</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="chargeItem">
              <a-input v-model="model.chargeItem" placeholder="请输入收费项目"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付费周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargeCycle">
              <a-select style="width: 100%" v-model="model.chargeCycle" 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-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="计算公式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="designFormulas">
              <a-select style="width: 100%" v-model="model.designFormulas" placeholder="请选择计算公式">
                <a-select-option value="1">固定金额</a-select-option>
                <a-select-option value="2">建筑面积 × 单价</a-select-option>
                <a-select-option value="3">（本期度数 - 上期度数） × 单价</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="model.designFormulas === '1' ? '固定金额' : '单价'" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input type="number" v-model="model.amount" :placeholder="`请输入${model.designFormulas === '1' ? '固定金额' : '单价'}`"></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="remark">
              <a-textarea
                v-model="model.remark"
                placeholder="请输入"
                :auto-size="{ minRows: 3, maxRows: 5 }"
              />
            </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, getChargeListApi} from '@/api/api'

  export default {
    name: 'PropertyChargruleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        chargeList: [],
        dictOptions: [],
        model:{
          chargeTypeId: '',
          chargeItem: '',
          chargeCycle: '1',
          designFormulas: '1',
          amount: '',
          remark: ''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          chargeTypeId: [{ required: true, message: '请选择收费类型', trigger: 'change' }],
          chargeItem: [{ required: true, message: '请输入收费项目', trigger: 'blur' }],
          amount: [{ required: true, message: '请输入金额', trigger: 'blur' }]
        },
        url: {
          add: "/property-community/system/companyChargerule/add",
          edit: "/property-community/system/companyChargerule/edit",
          queryById: "/property-community/system/companyChargerule/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictData()
      this.getChargeList()
    },
    methods: {
      initDictData() {
        //优先从缓存中读取字典配置
        if(getDictItemsFromCache('chargingCycle')){
          this.dictOptions = getDictItemsFromCache('chargingCycle');
          return
        }
        // //根据字典Code, 初始化字典数组
        ajaxGetDictItems('chargingCycle', null).then((res) => {
          if (res.success) {
            this.dictOptions = res.result;
          }
        })
      },
      async getChargeList() {
        let {result} = await getChargeListApi()
        this.chargeList = result
      },
      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>