<!-- 采购申请单 -->
<template>
  <Modal
    v-model="modal"
    width="900px"
    :transfer="false"
    @on-cancel="cancel"
    class-name="vertical-center-modal"
  >
    <!-- 标题 -->
    <div class="title" slot="header">采购申请单</div>
    <!-- 底部按钮 -->
    <div class="no-print footer" v-if="type === 'add'" slot="footer">
      <Button @click="cfm" class="btn" type="primary">提交</Button>
      <Button @click="cancel" class="btn">取消</Button>
    </div>
    <div class="no-print" slot="footer">
      <div class="footer" v-if="type === 'form'">
        <Button @click="print" class="btn" type="primary">打印</Button>
        <Button @click="cancel" class="btn">取消</Button>
      </div>
    </div>
    <div class="content" ref="print">
      <!-- 头部信息 -->
      <div class="info" style="padding: 10px 0;">
        <div class="row" style="margin-bottom: 10px;display: flex;justify-content: space-between;">
          <div class="col" style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;">
            <div class="label" style="width: 70px;">申购单号：</div>
            <div class="value" style="flex: 1;">{{ formData.applyBillNo }}</div>
          </div>
          <div class="col" style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;"
               v-if="formData.applyPurType === 0 || formData.applyPurType === '物料'">
            <div class="label" style="width: 70px;">采购部门：</div>
            <div class="value" style="flex: 1;">采购部</div>
          </div>
          <div class="col" v-else style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;"></div>
          <div class="col" style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;">
            <div class="label" style="width: 70px;">申购日期：</div>
            <div class="value" style="flex: 1;">{{ $dayjs().format('YYYY-MM-DD') }}</div>
          </div>
        </div>
        <div class="row" style="margin-bottom: 10px;display: flex;justify-content: space-between;">
          <div class="col" style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;">
            <div class="label" style="width: 70px;">申购部门：</div>
            <div class="value" style="flex: 1;">仓库</div>
          </div>
          <div class="col" style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;">
            <div class="label" style="width: 70px;">申购人：</div>
            <div class="value" style="flex: 1;">{{ $store.state.user.name }}</div>
          </div>
          <div class="col" style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;">
            <div class="label" style="width: 70px;">申购类型：</div>
            <div class="value" v-if="type==='add'">
              物料
            </div>
            <div class="value" v-if="type==='form'" style="flex: 1;">
              {{ formData.applyPurType }}
            </div>
          </div>
        </div>
        <div class="row" style="margin-bottom: 10px;display: flex;justify-content: space-between;">
          <div class="col"  style="padding: 0 5px;flex: 1;display: flex;align-items: flex-start;">
            <!-- <div class="label" style="width: 70px;">{{ formData.applyPurType === 0 ? '申购说明：' : '申购内容' }}</div> -->
            <div class="label" style="width: 70px;">申购说明:</div>
            <div class="value" v-if="type === 'add'">
              <Input v-model="formData.applyAccount" type="textarea"/>
            </div>
            <div class="value" v-if="type === 'form'" style="flex: 1;">
              {{ formData.applyPurType === '物料'? formData.applyAccount :formData.purchaseContent }}
            </div>
          </div>
        </div>
      </div>
      <!-- 副标题 -->
      <div class="sub" v-if="formData.applyPurType === 0 || formData.applyPurType === '物料'"
        style="margin-bottom: 5px;display: flex;justify-content: space-between;align-items: center;"
      >
        <span class="sub-title" style="margin-top: 10px;font-size: 18px;font-weight: bold;">
          申购明细
        </span>
      </div>
      <!-- 申购明细表格 -->
      <div class="table">
        <table style="border-collapse:collapse;border:none;width: 100%;">
          <tr>
            <th style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">物料编码</th>
            <th style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">物料名称</th>
            <th style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">单位</th>
            <th style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">请购数量</th>
            <th style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">用途</th>
          </tr>
          <tr v-for="(item, index) in materialList" :key="index">
            <td style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">{{ item.materialCode }}</td>
            <td style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">{{ item.materialName }}</td>
            <td style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">米</td>
            <td style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">
              <span v-if="type === 'form'">{{ item.purchaseQty }}</span>
              <Input v-if="type === 'add'" v-model="item.purchaseQty" type="number"/>
            </td>
            <td style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;">
              <span v-if="type === 'form'">{{ item.purpose }}</span>
              <Input v-if="type === 'add'" v-model="item.purpose"/>
            </td>
          </tr>
        </table>
      </div>
    </div>
  </Modal>
</template>

<script>
import { AddApplyPurchase } from '@/api/applyPurchase'
export default {
  name: 'index',
  data () {
    return {
      selectedList: [], // 用于表格勾选回填
      materialShow: false,
      materialList: [],
      purchaseFlowsList: [],
      modal: false,
      formData: {},
      purTypeList: [
        { label: '物料', value: 0 },
        { label: '行政', value: 1 }
      ]
    }
  },
  components: {
  },
  props: {
    data: {
      type: Object,
      default () {
        return {}
      }
    },
    type: {
      type: String,
      default: 'add'
    },
    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.materialList.push({ ...this.data })
      }
    }
  },
  methods: {
    // 申购提交
    cfm () {
      this.formData.applyPurType = 0
      this.formData.materialList = this.materialList
      // 选择申购类型不同时提交不同的字段
      if (this.formData.applyPurType === 1) {
        this.formData.purchaseContent = this.formData.applyAccount
      }
      AddApplyPurchase(this.formData).then(res => {
        if (res.code === 0) {
          this.$Message.info('新增成功！')
          this.$emit('getlist')
          this.cancel()
        }
      })
    },
    cancel () {
      this.formData = {}
      this.materialList = []
      this.selectedList = []
      this.modal = false
    },
    // 打印
    print () {
      // 未改成vue-easy-print
      this.$print(this.$refs.print)
    }
  }
}
</script>

<style lang="less" scoped>
::-webkit-scrollbar {
  width: 0; /*对垂直流动条有效*/
}
.content {
    padding: 0 40px;
    width: 100%;
    max-height: 80vh;
    overflow: auto;
    .info {
      padding: 10px 0;
      .row {
        margin-bottom: 10px;
        &:last-child {
          margin-bottom: 0;
        }
        display: flex;
        justify-content: space-between;
        .col {
          padding: 0 5px;
          flex: 1;
          display: flex;
          align-items: flex-start;
          .label {
            width: 70px;
          }
          .value {
            flex: 1;
          }
        }
      }
    }
    .sub {
      margin-bottom: 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .sub-title {
        margin-top: 10px;
        font-size: 18px;
        font-weight: bold;
      }
    }
    .table {
      table {
        border-collapse:collapse;
        border:none;
        width: 100%;
        tr {
          td,th {
            padding: 5px;
            text-align: center;
            border:solid #dcdee2 1px;
            position: relative;
            font-size: 12px;
            th {
              background-color: #eeeeee;
            }
            .del {
              position: absolute;
              left: -30px;
              top: 50%;
              transform: translateY(-50%);
              height: 20px;
              display: flex;
              justify-content: center;
              align-items: center;
              width: 20px;
              border-radius: 50%;
              background-color: red;
              color: #FFFFFF;
              font-weight: bold;
              font-size: 19px;
              cursor: pointer;
              &:after {
                content: '-';
                display: inline-block;
                border-bottom: 3px solid #FFFFFF;
                width: 40%;
                height: 0px;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
              }
            }
          }
        }
      }
    }
  }

///deep/ .ivu-modal-body {
//  min-width: 750px;
//  height: 80vh;
//  overflow: auto;
//}
</style>
