<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="advName">
              <a-input v-model="model.advName" placeholder="请输入广告名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="广告位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advPosition">
              <j-dict-select-tag v-model="model.advPosition" dictCode="advPosition" placeholder="请选择广告位置" disabled />
              <!-- <a-select v-model="model.advPosition" style="width: 100%" placeholder="请选择广告位置" disabled>
                <a-select-option value="home">业主小程序banner图</a-select-option>
              </a-select> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="广告有效时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advDate">
              <a-range-picker valueFormat="YYYY-MM-DD" :placeholder="['广告有效开始时间', '请选择广告有效结束时间']" v-model="model.advDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="广告有效结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advEndDate">
              <j-date placeholder="请选择广告有效结束时间" v-model="model.advEndDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="广告链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advUrl">
              <a-input v-model="model.advUrl" placeholder="请输入广告链接"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
              <!-- <a-form-model-item label="banner图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advImageUrl">
                <j-upload v-model="model.advImageUrl"></j-upload>
              </a-form-model-item> -->

              <a-form-model-item label="banner图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advImageUrl">
                <j-image-upload bizPath="scott/pic" accept="image/png, image/jpeg" v-model="model.advImageUrl"></j-image-upload>
              </a-form-model-item>
            </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否定时发布" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isScheduledRelease">
              <!-- <j-dict-select-tag type="radio" v-model="model.isScheduledRelease" dictCode="release" placeholder="请选择是否定时发布" /> -->
              <a-radio-group name="radioGroup" v-model="model.isScheduledRelease">
                <a-radio :value="1">是</a-radio>
                <a-radio :value="0">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.isScheduledRelease === 1">
            <a-form-model-item label="发布日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releaseTime">
              <j-date placeholder="请选择发布日期" v-model="model.releaseTime"  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'

  export default {
    name: 'PropertyAdvForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          advName: '',
          advPosition: 'home',
          advDate: [],
          advUrl: '',
          advImageUrl: '',
          isScheduledRelease: 0,
          releaseTime: ''
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          advName: [{ required: true, message: '请输入广告名称', trigger: 'blur' }],
          advImageUrl: [{ required: true, message: '请上传banner图', trigger: 'change' }],
          releaseTime: [{ required: true, message: '请选择发布日期', trigger: 'change' }]
        },
        url: {
          add: "/property-community/property/propertyAdv/add",
          edit: "/property-community/property/propertyAdv/edit",
          queryById: "/property-community/property/propertyAdv/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record, {advDate: [record.advStartDate, record.advEndDate]});
        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';
            }
            this.model['advStartDate'] = this.model['advDate'][0]
            this.model['advEndDate'] = this.model['advDate'][1]
            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>