<template>
  <div>
    <p class="edit_page_title">{{newsType==='add'?'添加公告':'编辑公告'}}</p>
    <Form ref="newInfo" :model="newInfo" :rules="newInfo_rule" label-position="left" :label-width="90">
      <FormItem label="标题：" prop="title">
        <Input v-model="newInfo.title" placeholder="请输入标题" class="wid_275" :maxlength="100"></Input>
      </FormItem>
      <FormItem label="描述：" prop="description">
        <Input v-model="newInfo.description" placeholder="请输入描述" class="wid_275 zd1 fl" :maxlength="100"></Input>
        <p class="form_tap_text">用于分享资讯文章时展示</p>
        <div class="clear"></div>
      </FormItem>
      <FormItem label="内容：" prop="content">
        <UE :defaultMsg="newInfo.content" :config="config" :id="ue" ref="ueInte"></UE>
      </FormItem>
      <FormItem label="分类：" >
        <RadioGroup v-model="newInfo.category">
          <Radio label="gonggao">公告通知</Radio>
          <Radio label="biaoyang">内部表扬</Radio>
          <Radio label="falv">法律法规</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="推广人群：" prop="role_ids">
        <CheckboxGroup v-model="newInfo.role_ids" class="wid_275 zd1 fl" @on-change='checkchange'>
          <Checkbox v-for="(item,index) in checkList" :key="index" :label="item.id.toString()" class="wid_70">{{item.name}}</Checkbox>
        </CheckboxGroup>
        <p class="form_tap_text">选择公告推送人群，默认全部</p>
        <div class="clear"></div>
      </FormItem>
      <FormItem label="优先级：" prop="sort">
        <Input-number  v-model="newInfo.sort" class="wid_275 zd1 fl"></Input-number>
        <p class="form_tap_text">数值越大排序越靠前</p>
        <div class="clear"></div>
      </FormItem>
      <FormItem label="开始时间：" prop="begin_time">
        <DatePicker type="datetime" :value='newInfo.start_time' :options="bg_data" format="yyyy-MM-dd HH:mm:ss" class="wid_275" @on-change="bgdatechange"></DatePicker>
      </FormItem>
      <FormItem label="截止时间：" prop="end_time">
        <DatePicker type="datetime" :value='newInfo.end_time' :options="ed_data" format="yyyy-MM-dd HH:mm:ss" class="wid_275 zd1 fl" @on-change="eddatechange"></DatePicker>
        <p class="form_tap_text">不填默认永远展示</p>
        <div class="clear"></div>
      </FormItem>
      <FormItem class="form_btn">
        <Button type="primary" @click="editNewInfo('newInfo')">{{newsType==='add'?'添加':'保存'}}</Button>
        <Button class="return_btn mar_L_15" @click="return_false">返回</Button>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import UE from '@@/base/UE'
import {getNewtodaytime, changetime, changeUnixtime } from '@/assets/js/common'
export default {
  name: "edit_newNotice",
  props:{
    newsType:String,
    pindex:Number,
    newsid:Number,
  },
  components:{UE},
  data() {
    return {
      checkList:[],
      ue: 'ueInte',
      config: {
          initialFrameWidth: 700,
          initialFrameHeight: 250
        },
      newInfo:{
        title:'',
        description:'',
        content:'',
        sort:0,
        start_time:'',
        end_time:'',
        role_ids:['0'],
        category:'gonggao'
      },
      bg_data: {
        disabledDate (date) {
          return date && date.valueOf() < Date.now() - 86400000;
        }
      },
      ed_data: {
        disabledDate (date) {
          return date && date.valueOf() < Date.now() - 86400000;
        }
      },
      newInfo_rule: {
        title: { required: true, message: "请输入标题", trigger: "blur" },
        description: { required: true, message: "请输入描述", trigger: "blur" }
      }
    };
  },
  computed:{
    getToday(){
      let today=new Date()
      return today.getFullYear()+'-'+Number(Number(today.getMonth())+1)+'-'+today.getDate()+' '+today.getHours()+':'+today.getMinutes()+':'+today.getSeconds()
    }
  },
  watch: {
    // 'newInfo.start_time'(n){
    //   if(n){
    //     let datetime=getNewtodaytime(n)
    //     this.ed_data={
    //       disabledDate: date => {
    //         return date && date.valueOf() <  new Date(datetime) ;
    //       }
    //     }
    //   }else{
    //     this.ed_data={}
    //   }
    // },
    // 'newInfo.end_time'(n){
    //   if(n){
    //     this.bg_data={
    //       disabledDate: date => {
    //         return date && date.valueOf() >  new Date(n);
    //       }
    //     }
    //   }else{
    //     this.bg_data={}
    //   }
    // },
  },
  methods:{
    checkchange(n){
      this.$nextTick(()=>{
        if(n[this.newInfo.role_ids.length-1]=='0'){
          this.newInfo.role_ids=['0']
        }else{
          if(n.indexOf('0')!==-1){
            this.newInfo.role_ids.splice(n.indexOf('0'),1)
          }
        }
      })
    },
    editNewInfo(formname){
      this.$refs[formname].validate(valid => {
        if (valid) {
          let params={...this.newInfo}
          params.content = this.$refs.ueInte.getUEContent()// 调用子组件方法
          let arr=''
          for(let i in params.role_ids){
            arr+=params.role_ids[i]+','
          }
          params.role_ids = arr.substr(0, arr.length - 1);
          params.start_time=!!params.start_time?changetime(params.start_time):changetime(this.getToday)
          params.end_time=params.end_time!==''?changetime(params.end_time):''
          let api
          if(this.newsType=='edit'){
            params.id=this.newsid
            api='editNewsInfo'
          }else{
            api='addNewsInfo'
          }
          this.$api[api]({...params}).then(data =>{
            if(data.code==200){
              this.$emit('return_false',this.pindex)
              this.$Message.info(data.msg)
            }else{
              this.$Message.info(data.msg)
            }
          })
        }
      });
    },
    getNewsInfoDetail(id){
      this.$api.getNewsInfoDetail({id}).then(data =>{
        if(data){
          this.newInfo.content=data.content
          this.newInfo.title=data.title
          this.newInfo.description=data.description
          this.newInfo.category=data.category
          this.newInfo.role_ids=data.role_ids.split(',')
          this.newInfo.sort=data.sort
          this.newInfo.start_time=data.start_time?changeUnixtime(data.start_time):''
          this.newInfo.end_time=data.end_time?changeUnixtime(data.end_time):''
        }
      })
    },
    return_false(){
      this.$emit('return_false',this.pindex)
    },
    bgdatechange(date) {
      this.newInfo.start_time = date;
    },
    eddatechange(date) {
      this.newInfo.end_time = date;
    },
    getCheckRoleList(){
      this.$api.getRoleList({limit:200,page:1}).then(data =>{
        if(data.code==200){
          this.checkList=data.data
          this.checkList.splice(0,0,{id:'0',name:'全部'})
          if(this.newsType=='edit'&&this.newsid){
            this.getNewsInfoDetail(this.newsid)
          }
        }else{
          this.$Message.info(data.msg)
        }
      })
    }
  },
  created(){
    this.$nextTick(()=>{
      this.getCheckRoleList()
    }) 
  }
};
</script>

