<template>
  <div>
    <setActCode @sumbit="onSubmit">
      <template #typeCode>
        <el-form-item label="活码类型" :rules="[{ required: true, message: '请选择活码类型', trigger: 'blur' }]">
          <el-radio-group v-model="form.hmType">
            <el-radio :label="1">单人活码</el-radio>
          </el-radio-group>
        </el-form-item>
      </template>
      <template #member>
        <el-form-item label="使用成员" :rules="[{ required: true, message: '请选择使用成员', trigger: 'blur' }]">
          <el-radio-group v-model="form.memberType" @change="radioChange">
            <el-radio :label="1">全部成员</el-radio>
            <el-radio :label="2">部分成员</el-radio>
          </el-radio-group>
          <br />
          <p v-if="form.memberType == 1" class="tips">当前已选择 {{ hmMemberNum }} 个成员 <span>(活码名称默认为使用成员名称)</span></p>
          <select-clerk v-if="form.memberType == 2" :multiple="true" :data="data" @change="onChange"></select-clerk>
        </el-form-item>
      </template>
    </setActCode>
  </div>
</template>

<script>
import setActCode from '@/views/salesleads/components/actCodeSet.vue';
import SelectClerk from '../../components/select-clerk/index.vue';
import { postJsonRequest, getRequest } from '@/api/api';
export default {
  components: { setActCode, SelectClerk },
  data() {
    return {
      hmMemberNum: 1,
      form: {
        hmType: 1,
        // name: '',
        clerkIdList: [],
        memberType: 1,
        hmUserNum: null
      },
      data: [],
      storeClerkList: []
    };
  },
  created() {
    this.getClerkNum();
  },
  methods: {
    radioChange(e) {
      if (e == 1) {
        this.form.clerkIdList = ['-1'];
        this.data = [];
        this.form.hmUserNum = this.hmMemberNum;
      }
    },
    getClerkNum() {
      getRequest('/haoban-manage3-web/hm/qrcode/getAddCount', {}).then(res => {
        if (res.data.code == 0) {
          this.hmMemberNum = res.data.result || 1;
        }
      });
    },
    onChange(data) {
      this.data = data;
      this.form.clerkIdList = data;
      this.form.hmUserNum = data.length;
    },
    async onSubmit(event) {
      let obj = {
        ...event,
        ...this.form
      };
      await postJsonRequest('/haoban-manage3-web/hm/qrcode/addList', obj).then(res => {
        if (res.data.code == 0) {
          this.form = {
            hmType: 1,
            clerkIdList: [],
            hmUserNum: null //多人活码人数
          };
          this.$router.go(-1);
        } else {
          return this.$message.error(res.data.message);
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.ylbtn {
  margin-top: 10px;
  border: 1px solid #2f54eb;
  color: #2f54eb;
}
.damolish .el-form-item__content {
  line-height: 30px;
}
.tips {
  color: #303133;
  span {
    color: #909399;
  }
}
</style>
