<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="采购订单编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pono']" placeholder="请输入采购订单编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购订单行项目号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['poitemno']" placeholder="请输入采购订单行项目号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购订单行项目id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['poitemid']" placeholder="请输入采购订单行项目id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['concode']" placeholder="请输入合同编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['conname']" placeholder="请输入合同名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购方公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['buyername']" placeholder="请输入采购方公司名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购方公司编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['buyerCode']" placeholder="请输入采购方公司编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购方物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['materialcode']" placeholder="请输入采购方物料编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购方物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['materialdesc']" placeholder="请输入采购方物料描述"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['amount']" placeholder="请输入采购数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同编号（国网经法）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sellerconcode']" placeholder="请输入合同编号（国网经法）"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="技术规范流水号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['serialnumber']" placeholder="请输入技术规范流水号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同签订日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sellersigntime']" placeholder="请输入合同签订日期"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['contype']" placeholder="请输入合同类型" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['prjcode']" placeholder="请输入项目编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="工程项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['prjname']" placeholder="请输入工程项目名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matcode']" placeholder="请输入物资编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购技术固化ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fixedtechid']" placeholder="请输入采购技术固化ID"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同包号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pkgno']" placeholder="请输入合同包号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="招标批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bidbatcode']" placeholder="请输入招标批次号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="固化ID描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['extdes']" placeholder="请输入固化ID描述"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资大类编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matmaxcode']" placeholder="请输入物资大类编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资中类编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matmedcode']" placeholder="请输入物资中类编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资小类编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matmincode']" placeholder="请输入物资小类编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资大类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matmaxname']" placeholder="请输入物资大类名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资中类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matmedname']" placeholder="请输入物资中类名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物资小类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matminname']" placeholder="请输入物资小类名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'PurchaseorderForm',
    components: {
      JFormContainer,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/API/supplier-send/add",
          edit: "/API/supplier-send/edit",
          queryById: "/API/supplier-send/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'pono','poitemno','poitemid','concode','conname','buyername','buyerCode','materialcode','materialdesc','amount','sellerconcode','serialnumber','sellersigntime','contype','prjcode','prjname','matcode','fixedtechid','pkgno','bidbatcode','extdes','matmaxcode','matmedcode','matmincode','matmaxname','matmedname','matminname','modifytime','pagesize','pagenum','pagecount','total'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'pono','poitemno','poitemid','concode','conname','buyername','buyerCode','materialcode','materialdesc','amount','sellerconcode','serialnumber','sellersigntime','contype','prjcode','prjname','matcode','fixedtechid','pkgno','bidbatcode','extdes','matmaxcode','matmedcode','matmincode','matmaxname','matmedname','matminname','modifytime','pagesize','pagenum','pagecount','total'))
      },
    }
  }
</script>