<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">
              <j-date placeholder="请选择合同签订日期" v-decorator="['sellerSignTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同类型:1标准合同；2协议库存；3采购供货单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['conType']" placeholder="请输入合同类型:1标准合同；2协议库存；3采购供货单" 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 :span="24">
            <a-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择更新时间" v-decorator="['modifyTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="采购订单主键" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fid']" placeholder="请输入采购订单主键"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="销售订单编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fnumber']" 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'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: 'SupplierEndPurchaseorderCopyForm',
    components: {
      JFormContainer,
      JDate,
    },
    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: "/nhgff/supplierEndPurchaseorderCopy/add",
          edit: "/nhgff/supplierEndPurchaseorderCopy/edit",
          queryById: "/nhgff/supplierEndPurchaseorderCopy/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,'pkgNo','poNo','poItemNo','poItemId','conCode','conName','buyerName','buyerCode','materialCode','materialDesc','amount','sellerConCode','serialNumber','sellerSignTime','conType','prjCode','prjName','matCode','fixedTechId','bidBatCode','extDes','matMaxCode','matMedCode','matMinCode','matMaxName','matMedName','matMinName','modifyTime','fid','fnumber'))
        })
      },
      //渲染流程表单数据
      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,'pkgNo','poNo','poItemNo','poItemId','conCode','conName','buyerName','buyerCode','materialCode','materialDesc','amount','sellerConCode','serialNumber','sellerSignTime','conType','prjCode','prjName','matCode','fixedTechId','bidBatCode','extDes','matMaxCode','matMedCode','matMinCode','matMaxName','matMedName','matMinName','modifyTime','fid','fnumber'))
      },
    }
  }
</script>