<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="关联平台组合活动" prop="composeId">
        <el-select
          v-model="formData.composeId" size="mini" placeholder="请选择关联平台组合活动" filterable
        >
          <el-option
            v-for="(item, index) in activityList" :key="item.composeId"
            :label="`${item.composeName}（${item.composeId}）`" :value="item.composeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动配置类型" prop="configType">
        <el-select v-model="formData.configType" clearable size="mini" placeholder="请选择活动类型">
          <el-option label="升级角色" :value="1" />
          <el-option label="分佣" :value="2" />
          <el-option label="社区服务" :value="3" />
          <el-option label="代金券" :value="4" />
          <el-option label="消费金" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否参与" prop="joinRole">
        <el-select v-model="formData.joinRole" clearable size="mini" placeholder="请选择是否参与">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="规则" prop="joinRule">
        <el-select v-model="formData.joinRule" clearable size="mini" placeholder="请选择规则">
          <el-option label="金额" :value="1" />
          <el-option label="比例" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="升级类型" prop="roleType">
        <el-select v-model="formData.roleType" clearable size="mini" placeholder="请选择升级类型">
          <el-option label="团长" :value="1" />
          <el-option label="合伙人" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="参与值" prop="joinMoney">
        <el-input v-model="formData.joinMoney" placeholder="请输入参与值" style="width: 250px;">
          <template #append>
            <div v-if="formData.joinRule === 1">元</div>
            <div v-else-if="formData.joinRule === 2">%</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="关联佣金配置" prop="ruleId">
        <el-select
          v-model="formData.ruleId" clearable size="mini" placeholder="请选择关联佣金配置" filterable
        >
          <el-option
            v-for="(item, index) in ruleList" :key="item.ruleId"
            :label="`${item.activityName || '--'}（${item.ruleId}）`" :value="item.ruleId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { savePlatformComposeConfig, updatePlatformComposeConfig, getPlatformComposeConfigById, getPlatformComposePage } from '@/api/active/combinationActivities'
import { getAllPlatformBeeRule } from '@/api/commissionAllocation/gradeCommission'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        configId: '',
        composeId: '',
        configType: '',
        joinRole: '',
        joinRule: '',
        roleType: '',
        joinMoney: '',
        ruleId: ''
      },
      formRules: {
        composeId: [
          { required: true, message: '请选择平台组合活动' }
        ],
        configType: [
          { required: true, message: '请选择活动配置类型' }
        ],
        joinMoney: [
          { required: false, message: '请输入参与值' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ]
      },
      activityList: [],
      ruleList: []
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    async initList() {
      const res1 = await getPlatformComposePage({ page: 1, pageSize: 9999 })
      this.activityList = res1.data.list
      const res2 = await getAllPlatformBeeRule({ page: 1, pageSize: 9999 })
      this.ruleList = res2.data.list
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.configId ? '编辑组合活动配置' : '添加组合活动配置'
      this.formData = Object.assign(this.$options.data().formData, params, {
        joinMoney: (params.joinRule === 2) && (typeof params.joinMoney === 'number') ? params.joinMoney * 100 : params.joinMoney
      })
      this.visible = true
      this.initList()
      if (params.configId) {
        this.getInfo(params.configId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getPlatformComposeConfigById({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          configId: res.data.configId || '',
          composeId: res.data.composeId || '',
          configType: res.data.configType || '',
          joinRole: res.data.joinRole || '',
          joinRule: res.data.joinRule || '',
          roleType: res.data.roleType || '',
          joinMoney: (res.data.joinRule === 2) && (typeof res.data.joinMoney === 'number') ? res.data.joinMoney * 100 : res.data.joinMoney,
          ruleId: res.data.ruleId || ''
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { joinRule, joinMoney, ...otps } = this.formData
            const params = {
              joinMoney: joinRule === 2 ? joinMoney / 100 : joinMoney,
              ...otps
            }
            this.formData.configId ? await updatePlatformComposeConfig(params) : await savePlatformComposeConfig(params)
            loading.close()
            this.$message({ message: `${this.formData.configId ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

