<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="客户编号：" prop="client_num">
        <el-col :span="6">
          <el-input v-model="formData.client_num" placeholder="请输入客户编号"/>
        </el-col>
      </el-form-item>
      <el-form-item label="客户名称：">
        <el-col :span="6">
          <el-select v-model="formData.client_name_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in clientNameList" 
              :key="index"
              :label="item.client_name" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="客户单位：" prop="client_company">
        <el-col :span="6">
          <el-input v-model="formData.client_company" placeholder="请输入客户单位"/>
        </el-col>
      </el-form-item>
      <!-- <el-form-item label="联系人：" prop="client_man">
        <el-col :span="6">
          <el-input v-model="formData.client_man" placeholder="请输入联系人"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话：" prop="client_phone">
        <el-col :span="6">
          <el-input v-model="formData.client_phone" placeholder="请输入联系电话"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系地址：" prop="client_address">
        <el-col :span="6">
          <el-input v-model="formData.client_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="accept_finance">
        <el-col :span="6">
          <el-input v-model="formData.accept_finance" 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, checkNum, checkNum1 } from '@/utils/validate'
import { detailClient, updateClient, createClient, getClientData  } from '@/api/base/index'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        client_name_id: '',
        client_company: '',
        accept_man: '',
        accept_finance: '',
        init_due: '',
        init_pay: ''
      },
      // 校验规则
      formRules: {
        client_name_id: [ checkRqInput ],
        client_num: [ checkRqInput ],
        client_company: [ checkRqInput ],
        client_man: [ checkRqInput ],
        client_phone: [ checkRqInput, checkTel ],
        client_address: [ checkRqInput ],
        accept_man: [ checkRqInput ],
        accept_finance: [ checkRqInput ],
        init_due: [ checkRqInput, checkNum1 ],
        init_pay: [ checkRqInput, checkNum1 ]
      },
      passwordValue: this.$props.id?false:true, // 密码按钮状态
      payStatus: false, // 初始预收款状态
      dueStatus: false, // 初始欠款状态
      clientNameList: [], // 客户名称列表
    }
  },
  created() {
    this.fetchClientName()
    this.fetchDetail()
  },
  methods: {
    // 获取客户名称列表
    fetchClientName() {
      this.$http(getClientData(), (res) => {
        this.clientNameList = res.data
      })
    },
    // 获取企业详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailClient(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
            this.formData.client_name_id == 0 ? this.formData.client_name_id = '' : ''
            if (this.formData.init_pay>0) {
              this.payStatus = true
            } 
            if (this.formData.init_due>0) {
              this.dueStatus = true
            }
          }
        })
      }
      this.formData.client_id = id
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if(this.formData.client_name_id=='') delete this.formData.client_name_id==''
          if (this.formData.client_id) {
            this.$http(updateClient(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(createClient(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>
