<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="outTime">
      <el-date-picker
        v-model="dataForm.outTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="支出金额" prop="outMoney">
      <el-input v-model="dataForm.outMoney" placeholder="支出金额" onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="outNr">
      <el-input v-model="dataForm.outNr" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="outWay">
      <el-select
        v-model="dataForm.outWay"
        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%;"
        @change="selectZc"
      >
        <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="outProject">
      <el-select
        v-model="dataForm.outProject"
        placeholder="选择立项归属"
        style="width: 90%;"
      >
        <el-option
          v-for="item in projectList"
          :key="item.projectId"
          :label="item.project"
          :value="item.projectId"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item v-if="isShow" label="收款人" prop="outPayee">
      <el-input v-if="!selectskr" v-model="dataForm.outPayee" placeholder="收款人"></el-input>
      <el-select
        v-else
        v-model="dataForm.outPayee"
        placeholder="选择收款人"
        style="width: 90%;"
      >
        <el-option
          v-for="item in recipientList"
          :label="item.name"
          :value="item.dictId"
        ></el-option>
      </el-select>
    </el-form-item>
<!--    <el-form-item label="地址" prop="outPayeeAddress">-->
<!--      <el-input :disabled="otherZclbDisabled" v-model="dataForm.outPayeeAddress" placeholder="地址"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="电话" prop="outPayeePhone">-->
<!--      <el-input :disabled="otherZclbDisabled" v-model="dataForm.outPayeePhone" placeholder="电话"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="备注" prop="outRemark">-->
<!--      <el-input v-model="dataForm.outRemark" 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 {
        isShow: false,
        visible: false,
        selectskr: false, // 收款人
        otherZclbDisabled: true, // 其他支出类别不可以
        dataForm: {
          id: 0,
          outTime: '',
          outMoney: '',
          outNr: '',
          outWay: '',
          outType: '',
          outProject: '',
          outPayee: '',
          outPayeeAddress: '',
          outPayeePhone: '',
          outRemark: '',
          status: 0,
          writeUserId: '', // 填写人id
          approval: 0
        },
        dataOldForm: {
          id: 0,
          outTime: '',
          outMoney: '',
          outNr: '',
          outWay: '',
          outType: '',
          outProject: '',
          outPayee: '',
          outPayeeAddress: '',
          outPayeePhone: '',
          outRemark: '',
          status: 0,
          writeUserId: '', // 填写人id
          approval: 0
        },
        userId: '',
        authenticName: '',
        projectList: [], // 立项信息
        payList: [], // 支付方式
        recipientList: [], // 收款人
        recipientList_nbzz: [], // 收款人_内部转账
        recipientList_all: [], // 收款人_全部
        expenseList: [], // 支出类别
        dataRule: {
          outTime: [
            { 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()

          // 获取登录人信息
          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 (data.user.userId === 10 || data.user.userId === 1) {
                this.isShow = true
              }
            }
            // 获取立项数据
            this.$http.request({
              url: this.$http.adornUrl('/business/businessoutlay/allList'),
              method: 'get'
            }).then(({data}) => {
              // console.log(data);
              if (data && data.code === 0) {
                this.projectList = data.page
                this.projectList.forEach((item) => {
                  if (item.projectNewName !== null) {
                    item.project = item.projectNewName
                  }
                })
              }
              this.$http.request({
                url: this.$http.adornUrl('/business/businessoutlay/selectAllContainsDeparturePage'),
                method: 'get'
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.recipientList_all = data.page
                }
                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
                      } else if (item.dictCode === 'recipient') {
                        this.recipientList = []
                        item.children.forEach((item2) => {
                          item2.dictId = item2.dictId.toString()
                          this.recipientList.push(item2)
                        })
                        this.recipientList_nbzz = []
                        this.recipientList.forEach((item2) => {
                          if (item2.name === '肖爽' || item2.name === '侯吉庆' || item2.name === '李现举') {
                            this.recipientList_nbzz.push(item2)
                          }
                        })
                      } else if (item.dictCode === 'expense_type') {
                        this.expenseList = item.children
                      }
                    })
                  }
                  if (this.dataForm.id && this.dataForm.id !== 0) {
                    this.$http({
                      url: this.$http.adornUrl(`/business/businessoutlay/info/${this.dataForm.id}`),
                      method: 'get',
                      params: this.$http.adornParams()
                    }).then(({data}) => {
                      if (data && data.code === 0) {
                        this.dataForm = {
                          ...data.businessOutlay
                        }
                        this.dataOldForm = {
                          ...data.businessOutlay
                        }
                        this.selectZc()
                      }
                    })
                  }
                })
              })
            })
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        // if (this.authenticName !== '管理员') {
          this.dataForm.writeUserId = this.userId
        // }
        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/businessoutlay/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.outTime,
            //         'approvalData': JSON.stringify(this.dataForm),
            //         'approvalOldData': JSON.stringify(this.dataOldForm),
            //         'approvalType': 6,
            //         '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/businessoutlay/save`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id,
                'outTime': this.dataForm.outTime,
                'outMoney': this.dataForm.outMoney,
                'outNr': this.dataForm.outNr,
                'outWay': this.dataForm.outWay,
                'outType': this.dataForm.outType,
                'outProject': this.dataForm.outProject,
                'outPayee': this.dataForm.outPayee,
                'outPayeeAddress': this.dataForm.outPayeeAddress,
                'outPayeePhone': this.dataForm.outPayeePhone,
                'outRemark': this.dataForm.outRemark,
                '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)
              }
            })
            // }
          }
        })
      },
      selectZc () {
        if (this.dataForm.outType === 141 || this.dataForm.outType === 146) {
          this.selectskr = true
          if (this.dataForm.outType === 141) {
            this.recipientList = []
            this.recipientList = this.recipientList_all
          } else {
            this.recipientList = []
            this.recipientList = this.recipientList_nbzz
          }
          this.otherZclbDisabled = true
        } else {
          this.selectskr = false
          this.otherZclbDisabled = false
        }
      }
    }
  }
</script>
