<template>
  <view>
    <uni-section padding>
      <uni-forms ref="baseForm" :modelValue="baseFormData" :rules="rules" label-width="80px" label-align="right">
        <uni-forms-item label="产品" required name="cpId">
          <uni-data-select v-model="baseFormData.cpId" placeholder="请选择产品" :localdata="cpData" @change="xzcp" />
        </uni-forms-item>
        <uni-forms-item label="产品编码">
          <uni-easyinput v-model="baseFormData.cpbm" placeholder="请输入产品编码" disabled />
        </uni-forms-item>
        <uni-forms-item label="产品名称">
          <uni-easyinput v-model="baseFormData.cpName" placeholder="请输入产品名称" disabled />
        </uni-forms-item>
        <uni-forms-item label="单价">
          <uni-easyinput v-model="baseFormData.dj" placeholder="请输入单价" disabled />
        </uni-forms-item>
        <uni-forms-item label="客户" required name="lrId">
          <uni-data-select v-model="baseFormData.lrId" placeholder="请选择客户" :localdata="khData" @change="xzkh" />
        </uni-forms-item>
        <uni-forms-item label="收货人">
          <uni-easyinput v-model="baseFormData.shxm" placeholder="请输入收货人" disabled />
        </uni-forms-item>
        <uni-forms-item label="收货电话">
          <uni-easyinput v-model="baseFormData.shdh" placeholder="请输入收货电话" disabled />
        </uni-forms-item>
        <uni-forms-item label="收货地址" required name="shdz">
          <uni-easyinput v-model="baseFormData.shdz" placeholder="请输入收货地址" />
        </uni-forms-item>
        <uni-forms-item label="下单数量">
          <uni-easyinput v-model="baseFormData.xdsl" type="number" placeholder="请输入下单数量" @blur="sl" />
        </uni-forms-item>
        <uni-forms-item label="金额">
          <uni-easyinput v-model="baseFormData.je" placeholder="请输入金额" />
        </uni-forms-item>
        <uni-forms-item label="买家留言">
          <uni-easyinput v-model="baseFormData.xdbz" placeholder="请输入买家留言" />
        </uni-forms-item>
        <uni-forms-item label="支付凭据">
          <uni-easyinput v-model="baseFormData.zfpj" placeholder="请输入支付凭据" />
        </uni-forms-item>
        <uni-forms-item label="运单号">
          <uni-easyinput v-model="baseFormData.ydh" placeholder="请输入运单号" />
        </uni-forms-item>
        <uni-forms-item label="取货码">
          <uni-easyinput v-model="baseFormData.qhm" placeholder="请输入取货码" />
        </uni-forms-item>
        <uni-forms-item label="发货时间">
          <uni-datetime-picker type="datetime" v-model="baseFormData.fhsjs" placeholder="请选择发货时间" />
        </uni-forms-item>
        <uni-forms-item label="到货时间">
          <uni-datetime-picker type="datetime" v-model="baseFormData.dhsjs" placeholder="请选择到货时间" />
        </uni-forms-item>

        <view class="bottomButton">
          <button v-show="baseFormData.ddzt == null || baseFormData.ddzt == 0" type="primary" @click="submit()" size="mini">保存</button>
        </view>
      </uni-forms>
    </uni-section>
  </view>

</template>

<script>
  import { ObjdictList } from '@/api/dict.js'
  import { addDdjy } from '@/api/order.js'
  import { showConfirm } from '@/common/common.js'
  export default {
    data() {
      return {
        baseFormData: {},
        rules: {
          cpId: {
            rules: [{
              required: true,
              errorMessage: '产品不能为空'
						}]
          },
          lrId: {
            rules: [{
              required: true,
              errorMessage: '客户不能为空'
						}]
          },
          shdz: {
            rules: [{
              required: true,
              errorMessage: '收货地址不能为空'
						}]
          },

        },
        cpData: [],
        khData: [],
      }
    },
    onLoad(options) {
      if (Object.keys(options).length !== 0) {
        this.baseFormData = JSON.parse(options.item)
        this.baseFormData.fhsjs = new Date(this.baseFormData.fhsj)
        this.baseFormData.dhsjs = new Date(this.baseFormData.dhsj)
      }
    },
    mounted() {
      this.getCpList()
      this.getKhList()
    },
    methods: {
      sl(e) {
        let sl = e.detail.value
        if (!sl || sl < 1) {
          showConfirm("请输入正确下单数量")
        } else {
          if (this.baseFormData.dj) {
            this.baseFormData.je = this.baseFormData.dj * sl
          }
        }
      },
      xzkh(e) {
        let kh = this.khData.filter(obj => obj.value == e)
        this.baseFormData.shxm = kh[0].obj[1]
        this.baseFormData.shdh = kh[0].obj[2]
        this.baseFormData.shdz = kh[0].obj[3]
      },
      xzcp(e) {
        let kh = this.cpData.filter(obj => obj.value == e)

        this.baseFormData.cpbm = kh[0].obj[2]
        this.baseFormData.cpName = kh[0].obj[1]
        this.baseFormData.dj = kh[0].obj[3]

        this.baseFormData.xdsl = 1
        this.baseFormData.je = this.baseFormData.dj
      },
      getCpList() {
        ObjdictList({
          dictType: 'cpxz',
          outType: 0,
          para1: uni.getStorageSync('userInfo').jgId
        }).then(res => {
          if (res.data.data) {
            Object.keys(res.data.data).map(e => {
              let obj = res.data.data[e]
              this.cpData.push({
                value: obj[0],
                text: obj[1],
                obj: {
                  ...obj
                }
              })
            })

          }

        })
      },
      getKhList() {
        ObjdictList({
          dictType: 'kh',
          outType: 0,
          para1: uni.getStorageSync('userInfo').jgId
        }).then(res => {
          if (res.data.data) {
            Object.keys(res.data.data).map(e => {
              let obj = res.data.data[e]
              this.khData.push({
                value: obj[0],
                text: obj[1],
                obj: {
                  ...obj
                }
              })
            })

          }

        })
      },
      submit() {
        if (this.baseFormData.fhsjs) {
          this.baseFormData.fhsj = this.baseFormData.fhsjs.replaceAll('-', '').replace(' ', '').replaceAll(':', '')
        }
        if (this.baseFormData.dhsjs) {
          this.baseFormData.dhsj = this.baseFormData.dhsjs.replaceAll('-', '').replace(' ', '').replaceAll(':', '')
        }
        this.$refs['baseForm'].validate().then(res => {

          addDdjy(this.baseFormData).then(r => {
            if (r.code == 200) {
              uni.showToast({
                title: `保存成功！`,
                duration: 2000,
                success: function(res) {
                  setTimeout(function() {
                    uni.switchTab({
                      url: '/pages/order/index'
                    });
                    uni.$emit('update', { isReset: true })
                  }, 2000)
                }
              })
            }
          })
        }).catch(err => {
          console.log("err", err)
          uni.showModal({
            title: '错误提示',
            content: err[0].errorMessage,
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定');
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });

        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bottomButton {
    height: 140rpx;
    width: 60%;
    margin: 0 auto;
    text-align: center;

    button {
      width: 40%;
    }

    button+button {
      margin-left: 15px;
    }
  }
</style>