<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    :footer="null">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">


        <a-form-item label="已选" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.totalNum}}个
        </a-form-item>

        <a-form-item label="企业名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
             {{model.companyName}}
        </a-form-item>


        <a-form-item label="服务商" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.subcontractorName}}
        </a-form-item>

        <template v-if="model.isThroughPlatform==1">
          <a-form-item label="服务费率" :labelCol="labelCol" :wrapperCol="wrapperCol">
            {{model.companyCpServiceRate}}%
          </a-form-item>
        <a-form-item label="开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          ￥{{model.serviceAmount}}元（创客佣金：￥{{model.companyCpAmount}}元+服务费：￥{{model.companyCpServiceFee}}元）
        </a-form-item>
        </template>
        <template v-else>
          <a-form-item label="服务费率" :labelCol="labelCol" :wrapperCol="wrapperCol">
            {{model.serviceRate}}%
          </a-form-item>
          <a-form-item label="开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
            ￥{{model.totalPayment}}（创客佣金：￥{{model.amount}}元+服务费：￥{{model.serviceFee}}元）
          </a-form-item>
        </template>

        <a-form-item label="发票抬头" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.hlwCompanyInvoiceInformation.invoiceTitle}}
        </a-form-item>

        <a-form-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.hlwCompanyInvoiceInformation.dutyParagraph}}
        </a-form-item>

        <a-form-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.hlwCompanyInvoiceInformation.accountBank}}
        </a-form-item>

        <a-form-item label="开户账户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.hlwCompanyInvoiceInformation.account}}
        </a-form-item>

        <a-form-item label="注册地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.hlwCompanyInvoiceInformation.registerAddress}}
        </a-form-item>

        <a-form-item label="注册电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.hlwCompanyInvoiceInformation.registerPhone}}
        </a-form-item>

        <a-form-item style="margin-bottom: 90px" label="票种选择" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <j-dict-select-tag  type="list" v-decorator="[ 'invoiceType', validatorRules.invoiceType]"    :trigger-change="true"  dictCode="invoice_type" placeholder="请选择票种" />
        </a-form-item>

      </a-form>
    </a-spin>
    <div  class="drawer-bootom-button">
      <a-button class="ant-btn"  @click="handleCancel">取消</a-button>
      <a-button class="ant-btn ant-btn-primary" style="margin-left: 8px"  @click="handleOk">申请开票</a-button>
    </div>
  </a-modal>
</template>

<script>
  import Vue from 'vue'
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import { ACCESS_TOKEN } from "@/store/mutation-types"


  export default {
    name: "HlwInvoiceModal",
    components: { 
      JDate,
    },
    data () {
      return {
        headers:{},
        uploadLoading:false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {hlwCompanyInvoiceInformation:{}},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          invoiceType:{
            rules: [
              { required: true, message: '请选择票种!'},
            ],
            'initialValue':'1'
          },
        },
        url: {
          add: "/wisdom/hlwInvoice/add",
          fileUpload: window._CONFIG['domianURL']+"/hpSys/common/ossUpload",
          imgerver: window._CONFIG['imgDomainOssURL'],
        }
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    computed:{
      uploadAction:function () {
        return this.url.fileUpload;
      }
    },
    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,'amount','invoiceNumber','serviceInvoiceNumber','invoice','sysOrgCode','uploadTime','uploadBy'))
        })
      },
      getAvatarView(img){
        return this.url.imgerver + img;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.add;
            method = 'post';
            let formData = Object.assign(this.model, values);
            formData.invoiceTitle=this.model.hlwCompanyInvoiceInformation.invoiceTitle
            formData.dutyParagraph=this.model.hlwCompanyInvoiceInformation.dutyParagraph
            formData.accountBank=this.model.hlwCompanyInvoiceInformation.accountBank
            formData.account=this.model.hlwCompanyInvoiceInformation.account
            formData.registerAddress=this.model.hlwCompanyInvoiceInformation.registerAddress
            formData.registerPhone=this.model.hlwCompanyInvoiceInformation.registerPhone
            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;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'amount','invoiceNumber','invoice','sysOrgCode','uploadTime','uploadBy'))
      },


      
    }
  }
</script>
<style lang="less" scoped>
  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>