<template>
  <div style="height: 100%;">
    <div class="work_body_com">
      <scroller :on-refresh="chat_msg_session_list">
        <van-search v-model="keyword" placeholder="请输入搜索关键词"/>
        <van-swipe-cell>
          <van-cell is-link v-for="(item,index) in list" :key="index" :title="item.name" :to="item.path" class="employee_cell">
            {{item.time_text}}
            <template slot="icon">
              <van-tag style="display:inline-block; text-align: center; line-height: 0.4rem; font-size:0.2rem; width:0.4rem; height:0.4rem; padding:0;-webkit-border-radius: 50px;-moz-border-radius: 50px;border-radius: 50px; position: absolute; left:1.1rem; top:-0.02em;" v-if="item.num" round type="danger">{{item.num}}</van-tag>
              <img :src="item.avatar" class="employee_cell_head_img"/>
            </template>
            <template slot="label">{{item.new_content}}</template>
            <template slot="right">
              <van-button square type="danger" text="删除" />
              <van-button square type="primary" text="收藏"/>
            </template>
          </van-cell>
        </van-swipe-cell>
        <div style="height: 100px;">
        </div>
        <van-search v-model="keyword" placeholder="请输入搜索关键词"/>
        <div style="height: 100px;">
        </div>
        <van-search v-model="keyword" placeholder="请输入搜索关键词"/>
      </scroller>
    </div>
    <div style="background: #000; top:0; bottom:0; left:0; right:0; z-index:9999; position: fixed;"
         v-if="qr_scan_mark">
      <div style="background: #000; bottom:1rem; left:0; right:0; z-index:9999; position: fixed; text-align: center; z-index: 999;">
        <van-button v-show="!set_flash" @click="set_flash_true">开灯</van-button>
        <van-button v-show="set_flash" @click="set_flash_false">关灯</van-button>
        <van-button @click="$route_back">关闭</van-button>
      </div>
    </div>

  </div>
</template>

<script>
import Vue from 'vue'
import { SwipeCell } from 'vant'

Vue.use(SwipeCell)

export default {
  name: 'scan_qr',
  data () {
    return {
      qr_scan_mark: false,
      plus_show: false,
      barcode: null,
      set_flash: false
    }
  },
  methods: {
    set_flash_true () {
      this.set_flash = true
      this.barcode.setFlash(true)
    },
    set_flash_false () {
      this.set_flash = false
      this.barcode.setFlash(false)
    },
    close_scan () {
      this.barcode.close()
      this.set_flash = false
      this.qr_scan_mark = false
      // plus.webview.currentWebview().hide()
    },
    // 扫码成功回调
    onmarked (type, result) {
      let self = this
      var text = '未知: '
      switch (type) {
        case plus.barcode.QR:
          text = 'QR: '
          break
        case plus.barcode.EAN13:
          text = 'EAN13: '
          break
        case plus.barcode.EAN8:
          text = 'EAN8: '
          break
      }

      if (result.indexOf('join_company') >= 0) {
        // 加入组织
        self.$router.push({'path': result.split('#')[1]})
      } else if (result.indexOf('chat?touser') >= 0) {
        self.$router.push({'path': result.split('#')[1]})
      } else if (result.indexOf('invite_reg') >= 0) {
        self.$router.push({'path': result.split('#')[1]})
      } else if (result.indexOf('login_token') >= 0) {
        self.$router.push({'path': result.split('#')[1]})
      } else if (result.indexOf('http://') == 0 || result.indexOf('https://') == 0) {
        var webview = plus.webview.create(result, result)
        webview.setStyle({titleNView: {titleColor: '#000', autoBackButton: true}})
        plus.storage.setItem('qr_scaning', '1')
        plus.key.addEventListener('backbutton', function () {
          webview.canBack(function (e) {
            if (e.canBack) {
              webview.back()
            } else {
              webview.close()
              self.$route_back()
              plus.storage.removeItem('qr_scaning')
            }
          })
        })
        webview.show()
      } else if (text == '未知: ') {
        this.$toast(text + result)
      }
      this.close_scan()
    },
    // 创建Barcode扫码控件
    createBarcode () {
      this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
        top: '0',
        left: '0px',
        width: '100%',
        height: (window.innerHeight - 150) + 'px',
        position: 'static',
        frameColor: '#999999',
        scanbarColor: '#999999'
      })
      this.barcode.onmarked = this.onmarked
      plus.webview.currentWebview().append(this.barcode)

      this.qr_scan_mark = true
      this.barcode.start()
    }
  },
  created () {
    this.createBarcode()
    setTimeout(() => {
      plus.navigator.setStatusBarStyle('light')
    }, 500)
    window.$ajax_update = function () {
      plus.webview.currentWebview().close(plus.webview.currentWebview().id, 'none')
    }
  }
}
</script>

<style scoped>
  .cur_company_name {
    line-height: 1rem;
    text-indent: 0.5em;
    cursor: pointer;
  }

  .menu_title {
    text-indent: 0.5em;
    margin-bottom: 0.16rem;
  }

  .work_body_com {
    height: calc(100% - 0.92rem);
    position: relative;
  }

  .van-radio-group {
    padding: 0.24rem 0.32rem;
  }

  .van-radio {
    height: 0.8rem;
  }

  .employee_cell_head_img {
    width: 1rem;
    height: 1rem;
    border: 1px solid #eee;
    margin-right: 0.24rem;
  }

  .employee_cell {
    padding: 0.16rem 0.32rem;
  }

  .employee_cell .van-cell__title {
    line-height: 0.5rem;
    font-size:0.36rem;
    font-weight: bold;
  }
  .employee_cell .van-cell__right-icon{display: none;}
  .employee_cell .van-cell__label{
    font-weight: normal; font-size:0.28rem;}
</style>
