<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="日期" prop="outinDate">
      <el-date-picker @change="bindChange($event)"
        :disabled="!!dataForm.id"
        v-model="dataForm.outinDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="昨日余额" prop="outinYdayBalance">
      <el-input disabled v-model="dataForm.outinYdayBalance" placeholder="昨日余额" @change="jrYe()" onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')"></el-input>
    </el-form-item>
    <el-form-item label="今日收入" prop="outinTdayIncome">
      <el-input v-model="dataForm.outinTdayIncome" placeholder="今日收入" @change="jrYe()" onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')"></el-input>
    </el-form-item>
    <el-form-item label="今日支出" prop="outinTdayOut">
      <el-input disabled v-model="dataForm.outinTdayOut" placeholder="今日支出" @change="jrYe()" onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')"></el-input>
    </el-form-item>
    <el-form-item label="今日余额" prop="outinTdayBalance">
      <el-input disabled v-model="dataForm.outinTdayBalance" placeholder="今日余额" onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')"></el-input>
<!--      <el-button @click="jrYe()">计算</el-button>-->
    </el-form-item>
    <el-form-item label="收款方式" prop="outinCreditingWay">
      <el-select
        v-model="dataForm.outinCreditingWay"
        placeholder="选择收款方式"
        style="width: 90%;"
      >
        <el-option
          v-for="item in payList"
          :key="item.dictId"
          :label="item.name"
          :value="item.dictId"
        ></el-option>
      </el-select>
    </el-form-item>
      <el-form-item label="收入类别" prop="outType">
        <el-select
          v-model="dataForm.outType"
          placeholder="选择收款方式"
          style="width: 90%;"
        >
          <el-option
            v-for="item in expenseList"
            :key="item.dictId"
            :label="item.name"
            :value="item.dictId"
          ></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="备注" prop="outinRemark">
      <el-input v-model="dataForm.outinRemark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          outinDate: '',
          outinYdayBalance: '',
          outinTdayIncome: '',
          outinTdayOut: '',
          outinTdayBalance: '',
          outinCreditingWay: '',
          outType: '',
          outinRemark: '',
          status: 0,
          writeUserId: '', // 填写人id
          approval: 0
        },
        dataOldForm: {
          id: 0,
          outinDate: '',
          outinYdayBalance: '',
          outinTdayIncome: '',
          outinTdayOut: '',
          outinTdayBalance: '',
          outinCreditingWay: '',
          outType: '',
          outinRemark: '',
          status: 0,
          writeUserId: '', // 填写人id
          approval: 0
        },
        userId: '',
        authenticName: '',
        // 字典内容
        payList: [],
        expenseList: [], // 收入类别
        dataRule: {
          outinDate: [
            { required: true, message: '日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.dataForm.approval = 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id && this.dataForm.id !== 0) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessoutlayincome/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = {
                  ...data.businessOutlayIncome
                }
                this.dataOldForm = {
                  ...data.businessOutlayIncome
                }
              }
              // 获取登录人信息
              this.$http.request({
                url: this.$http.adornUrl('/sys/user/info'),
                method: 'get'
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.userId = data.user.userId
                  this.authenticName = data.user.authenticName
                  // // 获取对应的数据
                  // this.$http.request({
                  //   url: this.$http.adornUrl('/business/businessoutlayincome/getCalcVal'),
                  //   method: 'post',
                  //   data: {
                  //     'writeUserId': this.userId,
                  //     'outinRemark': this.dataForm.outinDate
                  //   }
                  // }).then(({data}) => {
                  //   if(data && data.code == 0){
                  //     this.dataForm.outinYdayBalance = data.zrye;
                  //     this.dataForm.outinTdayOut = data.jrzc;
                  //   }
                  // })
                }
              })
            })
          } else {
            // 获取登录人信息
            this.$http.request({
              url: this.$http.adornUrl('/sys/user/info'),
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.userId = data.user.userId
                this.authenticName = data.user.authenticName
                if(this.dataForm.outinDate == null || this.dataForm.outinDate == undefined || this.dataForm.outinDate ==''){
                  return
                }
                // 获取对应的数据
                this.$http.request({
                  url: this.$http.adornUrl('/business/businessoutlayincome/getCalcVal'),
                  method: 'post',
                  data: {
                    'writeUserId': this.userId,
                    'outinRemark': this.dataForm.outinDate
                  }
                }).then(({data}) => {
                  if(data && data.code == 0){
                    this.dataForm.outinYdayBalance = data.zrye;
                    this.dataForm.outinTdayOut = data.jrzc;
                    this.jrYe();
                  }
                })
              }
            })
          }
          this.$http.request({
            url: this.$http.adornUrl('/business/businessoutlay/dictList'),
            method: 'get'
          }).then(({data}) => {
            // console.log(data);
            if (data && data.code === 0) {
              data.data.forEach((item) => {
                if (item.dictCode === 'pay_type') {
                  this.payList = item.children
                }
                if (item.dictCode === 'expense_type') {
                  this.expenseList = item.children
                }
              })
            }
          })
        })
      },
      jrYe () {
        let v1 = this.dataForm.outinYdayBalance
        if (!v1) {
          v1 = 0
        } else {
          v1 = parseFloat(this.dataForm.outinYdayBalance)
        }
        let v2 = this.dataForm.outinTdayIncome
        if (!v2) {
          v2 = 0
        } else {
          v2 = parseFloat(this.dataForm.outinTdayIncome)
        }
        let v3 = this.dataForm.outinTdayOut
        if (!v3) {
          v3 = 0
        } else {
          v3 = parseFloat(this.dataForm.outinTdayOut)
        }
        this.dataForm.outinTdayBalance = (v1 + v2 - v3).toFixed(2)
        this.$forceUpdate()
      },

      bindChange: function (e) {
        this.dataForm.outinDate = e;
        // 获取对应的数据
        this.$http.request({
          url: this.$http.adornUrl('/business/businessoutlayincome/getCalcVal'),
          method: 'post',
          data: {
            'writeUserId': this.userId,
            'outinRemark': this.dataForm.outinDate
          }
        }).then(({data}) => {
          console.log(data);
          if(data && data.code == 0){
            this.dataForm.outinYdayBalance = data.zrye;
            this.dataForm.outinTdayOut = data.jrzc;
            this.jrYe();
          }
        })
      },

      // 表单提交
      dataFormSubmit () {
        if (this.authenticName !== '管理员') {
          this.dataForm.writeUserId = this.userId
        }
        this.jrYe();
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 进入审批
            /*if (this.dataForm.id && this.dataForm.id !== 0) {
              if (this.dataForm.approval === 1) {
                this.$message('待上次申请审批之后，在重新申请')
              } else if (this.dataForm.approval === 0 && JSON.stringify(this.dataForm) === JSON.stringify(this.dataOldForm)) {
                this.$message('请修改后在进行提交')
              } else {
                this.$http({
                  url: this.$http.adornUrl(`/business/businessoutlayincome/update`),
                  method: 'post',
                  data: this.$http.adornData({
                    'id': this.dataForm.id,
                    'approval': 1
                  })
                })
                // 提交到审批表中
                this.$http.request({
                  url: this.$http.adornUrl('/business/businessapproval/save'),
                  method: 'post',
                  data: this.$http.adornData({
                    'approvalId': this.dataForm.id,
                    'approvalName': this.dataForm.outinDate,
                    'approvalData': JSON.stringify(this.dataForm),
                    'approvalOldData': JSON.stringify(this.dataOldForm),
                    'approvalType': 7,
                    'approvalSuccess': 0
                  })
                }).then(({data}) => {
                  if (data && data.code === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                      }
                    })
                  } else {
                    this.$message.error(data.msg)
                  }
                })
              }
            } else {
              this.$http({
                url: this.$http.adornUrl(`/business/businessoutlayincome/save`),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.id || undefined,
                  'outinDate': this.dataForm.outinDate,
                  'outinYdayBalance': this.dataForm.outinYdayBalance,
                  'outinTdayIncome': this.dataForm.outinTdayIncome,
                  'outinTdayOut': this.dataForm.outinTdayOut,
                  'outinTdayBalance': this.dataForm.outinTdayBalance,
                  'outinCreditingWay': this.dataForm.outinCreditingWay,
                  'outinPayId': this.dataForm.outinPayId,
                  'outinRemark': this.dataForm.outinRemark,
                  'status': this.dataForm.status,
                  'writeUserId': this.dataForm.writeUserId,
                  'approval': this.dataForm.approval
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }*/
            this.$http({
              url: this.$http.adornUrl(`/business/businessoutlayincome/save`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'outinDate': this.dataForm.outinDate,
                'outinYdayBalance': this.dataForm.outinYdayBalance,
                'outinTdayIncome': this.dataForm.outinTdayIncome,
                'outinTdayOut': this.dataForm.outinTdayOut,
                'outinTdayBalance': this.dataForm.outinTdayBalance,
                'outinCreditingWay': this.dataForm.outinCreditingWay,
                'outinRemark': this.dataForm.outinRemark,
                'outType': this.dataForm.outType,
                'status': 0,
                'writeUserId': this.dataForm.writeUserId,
                'approval': 0
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
