<!-- 采购申请单 -->
<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">
      <side-bar v-if="showSider"></side-bar>
      <!-- 头部信息 -->
      <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.type === 0 || formData.type === '物料'
            "
          >
            <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;">{{ formData.userName }}</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'">
              <Select v-model="formData.type">
                <Option
                  v-for="item in purTypeList"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.label }}</Option
                >
              </Select>
            </div>
            <div class="value" v-if="type === 'form'" style="flex: 1;">
              {{ formData.type }}
            </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.type === 0 ? '申购说明：' : '申购内容' }} -->
              申购说明：
            </div>
            <div class="value" v-if="type === 'add'">
              <Input v-model="formData.remark" type="textarea" />
            </div>
            <div class="value" v-if="type === 'form'" style="flex: 1;">
              {{
                formData.type === '物料'
                  ? formData.remark
                  : formData.purchaseContent
              }}
            </div>
          </div>
        </div>
      </div>
      <!-- 副标题 -->
      <div
        class="sub"
        v-if="formData.type === 0 || formData.type === '物料'"
        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>
        <a v-if="type === 'add'" href="#" @click="materialShow = true"
          >+添加物料</a
        >
      </div>
      <!-- 申购明细表格 -->
      <div
        class="table"
        v-if="formData.type === 0 || formData.type === '物料'"
      >
        <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>
            <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 applyDetailsList" :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;"
            >
              {{ item.spec }}
            </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.quantity }}</span>
              <Input
                v-if="type === 'add'"
                v-model="item.quantity"
                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>
            <td
              style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;"
            >
              <Icon
                v-if="type === 'add'"
                @click="delRow(index)"
                custom="iconfont icon-delete"
                :size="12"
                color="red"
              />
            </td>
          </tr>
        </table>
      </div>
      <!-- 副标题 -->
      <div v-if="type === 'form'" class="sub">
        <span
          class="sub-title"
          style="margin-top: 20px;font-size: 18px;font-weight: bold;"
          >审批意见</span
        >
      </div>
      <!-- 审批意见表格 -->
      <div v-if="type === 'form'" 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>
          </tr>
          <tr v-for="(item, index) in purchaseFlowsList" :key="index">
            <td
              style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;"
            >
              {{ item.nodeName }}
            </td>
            <td
              style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;"
            >
              {{ item.userName }}
            </td>
            <td
              style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;"
            >
              {{ item.checkTime }}
            </td>
            <td
              style="padding: 5px;text-align: center;border:solid #dcdee2 1px;position: relative;font-size: 12px;"
            >
              {{ item.suggestions }}
            </td>
          </tr>
        </table>
      </div>
    </div>
    <my-modal-material1
      ref="materialModal"
      :multiple="false"
      v-model="materialShow"
      select-key="code"
      :selected-list="selectedList"
      @getselectedList="getselectedList"
    >
    </my-modal-material1>
  </Modal>
</template>

<script>
import { AddApplyPurchase } from '@/api/applyPurchase'
import { GetApprovalFlows } from '@/api/stock'
import MyModalMaterial1 from '_c/my-modal-form/my-modal-material'
import SideBar from './my-modal-sidebar'
export default {
  name: 'index',
  data () {
    return {
      selectedList: [], // 用于表格勾选回填
      materialShow: false,
      applyDetailsList: [],
      purchaseFlowsList: [],
      modal: false,
      formData: {},
      purTypeList: [
        { label: '物料', value: 0 },
        { label: '行政', value: 1 }
      ]
    }
  },
  components: {
    MyModalMaterial1,
    SideBar
  },
  props: {
    showSider: {
      type: Boolean,
      default: true
    },
    data: {
      type: Object,
      default () {
        return {}
      }
    },
    type: {
      type: String,
      default: 'add'
    },
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
        this.$set(this.formData, 'type', 0)
        if (n) {
          if (this.type === 'form') {
            this.formData = { ...this.data }
            this.applyDetailsList = this.formData.purDetails
            this.getApplyPurchaseFlows()
            console.log(this.formData)
          }
        }
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
    }
  },
  methods: {
    // 删除已添加物料
    delRow (index) {
      this.applyDetailsList.splice(index, 1)
      this.selectedList.splice(index, 1)
    },
    // 添加物料
    getselectedList (data) {
      console.log('end--------------data')
      console.log(data)
      this.applyDetailsList = []
      this.selectedList = data
      // 数据去重添加
      for (const select of data) {
        let flag = false
        for (const item of this.applyDetailsList) {
          if (select.code === item.materialCode) {
            flag = true
            break
          }
        }
        if (!flag) {
          this.applyDetailsList.push({
            materialCode: select.code,
            materialName: select.name,
            materialId:select.id,
            spec: select.spec
          })
        }
      }
    },
    // 申购提交
    cfm () {
      this.formData.applyDetailsList = this.applyDetailsList
      // 选择申购类型不同时提交不同的字段
      if (this.formData.type === 1) {
        this.formData.purchaseContent = this.formData.remark
      }
      this
        .$post('/api/storage-restructure/applyPurchase/addFlowCreate', this.formData).then(res => {
          if (res.code === 200) {
            this.$Message.info('新增成功！')
            this.$emit('cfm')
            this.cancel()
          }
        })
      
    },
    cancel () {
      this.formData = {}
      this.applyDetailsList = []
      this.selectedList = []
      this.modal = false
    },
    // 打印
    print () {
      // 未改成vue-easy-print
      this.$print(this.$refs.print)
    },
    // 审批流程数据
    getApplyPurchaseFlows () {
      GetApprovalFlows({
        linkId: this.formData.id
      }).then(res => {
        if (res.code === 0) {
          this.purchaseFlowsList = res.result
        }
      })
    }
  },
  beforeUpdate () {
    if (this.modal && this.type === 'form') {
      console.log('???', this.modal, this.type)
      this.formData = { ...this.data }
      this.applyDetailsList = this.formData.purDetails
    }
  }
}
</script>

<style lang="less" scoped>
::-webkit-scrollbar {
  width: 0; /*对垂直流动条有效*/
}
.content {
  padding: 0 40px;
  width: 100%;
  max-height: 80vh;
  overflow: auto;
  .title {
    padding: 10px 0;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
  }
  .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%);
            }
          }
        }
      }
    }
  }
  .footer {
    padding: 1.5vw 0;
    width: 100%;
    display: flex;
    justify-content: center;
    .btn {
      border: none;
      width: 9vw;
      min-width: 100px;
      height: 2vw;
      min-height: 32px;
      font-size: 0.8vw;
      &:first-child {
        margin-right: 30px;
      }
    }
  }
}

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