<!--
  @description: 服务费开票申请
  @Modifier: sunxinman/haofengxian
  @routerPath: /fee/serviceInvoiceApplyAdd
  @date: 2020/04/06-14:57
-->
<template>
  <div>
    <section class="title p0-16 clearfix">
      <!-- 返回 -->
      <div class="title-back pointer" @click="back()">
        <i class="iconfont icon-fanhui mr10" />服务费开票申请
      </div>
    </section>
    <div class="main">
      <el-form
        ref="fromData"
        :rules="rules"
        :model="formData"
        :inline-message="true"
      >
        <table class="from_table">
          <tr>
            <td>
              <el-form-item label="招标项目编号" />
            </td>
            <td>{{ formData.tenderNo }}</td>
            <td>
              <el-form-item label="招标项目名称" />
            </td>
            <td>{{ formData.tenderName }}</td>
          </tr>
          <tr>
            <td>
              <el-form-item label="标段（包）编号" />
            </td>
            <td>{{ formData.packNo }}</td>
            <td>
              <el-form-item label="标段（包）名称" />
            </td>
            <td>{{ formData.packName }}</td>
          </tr>
          <tr>
            <td>
              <el-form-item label="委托合同" />
            </td>
            <td />
            <td>
              <el-form-item label="开票单位" />
            </td>
            <td />
          </tr>
          <tr>
            <td>
              <el-form-item label="招标代理服务费收取对象" />
            </td>
            <td>
              <span
                v-for="(item,index) in collectionObjList"
                v-if="formData.paymentParty === item.typecode"
                :key="index"
              >{{ item.typename }}</span>
            </td>
            <td>
              <el-form-item label="招标代理服务费收取方式" />
            </td>
            <td>
              <span
                v-for="(item,index) in collectionWayList"
                v-if="formData.paymentMethod === item.typecode"
                :key="index"
              >{{ item.typename }}</span>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="行业分类" />
            </td>
            <td />
            <td>
              <el-form-item :label="paymentPartyLabel" />
            </td>
            <td>
              <el-input v-model.trim="formData.supplierName" style="width: calc(100% - 160px)" />
              <el-button type="primary" class="ml10" @click="selectBidWinner('00')">
                选择
              </el-button>
              <el-button @click="selectBidWinner('01')">
                清空
              </el-button>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="联系人" />
            </td>
            <td>{{ formData.supplierLinker }}</td>
            <td>
              <el-form-item label="联系电话" />
            </td>
            <td>{{ formData.supplierTel }}</td>
          </tr>
          <tr>
            <td>
              <el-form-item label="发票抬头" prop="invoiceTittle" />
            </td>
            <td>
              <el-input v-model.trim="formData.invoiceTittle" />
            </td>
            <td>
              <el-form-item label="开票金额（元）" />
            </td>
            <td>{{ formData.invoiceAmount }}</td>
          </tr>
          <tr>
            <td>
              <el-form-item label="纳税人识别号" />
            </td>
            <td>
              {{ formData.invoiceTax }}
            </td>
            <td>
              <el-form-item label="地址及电话" />
            </td>
            <td>
              {{ formData.invoiceAddress }}
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="开户银行及账号" />
            </td>
            <td>
              {{ formData.invoiceBank }}
            </td>
            <td>
              <el-form-item label="中标金额（元）" />
            </td>
            <td>{{ formData.winPrice }}</td>
          </tr>
          <tr>
            <td>
              <el-form-item label="开票级别" prop="invoiceLevel" />
            </td>
            <td>
              <el-radio-group v-model="formData.invoiceLevel">
                <el-radio v-for="(item,index) in invoiceLevelList" :key="index" :label="item.typecode">
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </td>
            <td>
              <el-form-item label="发票类型" prop="invoiceType" />
            </td>
            <td>
              <el-radio-group v-model="formData.invoiceType">
                <el-radio v-for="(item,index) in invoiceTypeList" :key="index" :label="item.typecode">
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="备注" prop="remark" />
            </td>
            <td colspan="3">
              <el-input
                v-model="formData.remark"
                :autosize="{ minRows: 4, maxRows: 4 }"
                type="textarea"
                maxlength="4000"
                show-word-limit
                style="width: 100%"
              />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item ref="recipientName" label="收件人姓名" prop="recipientName" />
            </td>
            <td>
              <el-input v-model.trim="formData.recipientName" placeholder="收件人姓名" />
            </td>
            <td>
              <el-form-item ref="recipientCompany" label="收件人单位" prop="recipientCompany" />
            </td>
            <td>
              <el-input v-model.trim="formData.recipientCompany" placeholder="收件人单位" />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="联系电话" prop="recipientTel" />
            </td>
            <td>
              <el-input v-model.trim="formData.recipientTel" placeholder="联系电话" />
            </td>
            <td>
              <el-form-item label="联系人邮箱" prop="recipientEmail" />
            </td>
            <td>
              <el-input v-model.trim="formData.recipientEmail" placeholder="联系人邮箱" />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="邮政编码" prop="recipientPostcode" />
            </td>
            <td colspan="3">
              <el-input v-model.trim="formData.recipientPostcode" placeholder="邮政编码" />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="收货地址" prop="recipientAddress" />
            </td>
            <td colspan="3">
              <cascader style="width:300px" />
              <el-input v-model.trim="formData.recipientAddress" placeholder="收货地址" style="width:300px" />
            </td>
          </tr>
        </table>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="but_footer">
      <div class="but_footer-h" />
      <div class="but_footer-but">
        <el-button :loading="confirmLoading" type="primary" @click="submitApproval('fromData')">
          提交审批
        </el-button>
        <el-button @click="back">
          关闭
        </el-button>
      </div>
    </div>
    <!-- 选择中标人弹框 -->
    <chooseBidWinner ref="bidWinner" @bidWinnerInfo="bidWinnerInfo" />
  </div>
