<!-- 采购入库单 -->
<template>
  <modal v-model="modal" width="700px">
    <div class="title" slot="header">
      领料申请单
    </div>
    <div class="footer" v-if="type === 'add'" slot="footer">
      <Button @click="cfm" class="btn" type="primary">提交</Button>
      <Button @click="cancel" class="btn">取消</Button>
    </div>
    <div class="footer" v-if="type === 'form'" slot="footer">
      <Button @click="cancel" class="btn" type="primary">关闭</Button>
    </div>
    <!--  表单主要内容  -->
    <div class="content" ref="print">
      <div class="row">
        <div class="col" v-if="type === 'form'">
          <div class="label">领料单号：</div>
          <div class="value">{{ data.applyBillNo }}</div>
        </div>
        <div class="col" v-if="type === 'add'">
          <div class="label">申请日期：</div>
          <div class="value">{{ $dayjs().format('YYYY-MM-DD') }}</div>
        </div>
        <div class="col" v-if="type === 'form'">
          <div class="label">申请日期：</div>
          <div class="value">{{ data.applyTime }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">申请部门：</div>
          <div class="value">仓库</div>
        </div>
        <div class="col">
          <div class="label">申请人：</div>
          <div class="value">{{ $store.state.user.name }}</div>
        </div>
      </div>
      <div class="sub-title">
        <span>
          申请信息
          <a v-if="type === 'add'" href="#" @click="show = true">+选择物料</a>
        </span>
        <my-modal-material ref="materialModal" :multiple="true" v-model="show" @getselectedList="getselectedList"></my-modal-material>
      </div>

      <div class="row">
        <div class="col">
          <div class="label">物料编码：</div>
          <div class="value">{{ type === 'add'? material.code : data.materialCode }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">物料名称：</div>
          <div class="value">{{ type === 'add'? material.name : data.materialName }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">物料规格：</div>
          <div class="value">{{ type === 'add'? material.spec : data.spec }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">物料单位：</div>
          <div class="value" v-if="material.code || type === 'form'">米</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">领料数量：</div>
          <div class="value">
            <Input v-model="material.takeQty" type="number" size="small" v-if="type === 'add'"/>
            <span v-if="type === 'form'">{{ data.takeQty }}</span>
          </div>
        </div>
      </div>
      <div class="sub-title" v-if="type === 'form'">
        <span>审批意见</span>
      </div>
      <div class="row"  v-if="type === 'form'">
        <div class="col">
          <div class="table">
            <table>
              <tr>
                <th>审批节点</th>
                <th>审批人</th>
                <th>审批时间</th>
                <th>审批意见</th>
              </tr>
              <tr v-for="(item, index) in flowList" :key="index">
                <td>{{ item.nodeName }}</td>
                <td>{{ item.userName }}</td>
                <td>{{ item.checkTime }}</td>
                <td>{{ item.suggestions }}</td>
              </tr>
            </table>
          </div>
        </div>
      </div>
    </div>
  </modal>
</template>

<script>
import MyModalMaterial from '_c/my-modal-form/my-modal-material'
import { AddApplyTake, GetApprovalFlows } from '@/api/stock'
export default {
  name: 'store-in-form',
  data () {
    return {
      show: false,
      modal: false,
      material: {},
      flowList: []
    }
  },
  components: {
    MyModalMaterial
  },
  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.type === 'form') {
        console.log(this.data.id)
        GetApprovalFlows({
          linkId: this.data.id
        }).then(res => {
          if (res.code === 0) {
            this.flowList = res.result
          }
        })
      }
    }
  },
  methods: {
    getselectedList (data) {
      console.log('????????????12312')
      console.log(data)
      this.material = data[0]
      this.material.materialCode = data[0].code
    },
    cfm () {
      // 未使用打印功能，未改成vue-easy-print
      // this.$print(this.$refs['print'])
      console.log('materialCode')
      console.log(this.materialList)
      if (!this.material.code) {
        return this.$Message.error('请选择物料')
      }
      AddApplyTake({
        applyAccount: this.$store.state.user.account,
        materialList: [this.material]
      }).then(res => {
        if (res.code === 0) {
          this.$Message.info(res.msg || '添加成功！')
          this.material = {}
          this.modal = false
          this.$emit('getlist')
        }
      })
    },
    cancel () {
      this.material = {}
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  .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;
    }
    a {
      font-size: 12px;
    }
  }
  .row {
    padding: 0 10px;
    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 {
            td,th {
              padding: 5px;
              text-align: center;
              border:solid #dcdee2 1px;
              position: relative;
              font-size: 12px;
              th {
                background-color: #eeeeee;
              }
            }
          }
        }
      }
    }
  }
  .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;
    }
  }
}
</style>
