<template>
  <div class="center-parts-shop-in-stock-from">
    <div class="center-parts-shop-in-stock-from-el">
      <el-page-header @back="goBack" content="配件入库"></el-page-header>
      <br />
      <el-table :data="tableData" size="mini" border header-cell-class-name="headerRowColor" style="width: 100%">
        <el-table-column label="序号" width="60">
          <template slot-scope="scope">
            <div class="serial_number">{{ scope.$index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="配件网点" width="180">
          <template slot-scope="scope">
            <el-select v-model="scope.row.receiveShopId" placeholder="请选择" @change="update2(scope.$index)">
              <el-option v-for="item in records" :key="item.websitId" :label="item.name" :value="item.websitId">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="配件编码" width="180">
          <template slot-scope="scope">
            <el-select v-model="scope.row.partsNumber" placeholder="请选择" @change="update(scope.$index)">
              <el-option
                v-for="item in partsList"
                :key="item.goodsCode"
                :label="item.goodsCode"
                :value="item.goodsCode"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="配件名称" width="180">
          <template slot-scope="scope">
            <el-select v-model="scope.row.partsNumber" placeholder="请选择" @change="update(scope.$index)">
              <el-option
                v-for="item in partsList"
                :key="item.goodsCode"
                :label="item.goodsName"
                :value="item.goodsCode"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="单位" width="120">
          <template slot-scope="scope">
            <el-input disabled v-model="scope.row.goodsStockUnit" placeholder="请输入内容"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="单价（采购价）" width="120">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.price" placeholder="请输入内容"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="数量" width="100">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.qty" placeholder="请输入内容"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="总金额" width="100">
          <template slot-scope="scope">
            <el-input disabled :value="scope.row.price * scope.row.qty || ''" placeholder="请输入内容"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="200">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" type="textarea" placeholder="请输入内容"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <div class="caozuo">
              <el-button type="danger" size="mini" @click="del(scope.$index)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <el-row>
        <el-button size="small" type="primary" @click="determine">提交</el-button>
        <el-button size="small" type="primary" @click="add">添加</el-button>
      </el-row>
    </div>
  </div>
</template>

<script>
import { partsNewInAdd } from '@/api/material-system/center/center-parts-shop-in-stock'
import { getWebsit } from '@/api/customerManagement.js'
import { listPageV2 } from '@/api/auxiliaryFittings/attachmentProfile'
export default {
  props: {},
  data() {
    return {
      tableData: [{ remark: '' }],
      partsList: [],
      records: []
    }
  },
  mounted() {
    getWebsit({ type: 'C', status: true, queryPartsWebsit: true }).then(res => {
      this.records = res.data
    })
    listPageV2({
      pageNum: 1,
      pageSize: -1,
      params: [{ param: 'a.status', compare: '=', value: 'ON' }]
    }).then(res => {
      this.partsList = res.data.records
    })
  },
  methods: {
    add() {
      this.tableData.push({ remark: '' })
    },
    del(index) {
      this.tableData.splice(index, 1)
    },
    update(index) {
      var data = this.partsList.find(item => item.goodsCode === this.tableData[index].partsNumber)
      Object.assign(this.tableData[index], data || {}, {
        remark: this.tableData?.[index]?.remark
      })
    },
    update2(index) {
      var data = this.records.find(item => item.websitId === this.tableData[index].receiveShopId)
      if (data) {
        Object.assign(this.tableData[index], {
          remark: this.tableData[index].remark,
          receivePartsWebsiteNumber: data.partsWebsiteNumber,
          receiveShopName: data.name
        })
      } else {
        this.tableData[index]['receivePartsWebsiteNumber'] = ''
        this.tableData[index]['receiveShopName'] = ''
      }
    },
    determine() {
      partsNewInAdd(
        this.tableData.map(item => {
          var data = { ...item }
          delete data.id
          return data
        })
      )
        .then(res => {
          this.$message({
            type: 'success',
            message: `添加成功!`
          })
          this.$emit('success')
          this.$emit('goBack')
        })
        .catch(err => {
          console.log(err)
        })
    },
    goBack() {
      this.$emit('goBack')
    }
  }
}
</script>

<style lang="scss" scoped>
.center-parts-shop-in-stock-from {
  min-width: 860px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  background: #fff;
  overflow: hidden;
  .center-parts-shop-in-stock-from-el {
    padding: 20px;
    width: 100%;
    height: 100%;
    overflow: auto;
  }
  .serial_number {
    width: 100%;
    height: 100%;
    text-align: center;
    padding: 6px;
  }
  .caozuo {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  ::v-deep th .cell {
    width: 100%;
    height: 100%;
    text-align: center;
  }
  ::v-deep .el-table__cell {
    .el-select,
    .el-input,
    .el-textarea__inner,
    .el-input__inner {
      // height: 100% !important;
      border: none !important;
    }
  }
  ::v-deep .cell {
    height: 100%;
    padding: 0 !important;
  }
  ::v-deep .headerRowColor {
    .cell {
      height: 100%;
      padding: 6px !important;
    }
  }
}
</style>