</template>

<script>
/* 接口*/
import { saveTenderFeeWinService, detailWinServicePackinfo, detailTenderFeeWinService, checkSupplierOfFee } from '@/api/fee/servicefee'
/* 混入公共方法 */
import { ServiceGroupMixin } from '../../mixins/serviceFeeGroupMixin'
// 组件
import chooseBidWinner from './chooseBidWinner-dialog'
import cascader from '@comp/common/cascader'// 地区三级联动
import { characters1_auto, checkLinktel, email } from '@/assets/js/validate'// 校验输入内容
export default {
  name: 'ChangeBulletinAdd',
  components: {
    chooseBidWinner,
    cascader
  },
  mixins: [ServiceGroupMixin],
  data() {
    return {
      confirmLoading: false, // 提交审批Loding
      packId: this.$route.query.packId, // 标段id
      stageId: this.$route.query.stageId, // 阶段id
      formData: {
        winServicePackinfoId: '', // 中标包组信息id
        auditStatus: '', // 审批状态
        createTime: '', // 创建日期
        createUser: '', // 创建人名称
        djbh: '', // 单据编号
        industryCode: '', // 行业分类代码
        industryName: '', // 行业分类
        invoiceAddress: '', // 地址电话
        invoiceAmount: 0, // 开票金额
        invoiceBank: '', // 银行行号
        invoiceLevel: '00', // 开票级别
        invoiceParty: '', // 开票单位
        invoiceTax: '', // 发票税号
        invoiceTittle: '', // 发票抬头
        invoiceType: '00', // 发票类型
        outId: '', // 退款申请ID
        packId: '', // 标段ID
        packName: '', // 标段名称
        packNo: '', // 标段编号
        paymentMethod: '', // 服务费支付方式00差额定率累进制 01固定金额 02固定比例
        paymentParty: '', // 服务费支付方服务费支付方00中标人 01招标人
        postCode: '', // 运单号
        postStatus: '', // 邮寄状态
        postTime: '', // 邮寄时间
        recipientAddress: '', // 收件人地址
        recipientCompany: '', // 收件人单位
        recipientEmail: '', // 收件人邮箱
        recipientName: '', // 收件人名称
        recipientPostcode: '', // 收件人邮编
        recipientTel: '', // 收件人电话
        remark: '', // 备注
        sendTime: '', // 审批发送时间
        stageId: '', // 阶段ID
        supplierId: '', // 供应商id
        supplierLinker: '', // 供应商联系人
        supplierName: '', // 供应商名称
        supplierTel: '', // 供应商联系电话
        tenderDepart: '', // 业务部门
        tenderDepartId: '', // 业务部门ID
        tenderId: '', // 项目id
        tenderManager: '', // 项目经理
        tenderManagerId: '', // 项目经理ID
        tenderName: '', // 项目名称
        tenderNo: '', // 项目编号
        updateTime: '', // 更新日期
        updateUser: '', // 更新人名称
        useStatus: '', // 使用状态
        winPrice: 0, // 中标价格
        winServiceId: '' // 服务费开票信息ID
      },
      // 校验
      rules: {
        invoiceTittle: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 发票抬头
        invoiceType: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 发票类型
        invoiceLevel: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 开票级别
        recipientAddress: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 收件人地址
        recipientCompany: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 收件人单位
        recipientEmail: [{ required: true, validator: email, trigger: 'blur' }], // 收件人邮箱
        recipientName: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 收件人名称
        recipientPostcode: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 收件人邮编
        recipientTel: [{ required: true, validator: checkLinktel, trigger: 'blur' }] // 收件人电话
      }
    }
  },
  mounted() {
    const winServiceId = this.$route.query.winServiceId // 中标服务费ID
    if (winServiceId) {
      // 查看中标服务费开票信息
      this.getTenderFeeWinServiceDetail(winServiceId)
    } else {
      // 查看中标服务费包组信息详情
      this.getWinServicePackinfoDetail()
    }
  },
  methods: {
    // 查看中标服务费包组信息详情
    getWinServicePackinfoDetail() {
      detailWinServicePackinfo({ stageId: this.stageId }).then(res => {
        const { data } = res
        this.formData = Object.assign(this.formData, data)
      }).catch(() => {
      })
    },
    // 查看中标服务费开票信息
    getTenderFeeWinServiceDetail(winServiceId) {
      detailTenderFeeWinService(winServiceId).then(res => {
        this.formData = res.data
        this.handlePaymentParty()
      }).catch(() => {
      })
    },
    // 处理是否显示招标人或者中标人文案 00中标人 01招标人
    handlePaymentParty() {
      if (this.formData.paymentPart === '01') {
        this.paymentPartyLabel = '招标人'
      }
    },
    // 返回
    back() {
      this.$router.push({
        path: '/fee/serviceInvoiceRecords',
        query: {
          ...this.$route.query
        }
      })
    },
    // 提交审批
    submitApproval() {
      this.$refs.fromData.validate((valid, field) => {
        if (valid) { // 校验通过
          const data = {
            stageId: this.stageId, // 阶段id
            supplierId: this.formData.supplierId, // 供应商id
            winServicePackinfoId: this.formData.winServicePackinfoId, // 中标包组信息id
            invoiceAmount: this.formData.invoiceAmount// 开票金额
          }
          checkSupplierOfFee(data).then(res => {
            if (res.data === '00') {
              // 00 正常
              this.saveData()// 保存数据
            } else if (res.data === '01') {
              // 01 欠款
              this.$confirm('此条服务费开票审批记录有欠款,继续操作？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                confirmButtonClass: 'confirmBtn',
                cancelButtonClass: 'cancelBtn',
                type: 'warning'
              }).then(() => {
                this.saveData()// 保存数据
              })
            }
          })
        }
      })
    },
    // 保存数据
    saveData() {
      const paramsData = {
        ...this.formData,
        packId: this.packId, // 标段id
        stageId: this.stageId// 阶段id
      }
      this.confirmLoading = true
      saveTenderFeeWinService(paramsData).then(res => {
        this.back()
      }).catch(() => {
        this.confirmLoading = false
      })
    },
    // 选择中标人 00选择 01清空
    selectBidWinner(type) {
      if (type === '00') {
        this.$refs.bidWinner.openDialogSelectBidWinner(this.formData.paymentPart)
      } else {
        const data = {
          supplierLinker: '', // 供应商联系人
          supplierName: '', // 供应商名称
          supplierTel: '', // 供应商联系电话
          invoiceTittle: '',
          invoiceAddress: '', // 地址及电话
          invoiceBank: '', // 开户银行及账号
          winPrice: '' // 中标金额
        }
        this.formData = Object.assign(this.formData, data)
      }
    },
    // 中标人信息（子组件传回）
    bidWinnerInfo(data) {
      this.formData = Object.assign(this.formData, data)
    }
  }
}
</script>

<style scoped lang="less">

</style>
