<!--suppress ALL -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="contractForm" label-well-code="right" label-width="100px" >
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="第几次付款" prop="reorder">
            <el-input v-model.trim="contractForm.reorder" type="text" placeholder="必填" oninput="value=value.replace(/^\.+|[^\d.]/g,'')"  @blur="reorderChange"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="付款金额" prop="money">
            <el-input v-model.trim="contractForm.money" type="text" placeholder="必填" oninput="value=value.replace(/^\.+|[^\d.]/g,'')"  @blur="moneyChange">
              <template slot="append">万元</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="经手人" prop="handler">
            <el-select v-model="contractForm.handler" filterable placeholder="经手人" clearable value="">
              <el-option
                v-for="item in principalList"
                :key="item.id"
                :label="item.name"
                :value="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="付款时间" prop="paytimeFmt">
            <el-date-picker
              v-model="contractForm.paytimeFmt"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              default-time="12:00:00">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData" :disabled = "isSubmit">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { addPayments,updatePayments } from '@/api/contract'
import { getPrincipalList} from '@/api/devicetype'

export default {
  name: 'EditPay',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      isSubmit: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      deviceList: null, // 在产设备列表
      contractForm: {
        id:'',
        reorder: '',
        money: '',
        handler: '',
        paytimeFmt: '',
        proId: ''
      }, // 表单
      equipmentFile: null,
      textMap: {
        update: '编辑付款信息',
        create: '新增付款信息',
        detail: '详情'
      }, // 表头显示标题
      deviceTypeList: null, // 设备类型列表
      rules: {
        reorder: [{ required: true, message: '第几次付款不能为空', trigger: ['blur', 'change'] }],
        money: [{ required: true, message: '付款金额不能为空', trigger: ['blur', 'change'] }],
        handler: [{ required: true, message: '经手人不能为空', trigger: ['blur', 'change'] }],
        paytimeFmt: [{ required: true, message: '付款时间不能为空', trigger: ['blur', 'change'] }],
      }, // 前端校验规则
      isEditMode: false,
      principalList: null // 批产负责人列表
    }
  },
  watch: {
  },
  mounted() {
  },
  created() {
    this.fetchPrincipalList()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        this.contractForm.proId = row
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.contractForm = {
          reorder: row.reorder,
          money: row.money,
          handler: row.handler,
          paytimeFmt: row.paytimeFmt,
          proId: row.proId,
          id:row.id
        }
        this.isSubmit = false
        this.isEditMode = true
      }
    },
    reorderChange(e){
      this.contractForm.reorder = e.target.value
    },
    moneyChange(e){
      this.contractForm.money = e.target.value
    },
    // 重置表单
    resetForm() {
      this.isSubmit = false
      this.equipmentFile = null
      this.contractForm = {
        reorder: '',
        money: '',
        handler: '',
        paytimeFmt: '',
        id:''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },

    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.isSubmit = true
          addPayments(this.contractForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                var proid = this.contractForm.proId
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
                this.contractForm.proId = proid
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
              this.isSubmit = false
            }else{
              this.$message.error(response.message)
              this.isSubmit = false
            }
          }).catch(() => {
            this.isSubmit = false
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.isSubmit = true
          updatePayments(this.contractForm).then(response => {
            if (response.code === 200) {
              this.$message.success(response.message)
              this.$emit('watchChild')
              this.dialogFormVisible = false
              this.isSubmit = false
            }else{
              this.$message.error(response.message)
              this.isSubmit = false
            }
          }).catch(() => {
            this.isSubmit = false
          })
        }
      })
    },
    // 获取批产负责人
    fetchPrincipalList() {
      getPrincipalList('checker').then(response => {
        this.principalList = response.data
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .labeltext{
    vertical-align: -webkit-baseline-middle;
  }
</style>
