<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="activityId">
        <el-select
          v-model="formData.activityId"
          size="mini"
          placeholder="请选择关联活动"
          filterable
          @change="(e) => (formData.activityName = activityList.find((i) => i.currencyId === e).currencyName)"
        >
          <el-option v-for="(item, index) in activityList" :key="item.currencyId" :label="`${item.currencyName}（${item.currencyId}）`" :value="item.currencyId" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动类型" prop="activityType">
        <el-select v-model="formData.activityType" size="mini" placeholder="请选择活动类型">
          <el-option label="消费金" :value="5" />
          <el-option label="组合活动" :value="6" />
        </el-select>
      </el-form-item>
      <div v-show="[0, 1].includes(type)">
        <el-form-item label="团长规则" prop="leadersRule">
          <el-select v-model="formData.leadersRule" size="mini" placeholder="请选择团长规则">
            <el-option label="比例" :value="1" />
            <el-option label="金额" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.leadersRule" label="直接团长规则" prop="leadersMoney">
          <el-input v-model="formData.leadersMoney" placeholder="请输入直接团长规则" style="width: 250px">
            <template #append>
              <div v-if="formData.leadersRule === 1">%</div>
              <div v-else-if="formData.leadersRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item v-if="formData.leadersRule" label="间接团长规则" prop="leadersInMoney">
          <el-input v-model="formData.leadersInMoney" placeholder="请输入间接团长规则" style="width: 250px">
            <template #append>
              <div v-if="formData.leadersRule === 1">%</div>
              <div v-else-if="formData.leadersRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="合伙人规则" prop="partnerRule">
          <el-select v-model="formData.partnerRule" size="mini" placeholder="请选择合伙人规则">
            <el-option label="比例" :value="1" />
            <el-option label="金额" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.partnerRule" label="直接合伙人规则" prop="partnerMoney">
          <el-input v-model="formData.partnerMoney" placeholder="请输入直接合伙人规则" style="width: 250px">
            <template #append>
              <div v-if="formData.partnerRule === 1">%</div>
              <div v-else-if="formData.partnerRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item v-if="formData.partnerRule" label="间接合伙人规则" prop="partnerInMoney">
          <el-input v-model="formData.partnerInMoney" placeholder="请输入间接合伙人规则" style="width: 250px">
            <template #append>
              <div v-if="formData.partnerRule === 1">%</div>
              <div v-else-if="formData.partnerRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
      </div>
      <div v-show="[0, 2].includes(type)">
        <el-form-item label="加盟商规则" prop="franchiseeRule">
          <el-select v-model="formData.franchiseeRule" size="mini" placeholder="请选择加盟商规则">
            <el-option label="比例" :value="1" />
            <el-option label="金额" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.franchiseeRule" label="加盟商佣金规则" prop="franchiseeMoney">
          <el-input v-model="formData.franchiseeMoney" placeholder="请输入加盟商佣金规则" style="width: 250px">
            <template #append>
              <div v-if="formData.franchiseeRule === 1">%</div>
              <div v-else-if="formData.franchiseeRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="代理商规则" prop="agentRule">
          <el-select v-model="formData.agentRule" size="mini" placeholder="请选择代理商规则">
            <el-option label="比例" :value="1" />
            <el-option label="金额" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.agentRule" label="代理商佣金规则" prop="agentMoney">
          <el-input v-model="formData.agentMoney" placeholder="请输入代理商佣金规则" style="width: 250px">
            <template #append>
              <div v-if="formData.agentRule === 1">%</div>
              <div v-else-if="formData.agentRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="社区店规则" prop="communityRule">
          <el-select v-model="formData.communityRule" size="mini" placeholder="请选择社区店规则">
            <el-option label="比例" :value="1" />
            <el-option label="金额" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.communityRule" label="社区店佣金规则" prop="communityMoney">
          <el-input v-model="formData.communityMoney" placeholder="请输入社区店佣金规则" style="width: 250px">
            <template #append>
              <div v-if="formData.communityRule === 1">%</div>
              <div v-else-if="formData.communityRule === 2">元</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="分佣来源" prop="sourceType">
          <el-select v-model="formData.sourceType" size="mini" placeholder="请选择分佣来源">
            <el-option label="订单金额" :value="1" />
            <el-option label="赠送代金券" :value="2" />
            <el-option label="消费金" :value="3" />
          </el-select>
        </el-form-item>
      </div>
    </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 { getAllPlatformBeeCurrency, getByIdPlatformBeeRule, savePlatformBeeRule, priceControlUpdatePlan } from '@/api/commissionAllocation/gradeCommission'

