<template>
    <div class="release-message-container">
        <van-nav-bar left-text="取消" right-text="发布"
                     @click-left="onClickLeft"
                     @click-right="onClickRight"
        />
        <div class="radio-group">
            <van-radio-group v-model="radio" direction="horizontal" @change="handleChangeRadio">
                <van-radio name="1">全员发送</van-radio>
                <van-radio name="2">部分可见</van-radio>
            </van-radio-group>
        </div>
        <div class="userlist">
            <van-checkbox-group v-model="checkIds">
                <van-checkbox :name="item.id" shape="square" v-for="(item,index) in userList" :key="index">
                    {{item.userName}}
                </van-checkbox>
            </van-checkbox-group>
        </div>
    </div>
</template>

<script>
  import {getClerkInfoBySupervisorUserId, newTask} from '@/utils/utils.Request.aApi'

  export default {
    name: 'releaseMessage',
    data() {
      return {
        radio: '1',
        checkIds: [],
        supervisorId: '',
        userList: []
      }
    },
    mounted() {
      this.supervisorUserId = sessionStorage.getItem('userId')
      this.getClerkInfoBySupervisorId()
    },
    methods: {
      // 根据主管id获取所属部门全部员工信息
      getClerkInfoBySupervisorId() {
        let _this = this
        getClerkInfoBySupervisorUserId({supervisorUserId: _this.supervisorUserId})
          .then(res => {
            if (res.result == 'success') {
              _this.userList = res.data
              _this.checkIds = res.data.map(v => {
                return v.id
              })
            } else {
              _this.userList = []
              _this.$toast(res.errorMsg);
            }
          })
          .catch(err => {

          })
      },
      onClickLeft() {
        this.$router.push('/addMessage')
      },
      onClickRight() {
        //发布  全员发送 获取全部id  部分可见 获取check 的id
        let title = this.$store.state.message.title
        let content = this.$store.state.message.content
        let supervisorUserId = this.supervisorUserId
        if(this.checkIds.length==0){
          _this.$toast("请选择转发人");
          return false
        }
        let clerkIds = this.checkIds + ""
        let requestParam = {
          title: title,
          content: content,
          supervisorUserId: supervisorUserId,
          clerkIds: clerkIds
        }
        let _this = this
        newTask(requestParam)
          .then(res => {
            if (res.result == 'success') {
              _this.$toast('发布成功');
              //清空store 值
              _this.$store.commit('message/title', "")
              _this.$store.commit('message/content', "")
              _this.$router.push('/message')
            } else {
              _this.$toast(res.errorMsg);
            }
          })
          .catch(err => {

          })

      },
      handleChangeRadio(val) {
        if (val == '1') {
          this.checkIds = this.userList.map(v => {
            return v.id
          })
        }
      },
    }
  }
</script>

<style lang="scss">
    .release-message-container {
        height: 100%;
        overflow: hidden;
        .van-nav-bar {
            padding: 0 20px;
            /*&:after{*/
            /*border: none;*/
            /*}*/
        }
        .radio-group {
            margin: 10px 0 20px 0;
            .van-radio-group--horizontal {
                padding: 0 20px;
                justify-content: space-around;
            }
            .van-radio__icon {
                font-size: 18px;
            }
            .van-radio__label {
                font-size: 14px;
            }

        }
        .userlist {
            height: calc(100% - 66px);
            overflow-x: hidden;
            overflow-y: scroll;
            .van-checkbox-group {
                padding: 0 30px;
                .van-checkbox {
                    margin-bottom: 10px;
                    .van-checkbox__icon {
                        font-size: 18px;
                    }
                    .van-checkbox__label {
                        font-size: 14px;
                    }
                }

            }
        }
    }
</style>
