<template>
  <div>
    <el-dialog
      title="库位"
      :visible.sync="addFormVisible"
      :before-close="handleFormClose"
      width="60%">
        <el-form
        :disabled="mode==='detail'"
          inline
          ref="addForm"
          label-position="top"
          :model="formData"
          :rules="rules">
          <el-form-item
          label="费率组编码"
          prop="rateCode">
            <el-input v-model="formData.rateCode" :disabled="mode==='edit'"></el-input>
          </el-form-item>
          <el-form-item
          label="费率组名称"
          prop="rateName">
            <el-input v-model="formData.rateName"></el-input>
          </el-form-item>
          <el-form-item
          label="备注"
          prop="remark">
            <el-input v-model="formData.remark"></el-input>
          </el-form-item>
        </el-form>
         <hr style="background-color:#F5F5F5;height:1px;border:none" v-if="mode==='detail'">
        <div class="row-mrg-t" v-if="mode==='detail'">
            <div class="operation-info">
              <span>创建人登录名(姓名)：{{formData.creatorLoginName}}({{formData.creatorNameCn}})</span>
              <span>创建时间：{{formData.createTime}}</span>
              <span>修改人登录名(姓名)：{{formData.modifierLoginName}}({{formData.modifierNameCn}})</span>
              <span>修改时间：{{formData.modifyTime}}</span>
            </div>
        </div>
        <div v-if="mode!=='detail'">
          <el-button v-auth="50600010108" type="primary" @click="submitForm">保存</el-button>
          <el-button v-auth="50600010109" type="primary" @click="submitAddForm">保存并新增</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      formData: {},
      rules: {
        rateCode: [
          { required: true, message: '费率组编码必填', trigger: 'blur' }
        ],
        rateName: [
          { required: true, message: '费率组名称必填', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    addFormVisible: Boolean,
    rowData: Object,
    mode: String
  },
  methods: {

    // 发送保存数据
    psotList () {
      const url = this.$API.charging.costGroup.addList
      const rateModel = JSON.stringify(this.formData)
      const param = { rateModel }
      // console.log(param)
      this.$https.post(url, param).then(res => {
        if (res.success) {
          this.$emit('saveOK')
        }
      })
    },

    // 关闭新增弹窗
    handleFormClose () {
      this.$emit('handleFormClose')
      this.$refs.addForm.resetFields()
    },

    // 保存
    submitForm () {
      this.$refs.addForm.validate((vaild) => {
        if (vaild) {
          this.psotList()
          this.$nextTick(() => {
            this.handleFormClose()
          })
        } else {
          return false
        }
      })
    },

    // 保存并新增
    submitAddForm () {
      this.$refs.addForm.validate((vaild) => {
        if (vaild) {
          this.psotList()
          this.$nextTick(() => {
            this.$refs.addForm.resetFields()
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    rowData: {
      handler (item) {
        this.formData = { ...item }
      },
      deep: true
    }
  }
}
</script>
