<!-- 分组编辑弹窗 -->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改分组' : '添加分组'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="上级分组:">
            <gro-select
              v-model="form.parentId"
              :data="groupsList"
              placeholder="请选择上级分组"
            />
          </el-form-item>
          <el-form-item label="分组名称:" prop="groupsName">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.groupsName"
              placeholder="请输入分组名称"
            />
          </el-form-item>
          <el-form-item label="分组id:" v-if="form.groupsId">
            <el-input v-model="form.groupsId" disabled />
          </el-form-item>
          <el-form-item label="排序号:">
            <el-input-number
              :min="0"
              :max="99999"
              v-model="form.sortNumber"
              controls-position="right"
              placeholder="请输入排序号"
              class="ele-fluid ele-text-left"
            />
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="用户来源:">
            <el-radio-group v-model="form.userFrom">
              <el-radio :label="0">机构</el-radio>
              <el-radio :label="1">团队</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="form.userFrom == 0 ? '是否门店:' : '是否团队:'">
            <el-radio-group v-model="form.ifBiz">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            v-if="form.userFrom == 0 && form.ifBiz == 1"
            label="原始门店:"
          >
            <ele-tree-select
              :data="organizationList"
              label-key="organizationName"
              value-key="organizationId"
              v-model="form.originalBizId"
              placeholder="请选择门店"
              size="small"
              :filterable="true"
              :multiple="false"
              :clearable="true"
              :disabled="false"
              :expand-on-click-node="true"
              :check-strictly="false"
              @change="onChange"
            />
          </el-form-item>
          <el-form-item
            label="统计类型:"
            v-if="!form.parentId"
            prop="statisticsType"
          >
            <el-select
              clearable
              v-model="form.statisticsType"
              placeholder="请选择统计类型"
              class="ele-fluid"
            >
              <el-option
                v-for="item in statisticsTypeList"
                :key="item.dictId"
                :value="item.dictId"
                :label="item.dictName"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </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 GroSelect from './gro-select.vue';
  import { addGroups, updateGroups } from '@/api/sylive/groups';
  import { infoOrganization } from '@/api/institution/organization';

  export default {
    components: { GroSelect },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      // 上级id
      parentId: Number,
      // 活动id
      activityId: Number,
      // 分组数据
      groupsList: Array,
      // 统计类型数据
      statisticsTypeList: Array,
      // 门店数据
      organizationList: Array
    },
    data() {
      const defaultForm = {
        groupsId: null,
        parentId: null,
        groupsName: '',
        userFrom: 0,
        ifBiz: 0,
        statisticsType: null,
        sortNumber: null,
        originalBizId: null
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          groupsName: [
            {
              required: true,
              message: '请输入分组名称',
              trigger: 'blur'
            }
          ],
          statisticsType: [
            {
              required: true,
              message: '请选择统计类型',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      onChange(value) {
        let organizationId = Number(value);
        if (organizationId) {
          infoOrganization(organizationId)
            .then((re) => {
              this.form.groupsName = re.organizationName;
            })
            .catch(() => {});
        }
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
            activityId: this.activityId || 0,
            parentId: this.form.parentId || 0
          };
          const saveOrUpdate = this.isUpdate ? updateGroups : addGroups;
          saveOrUpdate(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);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, this.data);
            this.isUpdate = true;
          } else {
            this.form.parentId = this.parentId;
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
