<template>
  <view class="wrapper">
    <uni-nav-bar left-icon="back" @clickLeft="back" statusBar fixed title="员工管理"></uni-nav-bar>
	<view v-if="applyList && applyList.length">
	  <view style="padding: 16rpx 24rpx;">待审核（{{applyList.length}}）人</view>
	  <view class="content" v-for="(item, index) in applyList" :key="index">
	    <image class="avatar" src="/static/imgs/mine/default-avatar.png"></image>
	    <view class="info-box">
	      <view class="name">{{ item.applicantName }}</view>
	      <view class="address">{{ item.mobile }}</view>
	    </view>
	    <view style="background-color: #ee0a24;line-height: 72rpx;text-align: center;width: 120rpx;color: white;" @click="handItem(item, 2)">拒绝</view>
	    <view style="background-color: #007AFF;margin-left: 24rpx;line-height: 72rpx;text-align: center;width: 120rpx;color: white;" @click="handItem(item, 1)">同意</view>
	  </view>
	</view>
	<view v-if="list && list.length">
	  <view style="padding: 16rpx 24rpx;">所有员工（{{list.length}}）人</view>
      <view class="content" v-for="(item, index) in list" :key="index" @click="lookItem(item)">
	    <image class="avatar" src="/static/imgs/mine/default-avatar.png"></image>
        <view class="info-box">
          <view class="name">{{ item.realName }}</view>
          <view class="address">{{ item.phone }}</view>
        </view>
	    <uni-icons type="forward" color="#999" size="24"></uni-icons>
      </view>
	</view>
	<view class="flex-col items-center" v-else>
	  <view class="flex-col items-center image-wrapper" style="padding-top: 240rpx;">
	    <image src="/static/imgs/digital-workshops/empty-list.png" style="width: 280rpx;height: 366rpx;" />
	  </view>
	  <text class="text_10">暂无数据</text>
	</view>
	<view class="van-fab" style="bottom:120rpx" @click="shareminiProgram">
	  <view class="flex-row-center-center" style="background-color: #007AFF;width: 100rpx;height: 100rpx;border-radius: 50rpx;">
		<uni-icons type="plusempty" size="36" color="white"></uni-icons>
	  </view>
	</view>
    <uni-popup ref="popup" type="center" :mask-click="false">
      <view class="popup-box">
        <view class="tip-title">确定要删除改地址吗？</view>
        <view class="operation-row">
          <view class="cancel-text" @tap="cancelTap">取消</view>
          <view class="line"></view>
          <view class="confirm-text" @tap="confirmTap">确定</view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import { go2, back, setCache } from '@/utils/hook.js'
