<!-- 新增采购合同 -->
<template>
    <div>
        <div class="my-title">{{ !id?'新增':'修改' }}</div>
        <!-- 表单 -->
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
          <el-row class="my-row">
            <el-col :span="8">
              <el-form-item label="合同编码" prop="contractCode">
                <el-input v-model="dataForm.contractCode" disabled placeholder="合同编码由系统生成" style="width: 200px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同类别" prop="type">
                <el-select
                  v-model="dataForm.type"
                  remote
                  placeholder="请选择">
                  <el-option
                    v-for="item in optionsType"
                    :key="item.code"
                    :label="item.value"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商" prop="supplierId">
                <supplier-component v-model="dataForm.supplierId" :supplier-id.sync="dataForm.supplierId"></supplier-component>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="my-row">
            <el-col :span="8">
              <el-form-item label="合同交期" prop="deliveryTime">
                <el-date-picker
                  v-model="dataForm.deliveryTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetime">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同号" prop="contractNumber">
                <el-input v-model="dataForm.contractNumber" placeholder="合同号" style="width: 200px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同金额" prop="totalAmount">
                <el-input-number v-model="dataForm.totalAmount" :step="1" :min="0" :precision="1"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="my-row" style="margin-top: 20px">
            <el-form-item label="备注" prop="notes">
              <el-input type="textarea" v-model="dataForm.notes"></el-input>
            </el-form-item>
          </el-row>
          <el-row class="my-row">
            <upload-component :title="'附件'" :accept="'*'" :file-obj-list="fileList" @uploadSuccess="uploadSuccess"/>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="onChose">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </div>
</template>

<script>
  import UploadComponent from '../common/upload-component'
  import { getContractDetail } from '@/api/sale'
  import SupplierComponent from '../common/supplier-component'

export default {
    name: 'contract-add-or-update',
    components: {
      SupplierComponent,
      UploadComponent
    },
    computed: {
      orgId: {
        get () { return this.$store.state.user.orgId }
      }
    },
    data () {
      return {
        id: 0,
        dataForm: {},
        dataRule: {
          type: [{ required: true, message: '请选择合同类别', trigger: 'change' }],
          contractNumber: [{ required: true, message: '合同号不能为空', trigger: 'blur' }],
          totalAmount: [{ required: true, message: '合同金额不能为空', trigger: 'change' }],
          supplierId: [{ required: true, message: '请选择供应商', trigger: 'change' }]
        },
        optionsType: [
          {
            code: '1', value: '采购合同'
          },
          {
            code: '2', value: '委外合同'
          }
        ],
        fileList: []
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      async init (id) {
        this.fileList = []
        this.dataForm = {}
        this.id = id || 0
        // 详情
        if (!id) return
        await getContractDetail(this.id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
            // 附件
            if (data.data.attachList) {
              data.data.attachList.forEach((item) => {
                this.fileList.push({
                  name: item.fileName,
                  url: item.url,
                  id: item.url
                })
              })
            }
          }
        })
      },
      uploadSuccess (fileList) {
        this.fileList = fileList
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 附件
            let fList = this.fileList
            if (fList.length > 0) {
              this.dataForm.attachList = []
              for (let i = 0; i < fList.length; i++) {
                this.dataForm.attachList.push({
                  fileName: fList[i].name,
                  url: fList[i].url
                })
              }
            } else {
              this.$message.error('请上传文件')
              return
            }
            // 产品明细
            this.$http({
              url: this.$http.adornUrl(`/biz-service/purPurchaseContract/${!this.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({...this.dataForm, orgId: this.orgId})
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
