<template>
  <div>
    <setActCode @sumbit="onSubmit" ref="setActCodePage">
      <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="memberType" @change="radioChange">
            <el-radio :label="1">全部成员</el-radio>
            <el-radio :label="2">部分成员</el-radio>
          </el-radio-group>
          <br />
          <p v-if="memberType == 1" class="tips">当前已选择 {{ hmMemberNum }} 个成员 <span>(活码名称默认为使用成员名称)</span></p>
          <select-clerk v-if="memberType == 2" :multiple="true" :data="data" @change="onChange"></select-clerk>
        </el-form-item>
      </template>
    </setActCode>
  </div>
</template>

<script>
import setActCode from '@/views/salesleads/components/act-code-set.vue';
import SelectClerk from '../../components/select-clerk/index.vue';
import { postJsonRequest, getRequest } from '@/api/api';
export default {
  components: { setActCode, SelectClerk },
  data() {
    return {
      hmMemberNum: 0,
      form: {
        hmType: 1,
        // name: '',
        clerkIdList: [],
        hmUserNum: null
      },

      memberType: 1,
      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 || 0;
        }
      });
    },
    onChange(data) {
      this.data = data;
      this.form.clerkIdList = data.map(item => {
        return item.clerkId;
      });
      this.form.hmUserNum = data.length;
    },
    async onSubmit(event) {
      if (this.memberType == 1) {
        this.form.clerkIdList = ['-1'];
        this.form.hmUserNum = this.hmMemberNum;
      }
      let obj = {
        ...event,
        ...this.form
      };
      obj.passFlag = obj.passFlag ? 1 : 0;
      await postJsonRequest('/haoban-manage3-web/hm/qrcode/addList', obj).then(res => {
        if (res.data.code == 0) {
          setTimeout(() => {
            this.$route.meta.refresh = true;
            this.$refs.setActCodePage.btnLoading = false;
            this.$router.go(-1);
            this.form = {
              hmType: 1,
              clerkIdList: [],
              hmUserNum: null //多人活码人数
            };
          }, 1000);
        } else {
          this.$refs.setActCodePage.btnLoading = false;
          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>
