<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="客户单位：" prop="client_id">
        <el-col :span="6">
          <el-select v-model="formData.client_id" placeholder="请选择" :disabled="state" clearable>
            <el-option v-for="(item, index) in ClientList" 
              :key="index"
              :label="item.client_company" :value="item.client_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="金额：" prop="order_fee">
        <el-col :span="6">
          <el-input v-model="formData.order_fee" placeholder="请输入金额"/>
        </el-col>
      </el-form-item>
      <el-form-item label="备注：" prop="order_note">
        <el-col :span="6">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入备注"
            v-model="formData.order_note">
          </el-input>
        </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, checkNum, checkNum1 } from '@/utils/validate'
import { getClient  } from '@/api/base/index'
import { saveClientAccount, detailAccountOrder } from '@/api/order/index'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        client_id: '',
        order_fee: '',
        order_note: ''
      },
      // 校验规则
      formRules: {
        client_id: [ checkRqInput ],
        order_note: [ checkRqInput ],
        order_fee: [ checkRqInput, checkNum1 ]
      },
      ClientList: [], // 客户单位列表
      state: false
    }
  },
  created() {
    this.fetchDetail()
    this.fetchClient()
  },
  methods: {
    // 获取客户单位列表
    fetchClient() {
      this.$http(getClient(), (res) => {
        if (res.code == 0) {
          this.ClientList = res.data
        }
      })
    },
    // 获取企业详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailAccountOrder(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
            this.state = true
          }
        })
      }
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if (this.formData.client_id) {
            this.$http(saveClientAccount(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(saveClientAccount(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)
    },
  }
}
</script>

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