export default {
  name: 'EditModal',
  components: {},
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      type: 0,
      visible: false,
      activityList: [],
      formData: {
        ruleId: '',
        activityId: '',
        activityType: '',
        leadersRule: '',
        leadersMoney: '',
        leadersInMoney: '',
        partnerRule: '',
        partnerMoney: '',
        partnerInMoney: '',
        franchiseeRule: '',
        franchiseeMoney: '',
        communityRule: '',
        communityMoney: '',
        sourceType: '',
        agentRule: '',
        agentMoney: ''
      },
      formRules: {
        activityId: [{ required: true, message: '缺少活动' }],
        activityType: [{ required: true, message: '请选择活动' }],
        leadersRule: [{ required: true, message: '请选择团长规则' }],
        leadersMoney: [
          { required: false, message: '请输入直接团长规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' } // >0的数字, 最多2位小数正则
        ],
        leadersInMoney: [
          { required: false, message: '请输入间接团长规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        partnerRule: [{ required: true, message: '请选择合伙人规则' }],
        partnerMoney: [
          { required: false, message: '请输入直接合伙人规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        partnerInMoney: [
          { required: false, message: '请输入间接合伙人规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        franchiseeMoney: [
          { required: false, message: '请输入间接合伙人规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        communityMoney: [
          { required: false, message: '请输入间接合伙人规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        agentMoney: [
          { required: false, message: '请输入间接合伙人规则' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        sourceType: [{ required: true, message: '请选择分佣来源' }]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    async initList() {
      const res = await getAllPlatformBeeCurrency({})
      this.activityList = res.data
    },
    handleOpen(params = {}, type) {
      this.type = type
      this.modalOptions.title = params.ruleId ? '编辑平台消费金活动分佣规则' : '添加平台消费金活动分佣规则'
      this.formData = Object.assign(this.$options.data().formData, params, {
        ruleId: params.ruleId || '',
        activityId: params.activityId || '',
        activityName: params.activityName || '',
        activityType: params.activityType || '',
        leadersRule: params.leadersRule || '',
        leadersMoney: params.leadersRule === 1 && typeof params.leadersMoney === 'number' ? params.leadersMoney * 100 : params.leadersMoney,
        leadersInMoney: params.leadersRule === 1 && typeof params.leadersInMoney === 'number' ? params.leadersInMoney * 100 : params.leadersInMoney,
        partnerRule: params.partnerRule || '',
        partnerMoney: params.partnerRule === 1 && typeof params.partnerMoney === 'number' ? params.partnerMoney * 100 : params.partnerMoney,
        partnerInMoney: params.partnerRule === 1 && typeof params.partnerInMoney === 'number' ? params.partnerInMoney * 100 : params.partnerInMoney,
        franchiseeRule: params.franchiseeRule || '',
        franchiseeMoney: params.franchiseeRule === 1 && typeof params.franchiseeMoney === 'number' ? params.franchiseeMoney * 100 : params.franchiseeMoney,
        agentRule: params.agentRule || '',
        agentMoney: params.agentRule === 1 && typeof params.agentMoney === 'number' ? params.agentMoney * 100 : params.agentMoney,
        communityRule: params.communityRule || '',
        communityMoney: params.communityRule === 1 && typeof params.communityMoney === 'number' ? params.communityMoney * 100 : params.communityMoney,
        sourceType: params.sourceType || ''
      })
      this.visible = true
      this.initList()
      if (params.ruleId) {
        this.getInfo(params.ruleId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getByIdPlatformBeeRule({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          ruleId: res.data.ruleId || '',
          activityId: res.data.activityId || '',
          activityName: res.data.activityName || '',
          activityType: res.data.activityType || '',
          leadersRule: res.data.leadersRule || '',
          leadersMoney: res.data.leadersRule === 1 && typeof res.data.leadersMoney === 'number' ? res.data.leadersMoney * 100 : res.data.leadersMoney,
          leadersInMoney: res.data.leadersRule === 1 && typeof res.data.leadersInMoney === 'number' ? res.data.leadersInMoney * 100 || '0.0' : res.data.leadersInMoney  || '0.0',
          partnerRule: res.data.partnerRule || '',
          partnerMoney: res.data.partnerRule === 1 && typeof res.data.partnerMoney === 'number' ? res.data.partnerMoney * 100  || '0.0' : res.data.partnerMoney  || '0.0',
          partnerInMoney: res.data.partnerRule === 1 && typeof res.data.partnerInMoney === 'number' ? res.data.partnerInMoney * 100 : res.data.partnerInMoney,
          franchiseeRule: res.data.franchiseeRule || '',
          franchiseeMoney: res.data.franchiseeRule === 1 && typeof res.data.franchiseeMoney === 'number' ? res.data.franchiseeMoney * 100  || '0.0' : res.data.franchiseeMoney  || '0.0',
          communityRule: res.data.communityRule || '',
          communityMoney: res.data.communityRule === 1 && typeof res.data.communityMoney === 'number' ? res.data.communityMoney * 100  || '0.0' : res.data.communityMoney  || '0.0',
          sourceType: res.data.sourceType || ''
        })
        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 { leadersRule, partnerRule, leadersMoney, leadersInMoney, partnerMoney, partnerInMoney, franchiseeRule, communityRule, franchiseeMoney, communityMoney, ...otps } = this.formData
            const params = {
              ...otps,
              leadersMoney: leadersRule === 1 ? leadersMoney / 100 : leadersMoney,
              leadersInMoney: leadersRule === 1 ? leadersInMoney / 100 : leadersInMoney,
              partnerMoney: partnerRule === 1 ? partnerMoney / 100 : partnerMoney,
              partnerInMoney: partnerRule === 1 ? partnerInMoney / 100 : partnerInMoney,
              franchiseeMoney: franchiseeRule === 1 ? franchiseeMoney / 100 : franchiseeMoney,
              communityMoney: communityRule === 1 ? communityMoney / 100 : communityMoney,
              leadersRule,
              partnerRule,
              franchiseeRule,
              communityRule
            }
            this.formData.ruleId ? await priceControlUpdatePlan(params) : await savePlatformBeeRule(params)
            loading.close()
            this.$message({ message: `${this.formData.ruleId ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>
