<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="供应商编号：" prop="supplier_num">
        <el-col :span="6">
          <el-input v-model="formData.supplier_num" placeholder="请输入供应商编号"/>
        </el-col>
      </el-form-item>
      <el-form-item label="供应商名称：" prop="supplier_name">
        <el-col :span="6">
          <el-input v-model="formData.supplier_name" placeholder="请输入供应商名称"/>
        </el-col>
      </el-form-item>
      <!-- <el-form-item label="联系人：" prop="supplier_man">
        <el-col :span="6">
          <el-input v-model="formData.supplier_man" placeholder="请输入联系人"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话：" prop="supplier_phone">
        <el-col :span="6">
          <el-input v-model="formData.supplier_phone" placeholder="请输入联系电话"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系地址：" prop="supplier_address">
        <el-col :span="6">
          <el-input v-model="formData.supplier_address" placeholder="请输入联系地址"/>
        </el-col>
      </el-form-item>
      <el-form-item label="对接负责人：" prop="accept_man">
        <el-col :span="6">
          <el-input v-model="formData.accept_man" placeholder="请输入对接负责人"/>
        </el-col>
      </el-form-item> -->
      <el-form-item label="初始欠款：" prop="init_due">
        <el-col :span="6">
          <el-input :disabled="payStatus" v-model="formData.init_due" @change="initDue(formData.init_due)" placeholder="请输入初始欠款"/>
        </el-col>
      </el-form-item>
      <el-form-item label="初始预付款：" prop="init_pay">
        <el-col :span="6">
          <el-input :disabled="dueStatus" v-model="formData.init_pay" @change="initPay(formData.init_pay)" placeholder="请输入初始预付款"/>
        </el-col>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkRqInput, checkPwdLength, checkTel, checkNum1 } from '@/utils/validate'
import { detailSupplier, updateSupplier, createSupplier  } from '@/api/base/index'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        supplier_name: '',
        init_due: '',
        init_pay: ''
      },
      // 校验规则
      formRules: {
        supplier_num: [ checkRqInput ],
        supplier_name: [ checkRqInput ],
        supplier_man: [ checkRqInput ],
        supplier_phone: [ checkRqInput, checkTel ],
        supplier_address: [ checkRqInput ],
        accept_man: [ checkRqInput ],
        init_due: [ checkRqInput, checkNum1 ],
        init_pay: [ checkRqInput, checkNum1 ]
      },
      passwordValue: this.$props.id?false:true, // 密码按钮状态
      payStatus: false, // 初始预收款状态
      dueStatus: false, // 初始欠款状态
    }
  },
  created() {
    this.fetchDetail()
  },
  methods: {
    // 获取企业详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailSupplier(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
            if (this.formData.init_pay>0) {
              this.payStatus = true
            } 
            if (this.formData.init_due>0) {
              this.dueStatus = true
            }
          }
        })
      }
      this.formData.supplier_id = id
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if (this.formData.supplier_id) {
            this.$http(updateSupplier(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(createSupplier(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          }
        } else {
          return false
        }
      })
    },
    // 取消
    onCancel() {
      this.$message({
        message: '已取消!',
        type: 'warning'
      })
      this.$router.go(-1)
    },
    // 初始欠款输入
    initDue(value) {
      (value == ''||value==0) ? this.dueStatus = false : this.dueStatus = true
      this.field('init_pay')
      this.formData.init_pay = 0
    },
    // 初始预收款
    initPay(value) {
      (value == ''||value==0) ? this.payStatus = false : this.payStatus = true
      this.field('init_due')
      this.formData.init_due = 0
    },
    // 去除验证
    field(temp) {
      let _field = this.$refs['postForm'].fields
      _field.map(i => {
          if(i.prop === temp){  //通过prop属性值相同来判断是哪个输入框，比如：要移除prop为'user'
              i.resetField() 
              return false
          }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
