<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @open="onOpen">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-form-item label="店铺" prop="storeId">
        <store-input v-model="form.storeId" :disabled="hasView([views.store, views.mchStore])"/>
      </el-form-item>
      <el-form-item label="用户" prop="userId" v-if="userInputType === 'dialog'">
        <user-input v-model="form.userId"/>
      </el-form-item>
      <el-form-item label="用户" prop="userId" v-else-if="userInputType === 'search'">
        <user-search-input v-if="open && !loading" v-model="form.userId" :init-options="initUserSearchOptions"/>
      </el-form-item>
      <el-form-item label="备注名" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注名" maxlength="200" show-word-limit/>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
          <el-option
            v-for="dict in dict.type.store_staff_role"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分成比例" prop="point">
        <el-input-number
          v-model="form.point"
          placeholder="请输入分成比例"
          :min="0"
          :max="100"
          style="width: calc(100% - 2em)"
          controls-position="right"
        /> %
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-switch v-model="form.enabled" active-text="启用" inactive-text="禁用"/>
      </el-form-item>
      <el-form-item label="权限列表" prop="permissions">
        <el-checkbox-group v-model="form.permissions">
          <el-checkbox
            v-for="dict in dict.type.store_staff_permissions"
            :key="dict.value"
            :label="dict.value">
            {{dict.label}}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </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 { StoreStaffRole, UserType } from '@/utils/constants'
import { addStoreStaff, getStoreStaff, updateStoreStaff } from '@/api/ss/storeStaff'
import StoreInput from '@/components/Business/Store/StoreInput.vue'
import UserInput from '@/components/Business/SmUser/UserInput.vue'
import { $view } from '@/utils/mixins'
import { mapGetters } from 'vuex'
import UserSearchInput from '@/components/Business/SmUser/UserSearchInput.vue'

export default {
  name: "StoreStaffEditDialog",
  mixins: [$view],
  dicts: ['store_staff_permissions', 'store_staff_role'],
  components: { UserSearchInput, UserInput, StoreInput },
  props: {
    // ID
    employId: {
      type: String,
      default: null,
    },
    show: {
      type: Boolean,
      default: false
    },
    initForm: {
      type: Object,
      default: () => ({})
    },
    // 用户的输入方式:dialog 弹窗选择，search 搜索选择
    userInputType: {
      type: String,
      default: "dialog"
    },
    // 更新API
    updateApi: {
      type: Function,
      default: updateStoreStaff
    },
    // 新增API
    addApi: {
      type: Function,
      default: addStoreStaff
    },
    // 加载API
    loadApi: {
      type: Function,
      default: getStoreStaff
    }
  },
  data() {
    return {
      // 表单参数
      form: {},
      title: null,
      loading: false,
      // 表单校验
      rules: {
        storeId: [
          { required: true, message: "店铺不能为空", trigger: "change" }
        ],
        userId: [
          { required: true, message: "用户不能为空", trigger: "change" }
        ],
        point: [
          { required: true, message: "分成比例不能为空", trigger: "change" }
        ],
        enabled: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
      }
    }
  },
  computed: {
    initUserSearchOptions() {
      return [{value: this.form.userId, label: this.form.userName}]
    },
    UserType() {
      return UserType
    },
    ...mapGetters(['userType']),
    open: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  created() {
    this.reset();
  },
  methods: {
    onOpen() {
      if (this.employId != null) {
        this.loading = true;
        this.loadApi(this.employId).then(res => {
          this.form = res.data;
        }).finally(() => {
          this.loading = false;
        })
      } else {
        this.reset();
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        employId: null,
        storeId: null,
        userId: null,
        remark: null,
        role: StoreStaffRole.STAFF,
        point: 0,
        enabled: true,
        permissions: [],
        ...this.initForm
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.employId != null) {
            this.updateApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('success')
            });
          } else {
            this.addApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('success')
            });
          }
        }
      });
    },
  }
}
</script>
