<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="500px" 
    append-to-body 
    :close-on-click-modal="false"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-row>
        <form-col :span="span" label="运营区" prop="areaId">
          <area-remote-select v-model="form.areaId" :init-options="initAreaOptions" style="width: 100%;" v-if="!loading"/>
        </form-col>
        <form-col :span="span" label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.area_join_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="span" label="绑定用户" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入绑定用户手机号" />
        </form-col>
        <form-col :span="span" label="分成比例" prop="point">
          <el-input v-model="form.point" placeholder="请输入分成比例">
            <template slot="append">%</template>
          </el-input>
        </form-col>
        <form-col :span="span" label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" maxlength="30" show-word-limit/>
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getAreaJoin, addAreaJoin, updateAreaJoin } from "@/api/bst/areaJoin";
import FormCol from "@/components/FormCol/index.vue";
import AreaRemoteSelect from "@/components/Business/Area/AreaRemoteSelect.vue";
import { AreaJoinType } from "@/utils/enums";
import UserInput from '@/components/Business/User/UserInput.vue';

export default {
  name: 'AreaJoinEditDialog',
  components: { FormCol, AreaRemoteSelect, UserInput },
  dicts: ['area_join_type'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      AreaJoinType,
      span: 24,
      title: '',
      form: {},
      loading: false,
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        areaId: [
          { required: true, message: "运营区不能为空", trigger: "change" }
        ],
        point: [
          { required: true, message: "分成比例不能为空", trigger: "blur" }
        ],
        userPhone: [
          { required: true, message: "绑定用户手机号不能为空", trigger: "blur" }
        ]
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    },
    initAreaOptions() {
      return [{id: this.form.areaId, name: this.form.areaName}]
    },
  },
  methods: {
    handleOpen() {
      if (this.id == null) {
        this.reset();
        this.title = "添加运营加盟";
      } else {
        this.getDetail();
        this.title = "修改运营加盟";
      }
    },
    reset() {
      this.form = {
        id: null,
        type: AreaJoinType.JOIN,
        areaId: null,
        userId: null,
        point: null,
        createTime: null,
        remark: null,
        createId: null,
        ...this.initData
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
    },
    getDetail() {
      this.loading = true;
      getAreaJoin(this.id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const promise = this.form.id != null ? updateAreaJoin(this.form) : addAreaJoin(this.form);
          promise.then(response => {
            this.$modal.msgSuccess(this.form.id != null ? "修改成功" : "新增成功");
            this.dialogVisible = false;
            this.$emit('success');
          });
        }
      });
    },
    cancel() {
      this.dialogVisible = false;
      this.reset();
    }
  }
}
</script>

<style lang="scss" scoped>

</style> 