<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="产品编码" prop="prodCode">
        <el-input v-model.trim="form.prodCode" />
      </el-form-item>
      <el-form-item label="产品名称" prop="prodName">
        <el-input v-model.trim="form.prodName" />
      </el-form-item>
      <el-form-item label="产品分类" prop="prodClass">
        <el-input v-model.trim="form.prodClass" />
      </el-form-item>
      <el-form-item label="建议成交价" prop="guidPrice">
        <el-input v-model.trim="form.guidPrice" />
      </el-form-item>
      <el-form-item label="经销商价" prop="distPrice">
        <el-input v-model.trim="form.distPrice" />
      </el-form-item>
      <el-form-item label="签约代理价" prop="agentPrice">
        <el-input v-model.trim="form.agentPrice" />
      </el-form-item>
      <el-form-item label="市场报价" prop="marketPrice">
        <el-input v-model.trim="form.marketPrice" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model.trim="form.remark" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import productApi from '@/api/product'
  export default {
    name: 'UserEdit',
    data() {
      return {
        form: {
          roles: [],
        },
        rules: {
          userName: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          password: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          roles: [{ required: true, trigger: 'blur', message: '请选择角色' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              this.form.guidPrice = parseFloat(this.form.guidPrice).toFixed(2)
              this.form.distPrice = parseFloat(this.form.distPrice).toFixed(2)
              this.form.agentPrice = parseFloat(this.form.agentPrice).toFixed(2)
              this.form.marketPrice = parseFloat(this.form.marketPrice).toFixed(
                2
              )
              console.log(this.form)
              const { msg } = await productApi.doEdit(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              this.form.guidPrice = parseFloat(this.form.guidPrice).toFixed(2)
              console.log(this.form)
              const { msg } = await productApi.doAdd(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
