<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="form" :rules="rules" ref="form" label-width="0">
        <u-form-item prop="contractCode">
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            合同编号
          </view>
          <u-input v-model="form.contractCode" disabled />
        </u-form-item>
        <u-form-item prop="collectionAmount">
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            回款金额
          </view>
          <u-input v-model.number="form.collectionAmount" placeholder="请输入回款金额" />
        </u-form-item>
        <u-form-item prop="collectionDatetime" @click="show = true">
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            回款日期
          </view>
          <u-input
            v-model="form.collectionDatetime"
            disabled
            disabledColor="#ffffff"
            placeholder="请选择回款日期"></u-input>
        </u-form-item>
        <u-form-item prop="collectionType" @click="showPicker = true">
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            回款方式
          </view>
          <u-input v-model="form.collectionName" disabled disabledColor="#ffffff" placeholder="请选择回款方式" />
        </u-form-item>
        <u-form-item prop="remark">
          <view class="form-label flex_l">
            <view class="label-tag"></view>
            备注
          </view>
          <u-textarea
            fontSize="26rpx"
            v-model="form.remark"
            placeholder="输入备注"
            height="180"
            :count="true"
            maxlength="300"></u-textarea>
        </u-form-item>
      </u-form>
      <view class="save" @click="save">保存</view>
    </view>
    <u-calendar :show="show" @confirm="confirmCalendar" @close="show = false"></u-calendar>
    <u-action-sheet :actions="collectionTypeOption" @select="selectClick" :show="showPicker"></u-action-sheet>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
  import api from '@/api/contract'
  import to from 'await-to-js'
  export default {
    data() {
      return {
        height: '',
        paddingTop: '',
        form: {
          planId: null, //计划回款id
          collectionDatetime: '', //回款日期
          contractCode: '', //合同编号
          contractId: null, //合同id
          collectionAmount: '', //回款金额
          collectionType: '', //回款方式
          collectionName: '',
          remark: '', //备注
        },
        rules: {
          contractCode: [
            {
              required: true,
              trigger: 'blur',
              message: '请选择合同',
            },
          ],
          collectionAmount: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入回款金额',
            },
          ],
          collectionDatetime: [
            {
              required: true,
              trigger: 'change',
              message: '请选择回款日期',
            },
          ],
        },
        show: false,
        showPicker: false,
        collectionTypeOption: [],
      }
    },
    created() {
      const navData = uni.getMenuButtonBoundingClientRect()
      this.height = navData.height + 'px'
      this.paddingTop = navData.top + 'px'
      this.getOptions()
    },
    onLoad(option) {
      this.form.contractId = parseInt(option.id)
      this.form.contractCode = option.code
    },
    methods: {
      getOptions() {
        Promise.all([this.getDicts('collection_type')])
          .then(([collectionType]) => {
            this.collectionTypeOption = collectionType.data.values || []
            this.collectionTypeOption.forEach((item) => (item.name = item.value))
          })
          .catch((err) => console.log(err))
      },
      closeMoveInModel() {},
      confirmCalendar(val) {
        this.form.collectionDatetime = val[0]
        this.show = false
      },
      selectClick(val) {
        this.form.collectionType = val.key
        this.form.collectionName = val.value
        this.showPicker = false
      },
      async save() {
        let params = {
          ...this.form,
        }
        delete params.collectionName
        params.collectionAmount = parseInt(params.collectionAmount)
        this.$refs.form
          .validate()
          .then(async (valid) => {
            if (valid) {
              const [err, res] = await to(api.addCollection(params))
              if (err) return
              this.$refs.uToast.show({
                type: 'success',
                message: '创建成功',
                complete: () => {
                  this.goBack()
                },
              })
            }
          })
          .catch((errors) => {})
      },
      goBack() {
        uni.navigateBack({
          //关闭当前页面，返回上一页面或多级页面。
          delta: 1,
        })
      },
    },
  }
</script>

<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 - 280rpx);
      background: #ffffff;
      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;
        }
      }

      .save {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 92rpx;
        background: #3e7ef8;
        border-radius: 31rpx 31rpx 0 0;
        margin: 116rpx auto 0;
        font-size: 32rpx;
        color: #ffffff;
        text-align: center;
        line-height: 92rpx;
      }
    }
  }
</style>
