<template>
  <div class="add">
    <div class="pad-l32">
      <div class="item border">
        <span class="label"><span class="tips">*</span>名称</span>
        <div class="value">
          <u-input type="text" placeholder="请输入" v-model="userInfo.name" input-align="right" placeholder-style="color:#999;font-size:16px;" height="48"  :maxlength="30" :clearable="false" />
        </div>
      </div>
      <div class="item border">
        <span class="label"><span class="tips">*</span>类型</span>
        <div class="value">
          <u-input type="text" placeholder="请输入" v-model="userInfo.type" input-align="right" placeholder-style="color:#999;font-size:16px;" height="48"  :maxlength="30" :clearable="false" />
        </div>
      </div>
      <div class="item border">
        <span class="label"><span class="tips">*</span>电话</span>
        <div class="value">
          <u-input type="number" placeholder="请输入" v-model="userInfo.phone" input-align="right" placeholder-style="color:#999;font-size:16px;" height="48"  :maxlength="11" :clearable="false" />
        </div>
      </div>
      <div class="item border">
        <span class="label"><span class="tips">*</span>是否公开</span>
        <div class="value" @click="showSelect=true">
          <span v-if="userInfo.isPublic !== ''">{{$dict.getLabel('yesOrNo', userInfo.isPublic)}}</span>
          <span v-else class="color-999">请选择</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"></u-icon>
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>地区</span>
        <div class="value">
          <AiAreaPicker :areaId="user.areaId" v-model="userInfo.areaId" :name.sync="userInfo.areaName" @select="areaSelect">
            <span class="label" v-if="userInfo.areaName">{{ userInfo.areaName }}</span>
            <span v-else class="color-999">请选择</span>
            <u-icon name="arrow-right" color="#cccccc" size="14"></u-icon>
          </AiAreaPicker>
        </div>
      </div>
    </div>

    
    <div class="footer" @click="submit">
      <div class="btn">保存</div>
    </div>

    <u-select v-model="showSelect" :list="$dict.getDict('yesOrNo')"  label-name="dictName" value-name="dictValue" @confirm="confirmSelect"></u-select>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  props: ['params'],
  data() {
    return {
      id: '',
      userInfo: {
        id: '',
        name: '',
        phone: '',
        type: '',
        isPublic: '',
        areaId: '',
        areaName: ''
      },
      showSelect: false,
      flag: true
    }
  },
  computed: { ...mapState(['user']) },
  onLoad(option) {  
    this.userInfo.areaId = this.user.areaId  
    this.userInfo.areaName = this.user.areaName  
    this.$dict.load('yesOrNo').then(() => {
      if(option.id) {
        this.userInfo.id = option.id
        this.getDetail()
      }
    })
  },
  onShow() {
    document.title = "新增"
  },

  methods: {
    areaSelect(e) {
      this.userInfo.areaId = e
    },
    confirmSelect(e) {
      this.userInfo.isPublic = e[0].value
    },
    getDetail() {
      this.$http.post(`/app/appconvenientaddressbook/queryDetailById?id=${this.userInfo.id}`).then(res => {
        if (res.code == 0) {
          this.userInfo = res.data
        }
      })
    },
    submit() {
      if(!this.userInfo.name) {
        return this.$u.toast('请输入名称')
      }
      if(!this.userInfo.type) {
        return this.$u.toast('请输入类型')
      }
      if(!this.userInfo.phone) {
        return this.$u.toast('请输入电话')
      }
      if(this.userInfo.isPublic === '') {
        return this.$u.toast('请选择是否公开')
      }
      if(!this.userInfo.areaId) {
        return this.$u.toast('请选择地区')
      }
      if(!this.flag) return
      this.$http.post(`/app/appconvenientaddressbook/addOrUpdate`, this.userInfo).then(res => {
        if (res.code == 0) {
          this.flag = false
          this.$u.toast('提交成功')
          uni.$emit('updateList')
          setTimeout(() => {
            uni.navigateBack()
          }, 600)
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.add {
  padding-bottom: 112px;
  .border{
    border-bottom: 1px solid #ddd;
  }
  .item{
    width: 100%;
    padding: 40px 32px 40px 0;
    background: #FFFFFF;
    display: flex;
    justify-content: space-between;
    font-size: 34px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #666;
    line-height: 48px;
    box-sizing: border-box;
    .color-999{
      color: #999;
    }
    .value{
      color: #333;
      .u-icon{
        margin-left: 16px;
      }
    }
    .tips{
      display: inline-block;
      width: 16px;
      font-size: 32px;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #FF4466;
      line-height: 44px;
      margin-right: 4px;
    }
  }
  .footer{
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
  }
  .btn{
    width: 100%;
    height: 112px;
    line-height: 112px;
    text-align: center;
    background: #3975C6;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFF;
  }
  .pad-l32{
    padding-left: 32px;
    background-color: #fff;
  }
}
</style>