<!-- 发票 -->
<template>
  <div>
    <modal
      v-model="modal"
      width="700px"
      class-name="vertical-center-modal"
      :footer-hide="true"
    >
      <!--  表单主要内容  -->
      <div class="title" slot="header">
        {{ data.applyBillNo }}
      </div>
      <div class="content" ref="print">
        <div class="row">
          <div class="col" style="align-items: center">
            <div class="value" style="flex: 1">
              <Button type="primary" @click="uploadShow = true"
                >上传发票</Button
              >
            </div>
          </div>
          <div class="col">
            总金额：
            <span style="color: red;">{{ amount || 0 }}元</span>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="table">
              <table style="border-collapse:collapse;border:none;width: 100%;">
                <tr>
                  <th
                    style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;"
                  >
                    文件名称
                  </th>
                  <th
                    style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;"
                  >
                    金额
                  </th>
                  <th
                    style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;"
                  >
                    操作
                  </th>
                </tr>
                <tr v-for="(item, index) in picList" :key="index">
                  <td
                    style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;"
                  >
                    {{ item.fileName }}
                  </td>
                  <td
                    style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;"
                  >
                    {{ item.amount }}
                  </td>
                  <td
                    style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;"
                  >
                    <Button
                      type="primary"
                      size="small"
                      @click="openPic(item.filePath)"
                      style="margin-right: 10px"
                      >查看</Button
                    >
                    <Button type="error" size="small" @click="removePic(index)"
                      >删除</Button
                    >
                  </td>
                </tr>
              </table>
            </div>
          </div>
        </div>
      </div>
    </modal>
    <modal
      v-model="uploadShow"
      class-name="vertical-center-modal"
      width="500px"
    >
      <div class="title" slot="header">上传发票</div>
      <div class="footer" slot="footer">
        <Button @click="uploadCfm" class="btn" type="primary" size="small"
          >提交</Button
        >
        <Button @click="uploadCancel" class="btn">取消</Button>
      </div>
      <div class="content">
        <div class="wrap">
          <div class="row">
            <div class="col" style="align-items: center">
              <div class="label">发票：</div>

              <Upload
                action=""
                ref="uploadFileFlag"
                :before-upload="beforeFile"
              >
                <Button icon="ios-cloud-upload-outline">
                  上传文件
                </Button>
              </Upload>
            </div>
          </div>
          <div class="row" v-if="uploadForm.fileName">
            <div class="col" style="position:relative;">
              {{ uploadForm.fileName }}
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">金额：</div>
              <div class="value">
                <InputNumber
                  :min="1"
                  v-model="uploadForm.amount"
                  style="width: 100px"
                ></InputNumber>
              </div>
            </div>
          </div>
        </div>
      </div>
    </modal>
  </div>
</template>

<script>
import { UploadBase64 } from '@/api/upload'
import { GetContractBills, UploadContractBills } from '@/api/applyPurchase'
import { filePreview } from '@/libs/util'

export default {
  name: 'store-in-form',
  data () {
    return {
      uploadForm: { amount: 0 },
      amount: 0,
      uploadShow: false,
      modal: false,
      picList: [],
      contractsList: []
    }
  },
  components: {},
  props: {
    data: {
      type: Object,
      default () {
        return {}
      }
    },
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        this.getContractBills()
      }
    }
  },
  methods: {
    /* 上传/删除附件 */
    uploadContractBills () {
      UploadContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        contractsList: this.contractsList
      }).then(res => {
        if (res.code === 0) {
          this.contractsList = []
          this.uploadCancel()
          this.getContractBills()
          this.$emit('getlist')
        }
      })
    },
    /* 移出附近 */
    removePic (index) {
      if (this.picList[index].id) {
        UploadContractBills({
          purchaseBillNo: this.data.purchaseBillNo,
          contractsList: [],
          delItems: [this.picList[index].id]
        }).then(res => {
          if (res.code === 0) {
            this.getContractBills()
            this.$emit('getlist')
          }
        })
      }
    },
    /* 获取附件 */
    getContractBills () {
      GetContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        fileType: 1
      }).then(res => {
        if (res.code === 0) {
          this.amount = 0
          this.picList = res.result
          this.picList.forEach(item => {
            this.amount += Number.parseFloat(item.amount)
          })
          this.amount = this.amount.toFixed(2)
        }
      })
    },
    beforeFile (file) {
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = event => {
        UploadBase64({
          fileBase64: event.srcElement.result,
          fileName: file.name,
          floder: 'order'
        }).then(res => {
          if (res.code === 0) {
            let fileObj = {
              size: file.size,
              fileName: file.name,
              filePath: res.result,
              fileType: 1
            }
            this.uploadForm = { ...this.uploadForm, ...fileObj }
          }
        })
      }
      return false
    },
    openPic (url) {
      filePreview(url)
    },
    uploadCfm () {
      if (!this.uploadForm.amount) return this.$Message.error('请输入金额')
      if (!this.uploadForm.fileName)
        return this.$Message.error('请选择上传文件')
      this.contractsList.push({ ...this.uploadForm })
      this.uploadContractBills()
    },
    uploadCancel () {
      this.uploadForm = { amount: 0 }
      this.uploadShow = false
    },
    cfm () {
      // 未使用打印功能，未改成vue-easy-print
      this.$print(this.$refs['print'])
    },
    cancel () {
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  .title {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
  }
  .sub-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px;
    font-size: 14px;
    font-weight: bold;
    &.border {
      border-bottom: 1px solid #000000;
    }
    .go {
      font-size: 10px;
      font-weight: normal;
      color: #0000ff;
    }
  }
  .card {
    margin-bottom: 5px;
    background-color: #ffffff;
    border: 1px solid rgba(215, 215, 215, 1);
    border-radius: 5px;
    .border {
      border-bottom: 1px solid #5d6c7b;
    }
    .line {
      width: 100%;
      border-bottom: 1px solid #5d6c7b;
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: center;
      .table {
        width: 100%;
        table {
          border-collapse: collapse;
          border: none;
          width: 100%;
          tr {
            th {
              background-color: #eeeeee;
            }
            td,
            th {
              padding: 5px;
              text-align: center;
              border: solid #dcdee2 1px;
              position: relative;
              font-size: 12px;
            }
          }
        }
      }
    }
  }
}
</style>