import { getMemberList, auditMember, getApplicationList } from '@/apis/enterpriseInfoApi.js'
import { createQrcode } from '@/apis/loginApi.js'
export default {
  data() {
    return {
      /**
       * 页面操作类型 none: 无操作, select: 选择
       */
      operation: 'none',
	  applyList: null,
      list: [],
      id: null,
	  qrcode: null
    }
  },
  onLoad(option) {
    if (option) {
      this.operation = option.operation
    }
  	var factoryId = uni.getStorageSync('factoryId')
  	this.organization = this.findOrgByFactoryId(factoryId)
  	createQrcode({id: 16, metaData: {organizationId: this.organization.organizationId, name: this.organization.organizationName}}).then((res) => {
  	  this.qrcode = res.id
  	})
	this.queryData()
	uni.$on('employee', this.onEvent)
  },
  onUnload() {
  	uni.$off('employee', this.onEvent)
  },
  methods: {
    go2,
    back,
	onEvent(e) {
		
	},
    queryData() {
      getMemberList({id: this.organization.organizationId}).then((res) => {
        if (res) {
          this.list = res.records
        } 
      })
	  getApplicationList({id: this.organization.organizationId}).then((res) => {
	    if (res) {
	      this.applyList = res
	    } else {
		  this.applyList = []
		}
	  })
    },
	findOrgByFactoryId(factoryId){
		var enterpriseList = this.$store.state.userInfo.enterpriseList
		for (var i = 0; i < enterpriseList.length; i++) {
			for (var k = 0; i < enterpriseList[i].factoryList.length; k++) {
				if(enterpriseList[i].factoryList[k].id == factoryId){
					return enterpriseList[i]
				}
			}
		}
	},
    // 设置默认地址
    radioTap(item) {
      setDefaultAddress({ id: item.id }).then((res) => {
        if (res) {
          this.queryData()
        }
      })
    },
    // 选择地址
    selectAddress(item) {
      if (this.operation === 'select') {
        setCache('address', item)
        back()
      }
    },
	handItem(item, status) {
	  auditMember({ id: item.id, status }).then((res) => {
	    if (res) {
	      this.queryData()
	    }
	  })
	},
    lookItem(item) {
      go2('employee-detail', { id: item.id })
    },
    // 新增收货地址
    addTap() {
      go2('add-address-manage', { type: '新增地址' })
    },
    // 删除
    delTap(item, index) {
      this.id = item.id
      this.$refs.popup.open('center')
      // this.$refs.popup.close()
    },
    // 编辑
    editTap(item, index) {
      go2('add-address-manage', { id: item.id, type: '编辑地址' })
    },
    confirmTap() {
      delAddress({ id: this.id }).then((res) => {
        this.$refs.popup.close()
        if (res) {
          uni.showToast({
            title: '删除成功',
            icon: 'success'
          })
          this.queryData()
        }
      })
    },
    cancelTap() {
      this.$refs.popup.close()
    },
	shareminiProgram(){
		uni.share({
		    provider: 'weixin',
		    scene: "WXSceneSession",
		    type: 5,
		    imageUrl: 'https://qncloud.oss-cn-shenzhen.aliyuncs.com/common/45309546072860651.webp',
		    title: '欢迎加入普瑞特',
		    miniProgram: {
		        id: 'gh_ddfcf0fe593d',
		        path: `pages/login/index?from=app&qrcode=${this.qrcode}`,
		        type: 0,
				webUrl: 'https://www.pgyer.com/ClU9'
		    }
		})
	}
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  .content {
	background: #ffffff;
	display: flex;
	align-items: center;
	padding-right: 24rpx;
	border-bottom: 2rpx solid rgba(221, 221, 221, 0.5);
    .info-box {
      max-height: 197rpx;
      padding: 24rpx 16rpx;
	  flex: 1;
      .name {
        font-size: 30rpx;
        color: #333333;
        letter-spacing: 0;
        font-weight: 400;
      }
      .address {
        padding-top: 16rpx;
        font-size: 28rpx;
        color: #333333;
        letter-spacing: 0;
        font-weight: 400;
      }
    }
    .other {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      padding: 0rpx 32rpx;
      height: 80rpx;
      background: #ffffff;
      .radioText {
        font-size: 28rpx;
        color: #007aff;
        letter-spacing: 0;
        font-weight: 400;
      }
      .not-radioText {
        font-size: 28rpx;
        color: #888888;
        letter-spacing: 0;
        font-weight: 400;
      }
      .left {
        font-size: 28rpx;
        color: #007aff;
        letter-spacing: 0;
        font-weight: 400;
      }
      .right {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        font-size: 28rpx;
        color: #888888;
        letter-spacing: 0;
        font-weight: 400;
        .edit {
          padding-left: 32rpx;
        }
      }
    }
  }
  .button-area {
    width: 750rpx;
    padding: 0 32rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    .button {
      flex-grow: 0;
      flex-shrink: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 10rpx;
      .text {
        font-size: 30rpx;
        font-weight: 500;
        text-align: center;
      }
    }
    .button__cancel {
      width: 270rpx;
      height: 88rpx;
      border: 2rpx solid #979797;
    }
    .button__submit {
      width: 100%;
      height: 88rpx;
      background: #007aff;
    }
  }
  .popup-box {
    width: 540rpx;
    height: 226rpx;
    background: #ffffff;
    border-radius: 14rpx;
    .tip-title {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      height: 126rpx;
      font-size: 32rpx;
      color: #333333;
      letter-spacing: 0;
      text-align: center;
      font-weight: 400;
    }
    .operation-row {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      align-items: center;
      height: 100rpx;
      background: #ffffff;
      border-top: 2rpx solid rgba(221, 221, 221, 0.5);
      .cancel-text {
        flex-grow: 1;
        flex-shrink: 1;
        font-size: 36rpx;
        color: #000000;
        letter-spacing: 0;
        text-align: center;
        font-weight: 400;
      }
      .line {
        flex-grow: 0;
        flex-shrink: 0;
        width: 2rpx;
        height: 100rpx;
        border-left: 2rpx solid #dcdee3;
      }
      .confirm-text {
        flex-grow: 1;
        flex-shrink: 1;
        font-size: 36rpx;
        color: #108ee9;
        letter-spacing: 0;
        text-align: center;
        font-weight: 400;
      }
    }
  }
}

.avatar {
        width: 90rpx;
        height: 90rpx;
        border-radius: 50%;
        flex-grow: 0;
        flex-shrink: 0;
        overflow: hidden;
        margin-left: 16rpx;
      }

.van-fab{
  border-radius: 50rpx;
  position: fixed;
  bottom: 160rpx;
  right: 36rpx;
  box-shadow: 0 0 8rpx rgba(0,0,0,.14),0 8rpx 16rpx rgba(0,0,0,.28);
  z-index: 12;
}
</style>
