<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="900px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="活动标题" prop="activityTitle">
            <el-input v-model="form.activityTitle" placeholder="请输入活动标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动编码" prop="activityCode">
            <el-input v-model="form.activityCode" placeholder="请输入活动编码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="展示图片" prop="activityBanner">
        <image-upload2 v-model="form.activityBanner" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker clearable
                            v-model="form.startTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker clearable
                            v-model="form.endTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="城市" prop="cityCode">
            <el-cascader v-model="form.cityCode" :props="{'value':'id'}" :options="areaOption" filterable style="width: 95%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入详细地址" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="女生上限" prop="womenMaxNum">
            <el-input-number :min="0" v-model="form.womenMaxNum" placeholder="请输入女生人数上限"  style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="男生上限" prop="manMaxNum">
            <el-input-number :min="0" v-model="form.manMaxNum" placeholder="请输入男生人数上限"  style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="费用说明" prop="priceText">
          <el-input v-model="form.priceText" placeholder="请输入费用说明" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="女生价格" prop="joinWomenPrice">
            <el-input-number :min="0" v-model="form.joinWomenPrice" placeholder="请输入女生参与价格" style="width: 100%"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="男生价格" prop="joinManPrice">
            <el-input-number :min="0" v-model="form.joinManPrice" placeholder="请输入男生参与价格"  style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="强制会员" prop="mustVip">
            <el-radio-group v-model="form.mustVip" size="mini">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="强制实名用" prop="mustAuth">
            <el-radio-group v-model="form.mustAuth" size="mini">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="详细说明" prop="activityText">
        <el-input  v-model="form.activityText" type="textarea" :autosize="{ minRows: 2, maxRows: 8}" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { enableStatusList } from '@/constant/statusMap'
import { getTreeAreaCode } from '@/api/xq/areaCode'
import { addActivityShop, getActivityShop, updateActivityShop } from '@/api/xq/activityShop'
import { addBanner, getBanner } from '@/api/xq/banner'

export default {
  components: {
  },
  data () {
    return {
      enableStatusList,
      open: false,
      title: '新增相亲会',
      form:{
        id: undefined,
        activityCode: undefined,
        activityTitle: undefined,
        activityBanner: undefined,
        startTime: undefined,
        endTime: undefined,
        cityName: undefined,
        cityCode: undefined,
        address: undefined,
        activityText: undefined,
        womenMaxNum: undefined,
        manMaxNum: undefined,
        joinWomenNum: undefined,
        joinManNum: undefined,
        handManNum: undefined,
        handWomenNum: undefined,
        joinWomenPrice: undefined,
        joinManPrice: undefined,
        mustVip: undefined,
        mustAuth: undefined,
        priceText: undefined,
        createTime: undefined,
        updateTime: undefined
      },
      // 表单校验
      rules: {
        activityCode: [
          { required: true, message: "活动编码不能为空", trigger: "blur" }
        ],
        activityTitle: [
          { required: true, message: "活动标题不能为空", trigger: "blur" }
        ],
        activityBanner: [
          { required: true, message: "活动展示图片不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        cityName: [
          { required: true, message: "城市名称不能为空", trigger: "blur" }
        ],
        cityCode: [
          { required: true, message: "城市编码不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        activityText: [
          { required: true, message: "活动详细说明不能为空", trigger: "blur" }
        ],
        womenMaxNum: [
          { required: true, message: "女生人数上限不能为空", trigger: "blur" }
        ],
        manMaxNum: [
          { required: true, message: "男生人数上限不能为空", trigger: "blur" }
        ],
        joinWomenNum: [
          { required: true, message: "报名参加女生人数不能为空", trigger: "blur" }
        ],
        joinManNum: [
          { required: true, message: "报名参加男生人数不能为空", trigger: "blur" }
        ],
        handManNum: [
          { required: true, message: "手动调整男生人数不能为空", trigger: "blur" }
        ],
        handWomenNum: [
          { required: true, message: "手动调整女生人数不能为空", trigger: "blur" }
        ],
        joinWomenPrice: [
          { required: true, message: "女生参与价格不能为空", trigger: "blur" }
        ],
        joinManPrice: [
          { required: true, message: "男生参与价格不能为空", trigger: "blur" }
        ],
        mustVip: [
          { required: true, message: "必须强制会员参与不能为空", trigger: "blur" }
        ],
        mustAuth: [
          { required: true, message: "必须强制实名用户参与不能为空", trigger: "blur" }
        ],
        priceText: [
          { required: true, message: "费用说明不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
      areaOption: [],
    }
  },
  created() {
    this.options();
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "相亲会";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getActivityShop(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    options(){
      getTreeAreaCode().then(response => {
        response.data.forEach(city => {
          let cityList = city.children;
          cityList.forEach(no => {
            no.children = undefined
          })
        })
        this.areaOption = response.data
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          addActivityShop({
            ...this.form,
            cityCode: this.getCode(this.form.cityCode),
          }).then(data => {
            this.$modal.msgSuccess("新增成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }else{
          updateActivityShop({
            ...this.form,
            cityCode: this.getCode(this.form.cityCode),
          }).then(data => {
            this.$modal.msgSuccess("修改成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
    getCode(code){
      if(!code){
        return code;
      }
      if(Array.isArray(code)){
        return code[code.length-1]
      }
      return code;
    }
  }
}
</script>
