<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="title">
              <a-input v-model="model.title" placeholder="请输入中文-活动标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="英文-活动标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enTitle">
              <a-input v-model="model.enTitle" placeholder="请输入英文-活动标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="no_type" placeholder="请选择活动类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="image">
              <j-image-upload isMultiple  v-model="model.image" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择活动时间"  v-model="model.startTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="中文-活动地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入中文-活动地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="英文-活动地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enAddress">
              <a-input v-model="model.enAddress" placeholder="请输入英文-活动地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sum">
              <a-input-number v-model="model.sum" placeholder="请输入活动人数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
              <a-input-number v-model="model.num" placeholder="请输入报名人数" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" placeholder="请输入报名费用" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="主理人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adminUser">
              <j-search-select-tag v-model="model.adminUser" dict="han_hai_member,nick_name,id"  />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="签到人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openNum">-->
<!--              <a-input-number v-model="model.openNum" placeholder="请输入签到人数" style="width: 100%"  />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="签到人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="doNum">
              <a-input-number v-model="model.doNum" placeholder="请输入签到人数" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否上架" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isOpen">
              <j-switch v-model="model.isOpen"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-dict-select-tag type="list" v-model="model.state" dictCode="no_state" placeholder="请选择活动状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="details">
              <j-editor v-model="model.details" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="英文-活动详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enDetails">
              <j-editor v-model="model.enDetails" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="中文-注意事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="precautions">
              <j-editor v-model="model.precautions" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="英文-注意事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enPrecautions">
              <j-editor v-model="model.enPrecautions" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="早鸟票" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birdPrice">
              <a-input-number v-model="model.birdPrice" placeholder="请输入早鸟票" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单人票" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="personPrice">
              <a-input-number v-model="model.personPrice" placeholder="请输入单人票" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="尊享票" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expensivePrice">
              <a-input-number v-model="model.expensivePrice" placeholder="请输入尊享票" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input v-model="model.longitude" placeholder="请输入经度" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input v-model="model.latitude" placeholder="请输入纬度" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地图选择位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <TencentMapPicker
                :latitude="model.latitude"
                :longitude="model.longitude"
                @onLocationSelected="handleLocationSelected"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="活动需知" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderDetails">
              <j-editor v-model="model.orderDetails" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
              <j-search-select-tag v-model="model.cityId" dict="popularize_city,city,id"  />
            </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 TencentMapPicker from './TencentMapPicker.vue';

  export default {
    name: 'PopularizeActivityForm',
    components: {
      TencentMapPicker
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           adminUser: [
              { required: true, message: '请输入主理人!'},
           ],
        },
        url: {
          add: "/popularizeActivity/popularizeActivity/add",
          edit: "/popularizeActivity/popularizeActivity/edit",
          queryById: "/popularizeActivity/popularizeActivity/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      handleLocationSelected({latitude, longitude}) {
        console.log('event11',latitude, longitude)
        this.model.latitude = latitude;
        this.model.longitude = longitude;
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (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>