<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="complaintTheme">
              <a-input v-model="model.complaintTheme" readonly placeholder="请输入投诉主题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="投诉人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userName">
              <a-input v-model="model.userName" placeholder="请输入投诉人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userPhone">
              <a-input v-model="model.userPhone" placeholder="请输入联系方式"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="住户地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userAddress">
              <a-input v-model="model.userAddress" placeholder="请输入住户地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="投诉时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="complaintTime">
              <a-input v-model="model.complaintTime" 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="problemDesc">
              <a-textarea placeholder="请输入问题描述" v-model="model.problemDesc" :auto-size="{ minRows: 3, maxRows: 5 }" allow-clear />
            </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="handleResult">
              <a-textarea placeholder="请输入处理结果" v-model="model.handleResult" :auto-size="{ minRows: 3, maxRows: 5 }" allow-clear />
            </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="handleStatus">
              <a-select style="width: 100%" v-model="model.handleStatus" 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-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 {
        dictOptions: [],
        model:{
          complaintTheme: '',
          userName: '',
          userPhone: '',
          userAddress: '',
          complaintTime: '',
          problemDesc: '',
          handleResult: '',
          handleStatus: ''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          complaintTheme: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
          employeePhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
          employeeDuties: [{ required: true, message: '请选择员工职务', trigger: 'change' }]
        },
        url: {
          add: "/property-community/employee/companyEmployee/add",
          edit: "/property-community/property/communityComplaint/edit",
          queryById: "/property-community/property/communityComplaint/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictData()
    },
    methods: {
      initDictData() {
        //优先从缓存中读取字典配置
        if(getDictItemsFromCache('complaint_status')){
          this.dictOptions = getDictItemsFromCache('complaint_status');
          return
        }
        // //根据字典Code, 初始化字典数组
        ajaxGetDictItems('complaint_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>