<template>
  <zj-page-layout :hasFooter="true">
    <view class="all-container">
      <view class="parts-container">
        <view class="title">
          <view class="left">配件信息</view>
          <view class="right" @tap="addParts">添加</view>
        </view>
        <view class="item" v-for="(item, index) in partsList" :key="index">
          <view class="row">
            <view class="label"><text>*</text>配件{{ index + 1 }}名称</view>
            <u--input placeholder="请输入名称" border="none" inputAlign="right" v-model="item.name"></u--input>
            <view class="del" v-if="partsList.length > 1" @tap="delParts(index)">删除</view>
          </view>
          <view class="row">
            <view class="label"><text>*</text>配件{{ index + 1 }}数量</view>
            <u-number-box v-model="item.num"></u-number-box>
          </view>
        </view>
      </view>

      <view class="remark-container">
        <view class="title">备注</view>
        <u--textarea v-model="remark" placeholder="请输入内容" border="none"></u--textarea>
      </view>
    </view>

    <template slot="footer">
      <view class="footer-btn-group">
        <u-button text="申请记录" @tap="toList"></u-button>
        <u-button type="primary" text="确认提交" @click="submitData"></u-button>
      </view>
    </template>
  </zj-page-layout>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      partsList: [],
      remark: ''
    }
  },

  onLoad({ id }) {
    this.id = id
    // this.getDetail();
    this.addParts()
  },

  methods: {
    addParts() {
      if (this.partsList.length > 0 && this.partsList.some(o => !o.name)) return this.$toast('请填写配件名称')

      this.partsList.push({
        name: '',
        num: 1
      })
    },

    delParts(index) {
      this.$modal({
        content: '确认删除吗？'
      })
        .then(() => {
          this.partsList.splice(index, 1)
        })
        .catch(() => {})
    },

    toList() {
      this.$navToPage({
        url: `/packageMaterial/pages/partsApply/list`
      })
    },

    submitData() {
      if (this.partsList.length > 0 && this.partsList.some(o => !o.name)) return this.$toast('请填写配件名称')

      this.$api
        .postJson('/material/parts/apply/add', {
          orderBaseId: this.id,
          items: this.partsList.map(item => {
            return {
              name: item.name,
              qty: item.num
            }
          }),
          remark: this.remark
        })
        .then(res => {
          this.$successToast('提交成功')
          this.crossPage.$emit('refreshWorkorderList', { tab: '' })
          this.crossPage.$emit('refreshWorkorderDetail', '')
          this.$navToPage(
            {
              url: `/packageMaterial/pages/partsApply/list`
            },
            'redirectTo'
          )
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.parts-container {
  background: #ffffff;
  padding: 30rpx 20rpx;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      font-size: 32rpx;
      font-weight: 600;
    }
    .right {
      color: $theme-color;
      font-size: 28rpx;
    }
  }
  .item {
    padding: 30rpx 0;
    border-bottom: 1px solid #eaeaea;
    &:last-child {
      border-bottom: none;
    }
    .row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      &:last-child {
        margin-top: 30rpx;
      }
      .label {
        font-size: 28rpx;
        margin-right: 30rpx;
      }
      .del {
        font-size: 28rpx;
        color: $error-color;
        margin-left: 30rpx;
      }
    }
  }
}

.remark-container {
  background: #ffffff;
  padding: 30rpx 20rpx;
  margin-top: 20rpx;
  .title {
    font-size: 32rpx;
    font-weight: 600;
  }
  ::v-deep .u-textarea {
    background: #f5f5f5;
    margin-top: 20rpx;
  }
}
</style>
