<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="业务id" prop="approvalId">
      <el-input v-model="dataForm.approvalId" placeholder="业务id"></el-input>
    </el-form-item>
    <el-form-item label="业务数据" prop="approvalData">
      <el-input v-model="dataForm.approvalData" placeholder="业务数据"></el-input>
    </el-form-item>
    <el-form-item label="申请人" prop="approvalApply">
      <el-input v-model="dataForm.approvalApply" placeholder="申请人"></el-input>
    </el-form-item>
    <el-form-item label="申请时间" prop="approvalApplyDate">
      <el-input v-model="dataForm.approvalApplyDate" placeholder="申请时间"></el-input>
    </el-form-item>
    <el-form-item label="业务类型（0-工人管理,1-地点名称管理，2-明日计划管理，3-今日总结管理)" prop="approvalType">
      <el-input v-model="dataForm.approvalType" placeholder="业务类型（0-工人管理,1-地点名称管理，2-明日计划管理，3-今日总结管理)"></el-input>
    </el-form-item>
    <el-form-item label="审批是否通过（0-未通过，1-通过）" prop="approvalSuccess">
      <el-input v-model="dataForm.approvalSuccess" placeholder="审批是否通过（0-未通过，1-通过）"></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,
          approvalId: '',
          approvalData: '',
          approvalApply: '',
          approvalApplyDate: '',
          approvalType: '',
          approvalSuccess: ''
        },
        dataRule: {
          approvalId: [
            { required: true, message: '业务id不能为空', trigger: 'blur' }
          ],
          approvalData: [
            { required: true, message: '业务数据不能为空', trigger: 'blur' }
          ],
          approvalApply: [
            { required: true, message: '申请人不能为空', trigger: 'blur' }
          ],
          approvalApplyDate: [
            { required: true, message: '申请时间不能为空', trigger: 'blur' }
          ],
          approvalType: [
            { required: true, message: '业务类型（0-工人管理,1-地点名称管理，2-明日计划管理，3-今日总结管理)不能为空', trigger: 'blur' }
          ],
          approvalSuccess: [
            { required: true, message: '审批是否通过（0-未通过，1-通过）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessapproval/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.approvalId = data.businessApproval.approvalId
                this.dataForm.approvalData = data.businessApproval.approvalData
                this.dataForm.approvalApply = data.businessApproval.approvalApply
                this.dataForm.approvalApplyDate = data.businessApproval.approvalApplyDate
                this.dataForm.approvalType = data.businessApproval.approvalType
                this.dataForm.approvalSuccess = data.businessApproval.approvalSuccess
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessapproval/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'approvalId': this.dataForm.approvalId,
                'approvalData': this.dataForm.approvalData,
                'approvalApply': this.dataForm.approvalApply,
                'approvalApplyDate': this.dataForm.approvalApplyDate,
                'approvalType': this.dataForm.approvalType,
                'approvalSuccess': this.dataForm.approvalSuccess
              })
            }).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>
