<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    title="修改佣金"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="一级线索佣金比例:">
        <el-input
          clearable
          v-model="form.cluesLevel1"
          type="number"
          placeholder="请输入一级线索佣金"
        />
      </el-form-item>
      <el-form-item label="二级线索佣金比例:">
        <el-input
          clearable
          v-model="form.cluesLevel2"
          type="number"
          placeholder="请输入二级线索佣金"
        />
      </el-form-item>
      <el-form-item label="三级线索佣金比例:">
        <el-input
          clearable
          v-model="form.cluesLevel3"
          type="number"
          placeholder="请输入三级线索佣金"
        />
      </el-form-item>
      <el-form-item label="四级线索佣金比例:">
        <el-input
          clearable
          v-model="form.cluesLevel4"
          type="number"
          placeholder="请输入四级线索佣金"
        />
      </el-form-item>
      <el-form-item label="一级订单佣金比例:">
        <el-input
          clearable
          v-model="form.orderLevel1"
          type="number"
          placeholder="请输入一订单索佣金"
        />
      </el-form-item>
      <el-form-item label="二级订单佣金比例:">
        <el-input
          clearable
          v-model="form.orderLevel2"
          type="number"
          placeholder="请输入二订单索佣金"
        />
      </el-form-item>
      <el-form-item label="三级订单佣金比例:">
        <el-input
          clearable
          v-model="form.orderLevel3"
          type="number"
          placeholder="请输入三级订单佣金"
        />
      </el-form-item>
      <el-form-item label="四级订单佣金比例:">
        <el-input
          clearable
          v-model="form.orderLevel4"
          type="number"
          placeholder="请输入四级订单佣金"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import {
    getOrganizationsCmmssn,
    updateOrganizationCmmssn
  } from '@/api/system/cmmssn';

  export default {
    components: {},
    props: {
      // 弹窗是否打开
      visible: Boolean,
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        teamId: '',
        cluesLevel1: '',
        cluesLevel2: '',
        cluesLevel3: '',
        cluesLevel4: '',
        orderLevel1: '',
        orderLevel2: '',
        orderLevel3: '',
        orderLevel4: '',
        showLevel: 3
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          title: [
            {
              required: true,
              message: '请输入名称',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      query() {
        if (!this.form.teamId) {
          return;
        }
        this.loading = true;
        getOrganizationsCmmssn({ teamId: this.form.teamId })
          .then((data) => {
            this.loading = false;
            this.$util.assignObject(this.form, { ...data });
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          updateOrganizationCmmssn(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              // this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },
      /* 判断是否是目录 */
      isDirectory(d) {
        return !!d.children?.length && !d.component;
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.form.teamId = this.data.id;
            this.query();
          }
        }
      }
    }
  };
</script>
