import { API } from './../../../utils/network/api'
import Toast from 'tdesign-miniprogram/toast/index';
const api = new API()
const app = getApp()
Page({

  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '常用语', //导航栏 中间的标题
    },
    height: app.globalData.height, 
    stickyProps: {
      offsetTop: (app.globalData.height * 2),
      zIndex: 99
    },
    list_1: [],
    list_2: [],
    show: false,
    add_message: "",
    current_words_type: 1,
    focus: false,
    words_css: "border-radius: 20rpx;margin-bottom: 20rpx;",
    from: "",
    open: false
  },
  onLoad(options){
    let from = options.from;
    if(from) {
      this.setData({
        from: from
      })
    }
  },
  onTabChange(e){
    this.setData({
      current_words_type: e.detail.value
    })
  },
  onShow(){
    this.getList(1);
    this.getList(2);
  },
  getList(words_type){
    api.getDoctorWords({words_type: words_type}).then(response => {
      console.log(response);
      let list_name = "list_" + words_type;
      console.log(list_name);
      this.setData({
        [list_name]: response.data,
      })
    }).catch(errors => {console.error(errors);})
  },
  add(e){
    let words_type = this.data.current_words_type;
    let words = this.data.add_message;
    
    api.postDoctorWords({words_type: words_type, words: words}).then(response => {
      console.log(response);
      this.setData({
        add_message: "",
        show: false
      })
    }).then(()=>{
      this.getList(words_type)
    }).catch(errors => {console.error(errors);})
  },
  delWords(e){
    console.log(e);
    let words_type = this.data.current_words_type;
    let doctor_words_id = e.currentTarget.dataset.doctor_words_id;
    if(doctor_words_id == ""){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '系统常用语,无法删除',
        theme: 'warning',
        direction: 'column',
      });
      return
    }
    api.delDoctorWords({doctor_words_id: doctor_words_id}).then(response => {
      console.log(response);
      this.setData({
        open: false
      })
    }).then(()=>{
      this.getList(words_type)
    }).catch(errors => {console.error(errors);})
  },
  showAdd(){
    this.setData({
      show: true,
    });
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  onInput(){
    this.setData({
      focus: true
    })
  },
  wordsClose(e){
    console.log(e);
    const { position, instance } = e.detail;
    console.log(position)
    console.log(instance)
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？',
        }).then(() => {
          instance.close();
        });
        break;
    }
  },
  wordsOpen(e){
    console.log("open")
    console.log(e);
    this.setData({
      open: true
    })
  },
  wordsClick(e){
    console.log("click")
    console.log(e);
    if(this.data.open){
      return
    }
    let words = e.currentTarget.dataset.words;
    let from = this.data.from;
    if(from){
      wx.setStorageSync('words', words);
      wx.navigateBack()
    }
  }
})