<!--
 * @Author: liuzhenlin 461480418@qq.ocm
 * @Date: 2023-01-12 11:57:48
 * @LastEditors: liuzhenlin
 * @LastEditTime: 2023-03-22 11:26:04
 * @Description: file content
 * @FilePath: \oms\pages\distributor\create.vue
-->
<template>
  <view class="home">
    <view class="nav">
      <view :style="{ paddingTop }">
        <view class="title" :style="[{ height }, { lineHeight: height }]">
          <view class="back" @click="goBack()">
            <u-icon name="arrow-left" color="#ffffff" size="22"></u-icon>
          </view>
          <text>新建经销商</text>
        </view>
      </view>
    </view>
    <view class="main">
      <u-form :model="addForm" :rules="rules" ref="addForm" label-width="0">
        <u-form-item prop="distName" customStyle="padding:40rpx 0 34rpx" borderBottom>
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            名称
          </view>
          <u-input border="none" v-model="addForm.distName" placeholder="请输入名称"></u-input>
        </u-form-item>

        <u-form-item prop="provinceDesc" borderBottom customStyle="padding:40rpx 0 30rpx" @click="showProvince = true">
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            选择省份
          </view>
          <u-input
            :readonly="true"
            placeholder="请选择省份"
            v-model="addForm.provinceDesc"
            border="none"
            suffixIcon="arrow-down"
            suffixIconStyle="color:#CDCDCD"
            clearable
            customStyle="padding: 0 30rpx 0 12rpx"></u-input>
        </u-form-item>

        <u-form-item prop="registerDistrict" customStyle="padding:40rpx 0 34rpx" borderBottom>
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            注册地
          </view>
          <u-input border="none" v-model="addForm.registerDistrict" placeholder="请输入备注"></u-input>
        </u-form-item>
      </u-form>
      <view class="center">
        <view class="handle-btn" @click="handleAdd" :class="!flag ? 'disabledBtn' : ''">保存</view>
      </view>
    </view>
    <!-- 选择省 -->
    <u-picker
      :show="showProvince"
      :columns="provinceColumns"
      keyName="distName"
      @cancel="showProvince = false"
      @confirm="pickProvince"></u-picker>
    <!-- 选择销售 -->
    <u-notify ref="uNotify"></u-notify>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>
<script>
  import { mapGetters } from 'vuex'
  import to from 'await-to-js'
  import distrApi from '../../api/base/distr'
  import SelectUser from 'components/SelectUser'
  export default {
    name: 'omsIndex',
    components: { SelectUser },
    data() {
      return {
        flag: true,
        height: '',
        paddingTop: '',
        showProvince: false,
        provinceColumns: [],
        addForm: {
          distName: '',
          provinceId: null,
          provinceDesc: '',
          registerDistrict: '',
          distType: '10',
        },
        rules: {
          distName: [{ required: true, trigger: 'blur', message: '请输入名称' }],
          registerDistrict: [{ required: true, trigger: 'blur', message: '请输入注册地' }],
          provinceDesc: [{ required: true, trigger: 'blur', message: '请选择省份' }],
        },
      }
    },
    computed: {
      ...mapGetters(['userId', 'nickName']),
    },
    created() {
      const navData = uni.getMenuButtonBoundingClientRect()
      this.height = navData.height + 'px'
      this.paddingTop = navData.top + 'px'
    },
    mounted() {
      this.getOptions()
    },
    methods: {
      getOptions() {
        Promise.all([distrApi.getProvinceList()])
          .then(([province]) => {
            this.provinceColumns = [province.data.list]
          })
          .catch((err) => console.log(err))
      },
      // 省份选择
      pickProvince(e) {
        this.addForm.provinceId = e.value[0].id
        this.addForm.provinceDesc = e.value[0].distName
        this.showProvince = false
      },
      handleAdd() {
        if (!this.flag) return
        this.$refs.addForm
          .validate()
          .then(async () => {
            let params = this.addForm
            params.belongSale = this.nickName
            params.belongSaleId = this.userId
            this.flag = false
            const [err, res] = await to(distrApi.doAdd(params))
            this.flag = true
            if (err) return
            if (res && res.code == 200) {
              this.$refs.uToast.show({
                type: 'success',
                message: '创建成功',
                complete: () => {
                  uni.navigateBack({
                    //关闭当前页面，返回上一页面或多级页面。
                    delta: 1,
                  })
                },
              })
            }
          })
          .catch((err) => {
            this.$refs.uNotify.show({
              top: this.height + this.paddingTop + 10,
              type: 'warning',
              message: err[0].message,
              duration: 1000 * 3,
            })
          })
      },
      goBack() {
        uni.navigateBack({
          //关闭当前页面，返回上一页面或多级页面。
          delta: 1,
        })
      },
    },
  }
</script>
<style>
  page {
    background: #f2f3f5;
  }
</style>
<style lang="scss" scoped>
  .home {
    padding-top: 188rpx;

    .nav {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 284rpx;
      background: #3e7ef8;

      .title {
        position: relative;
        text-align: center;
        font-size: 32rpx;
        font-weight: bold;
        color: #ffffff;

        .back {
          position: absolute;
          top: 0;
          bottom: 0;
          margin: auto;
          left: 70rpx;
          display: flex;
        }
      }
    }

    .main {
      position: absolute;
      width: 100%;
      height: calc(100vh - 188rpx);
      background: #ffffff;
      box-shadow: 0 6rpx 19rpx 2rpx rgba(0, 45, 132, 0.15);
      border-radius: 31rpx 31rpx 0 0;
      padding: 0 32rpx;
      overflow: auto;
      padding-bottom: 64rpx;

      .form-label {
        font-size: 32rpx;
        font-weight: bold;
        color: #323232;
        padding-bottom: 18rpx;

        .label-tag {
          width: 15rpx;
          height: 15rpx;
          background: #ff4d4f;
          border-radius: 50%;
          margin-right: 10rpx;
        }
      }

      .upload-file-box {
        padding: 20rpx 0;
        position: relative;

        .upload-btn {
          position: absolute;
          right: 0;
          top: 26rpx;
        }
      }
    }

    .handle-btn {
      width: 569rpx;
      height: 92rpx;
      background: #3e7ef8;
      border-radius: 31rpx;
      margin: 116rpx auto 0;
      font-size: 32rpx;
      color: #ffffff;
      text-align: center;
      line-height: 92rpx;
    }
  }
</style>
