<template>
  <div class="app-container">
    <el-card>
      <span>物料名称:</span>
      <el-input v-model="materialName" placeholder="物料名称" size="small" clearable/>
      <span>库存状态:</span>
      <el-select v-model="inventoryStateValue" placeholder="库存状态" size="small" clearable>
        <el-option
          v-for="item in inventoryState"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>仓库:</span>
      <el-select v-model="purchaseStateValue" placeholder="仓库" size="small">
        <el-option
          v-for="item in purchaseState"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="pageCurrent1=1;pageSize1=10;total1=0;searchTable1()">查询</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading1"
        :data="tableData1"
        border
        style="width: 100%;max-height: 300px;"
        height="300px"
        size="mini"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center" width="55"/>
        <el-table-column label="库存状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.库存是否不足)===1" type="danger" size="small">不足</el-tag>
            <el-tag v-else type="success" size="small">充足</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="物料名称" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="物料型号" min-width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column label="物料规格" min-width="200" align="center">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column label="供货商" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.供货商 }}
          </template>
        </el-table-column>
        <el-table-column label="当前库存量" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.当前库存量 }}
          </template>
        </el-table-column>
        <el-table-column label="安全库存量" min-width="100" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.changeSafeValue">
              <el-input v-model="scope.row.安全库存量" class="edit-input" size="mini"/>
              <el-button class="cancel-btn" size="mini" icon="el-icon-refresh" type="warning" @click="cancelEdit(scope.row)">取消</el-button>
            </template>
            <span v-else>{{ scope.row.安全库存量 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设置" min-width="100" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.changeSafeValue" type="mini" size="mini" icon="el-icon-circle-check-outline" @click="confirmEdit(scope.row)">确定</el-button>
            <el-button
              v-else
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="scope.row.changeSafeValue=!scope.row.changeSafeValue">设置</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>

    <el-card>
      <span>请购单号:</span>
      <el-input v-model="purchasecard" placeholder="请购单号" size="small" clearable/>
      <span>仓库:</span>
      <el-select v-model="HouseNumber" placeholder="请选择仓库" size="small" @change="searchDemo();payMethodChange()">
        <el-option
          v-for="item in House"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="pageCurrent2=1;pageSize2=10;total2=0;searchTable2()">查询</el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-plus"
        style="margin-left: 10px"
        @click="addTableData">创建离线请购单</el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-download"
        style="margin-left: 10px"
        @click="addMateriel">选入物料</el-button>
    </el-card>
    <el-card>
      <el-table v-loading="" :data="tableData2" border style="max-height: 300px;" height="300px" size="mini" highlight-current-row @row-click="searchTable3">
        <el-table-column label="请购单状态" align="center" width="80" fixed="left">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.是否传递)===0" type="warning" size="small">未传递</el-tag>
            <el-tag v-if="parseInt(scope.row.是否传递)===1" type="primary" size="small">已传递</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="请购单编号" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.请购单编号 }}
          </template>
        </el-table-column>
        <el-table-column label="请购人" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="仓库" align="center" min-width="250">
          <template slot-scope="scope">
            {{ scope.row.仓库名称 }}
          </template>
        </el-table-column>
        <el-table-column label="请购时间" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.请购时间.split(' ')[0] }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="280" fixed="right">
          <template slot-scope="scope">
            <el-button :disabled="parseInt(scope.row.离线合同状态) === 1" type="danger" size="mini" icon="el-icon-delete" @click="deleteOfflineContract(scope.row)">作废</el-button>
            <el-button type="warning" size="mini" icon="el-icon-download" @click="exportCard(scope.row)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin: 10px 0;">
        <el-pagination
          :current-page="pageCurrent2"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize2"
          :total="total2"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"/>
        <el-button style="margin-left:500px;" type="warning" size="mini" icon="el-icon-download" @click="doneRequestCard()">传递</el-button>
      </div>
      <el-table v-loading="" :data="tableData3" border style="max-height: 300px;" height="300px" size="mini">
        <el-table-column label="离线请购单编号" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.请购单编号 }}
          </template>
        </el-table-column>
        <el-table-column label="物料名称" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.物料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="物料规格" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.物料规格 }}
          </template>
        </el-table-column>
        <el-table-column label="物料型号" align="center" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.物料型号 }}
          </template>
        </el-table-column>
        <el-table-column label="物料品种" align="center" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.物料品种 }}
          </template>
        </el-table-column>
        <el-table-column label="物料类别" align="center" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.物料类别 }}
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" min-width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.数量" :disabled="disable" size="mini" style="width:70px"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="100">
          <template slot-scope="scope">
            <el-button :disabled="disable" type="danger" size="mini" icon="el-icon-delete" @click="deleteMateriel(scope.row)">移除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :visible.sync="dialogVisibleExport" center style="min-height: 300px" width="60%">
      <el-card>
        <div id="print">
          <div style="text-align: center">
            <h2>离 线 请 购 单</h2>
            <hr>
            <table class="tgClass" style="table-layout: fixed; width: 820px;margin: auto">
              <colgroup>
                <col style="width: 73px">
                <col style="width: 78px">
                <col style="width: 84px">
                <col style="width: 88px">
                <col style="width: 94px">
                <col style="width: 80px">
              </colgroup>
              <tr>
                <td class="tg-s268">请购单号:</td>
                <td class="tg-0lax" style=" text-align: left">{{ inbondCard[0] }}</td>
                <td class="tg-0lax">请购人:</td>
                <td class="tg-0lax" style="text-align: left">{{ inbondCard[1] }}</td>
              </tr>
              <tr>
                <td class="tg-0lax">仓库名称:</td>
                <td class="tg-0lax" style="text-align: left">{{ inbondCard[2] }}</td>
                <td class="tg-0lax">请购时间:</td>
                <td class="tg-0lax" style="text-align: left">{{ inbondCard[3] }}</td>
              </tr>
            </table>
            <div style="margin-top: 10px">
              <table class="tg" style="table-layout: fixed; width: 820px; margin: auto">
                <tr>
                  <th class="tg-c3ow">物料名称</th>
                  <th class="tg-c3ow">物料规格</th>
                  <th class="tg-c3ow">物料型号</th>
                  <th class="tg-c3ow">物料品种</th>
                  <th class="tg-c3ow">物料类别</th>
                  <th class="tg-c3ow">数量</th>
                </tr>
                <tr v-for="(list, index) in tableData3" :key="index">
                  <td class="tg-c3ow">{{ list.物料名称 }}</td>
                  <td class="tg-c3ow">{{ list.物料规格 }}</td>
                  <td class="tg-c3ow">{{ list.物料型号 }}</td>
                  <td class="tg-c3ow">{{ list.物料品种 }}</td>
                  <td class="tg-c3ow">{{ list.物料类别 }}</td>
                  <td class="tg-c3ow">{{ list.数量 }}</td>
                </tr>
              </table>
            </div>
          </div>
        </div>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisibleExport=false">取消</el-button>
        <el-button type="primary" size="small" @click="printCard">打印</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchRequestCard, initWarehouse, addRequestCard, searchInventoryNum, SeeDetail, addRequestCardDetail, deleteRequest, deleteRequestDetail, alterNumber, setSafeValue } from '@/api/Inventory/OfflinePurchase'
import { mapGetters } from 'vuex'
import $ from 'jquery'

export default {
  data() {
    return {
      purchasecard: '',
      purchasecard_check: 1,
      loading1: false,
      title2: '',
      purchaseStateValue: 2, // 采购状态 全部
      purchaseState: [
        {
          label: '未采购',
          value: 1
        }, {
          label: '全部',
          value: 2
        }
      ],
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      materialName: '',
      inventoryStateValue: '', // 库存状态
      inventoryStateValue_check: 1,
      inventoryState: [
        {
          label: '充足',
          value: 2
        },
        {
          label: '不足',
          value: 1
        }
      ],
      tableData1: [],
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      offlineContractNum: '', // 离线合同流水号
      offlineContract: '', // 离线合同号
      supplierName0: '', // 供应商名称（外面的）
      supplierName: '', // 供应商名称
      payMethod: [], // 付款方式
      payTime: [], // 付款时间节点
      tableData2: [],
      total2: 0,
      pageCurrent2: 1,
      pageSize2: 10,
      dialogVisible1: false,
      form1: {},
      tableData01: [],
      dialogVisible2: false,
      tableData3: [],
      textArea: '请选择',
      materielGroup: [], // 物料流水号组
      partGroup: [], // 离线合同明细流水号组
      numGroup: [], // 数量组
      priceGroup: [], // 单价组
      totalPrice: 0, // 合同总额
      tableData4: [], // 合同模板
      stipulateArrivalTime: '', // 规定到货时间
      payMethodName: '', // 付款方式名称
      paramRow: '', // 参数row
      disable2: true,
      disable3: true,
      disable4: true,
      purchasecardnumber: '', // 请购单流水号
      disable: false, // 其他的禁用
      House: [],
      HouseNumber: '',
      dialogVisibleExport: false, // 导出表单
      inbondCard: [] // 导出表格表头
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initWarehouse().then(response => { // 初始化仓库
        console.log(response)
        for (let i = 0; i < response.data.length; i++) {
          this.House.push({
            label: response.data[i].仓库名称,
            value: response.data[i].仓库流水号
          })
        }
      })
    },
    // 查询离线物料请购单
    searchTable2() {
      if (this.purchasecard === '') {
        this.purchasecard_check = 0
      } else {
        this.purchasecard_check = 1
      }
      searchRequestCard(this.pageCurrent2, this.pageSize2, this.purchasecard_check, this.purchasecard).then(response => {
        this.tableData2 = response.data.rows
        this.total2 = response.data.total
      })
    },
    // 查询离线物料请购单明细
    searchTable3(row) {
      this.purchasecardnumber = row.离线请购单流水号
      SeeDetail(this.purchasecardnumber).then(response => {
        this.tableData3 = response.data
      })
    },
    // 查询离线物料数量
    searchTable1() {
      if (this.materialName === '') {
        this.materialName_check = 0
      }
      if (this.inventoryStateValue === '') {
        this.inventoryStateValue_check = 0
      }
      searchInventoryNum(this.pageCurrent1, this.pageSize1, this.materialName_check, this.materialName, this.inventoryStateValue_check, this.inventoryStateValue).then(response => {
        this.tableData1 = response.data.rows
        this.tableData1.map(v => {
          this.$set(v, 'changeSafeValue', false)
          v.原安全库存量 = v.安全库存量
          return v
        })
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) { // 离线请购单列表分页
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) { // 离线请购单列表分页
      this.pageCurrent1 = val
      this.searchTable1()
    },
    handleSizeChange2(val) { // 离线请购单列表分页
      this.pageSize2 = val
      this.searchTable2()
    },
    handleCurrentChange2(val) { // 离线请购单列表分页
      this.pageCurrent2 = val
      this.searchTable2()
    },
    // 增加请购单
    addTableData() {
      if (this.HouseNumber === '') {
        this.$message.error('请选择仓库')
      } else {
        console.log(this.id, this.HouseNumber)
        addRequestCard(this.id, this.HouseNumber).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('添加成功')
            this.searchTable2()
          } else {
            this.$message.error('添加失败')
          }
        })
      }
    },
    handleSelectionChange(val) {
      this.materielGroup = []
      for (let i = 0; i < val.length; i++) {
        this.materielGroup.push(val[i].物料流水号)
      }
    },
    addMateriel() { // 将物料选入离线合同
      if (this.purchasecardnumber === '') {
        this.$message.error('请选择请购单')
      } else {
        addRequestCardDetail(this.purchasecardnumber, this.materielGroup).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('选入成功')
            SeeDetail(this.purchasecardnumber).then(response => {
              this.tableData3 = response.data
            })
          } else {
            this.$message.error('选入失败')
          }
        })
      }
    },
    deleteOfflineContract(row) { // 删除请购单
      this.$confirm('确定删除该请购单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRequest(row.离线请购单流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable2()
            SeeDetail(this.purchasecardnumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deleteMateriel(row) { // 删除请购单明细
      this.$confirm('确定移除该物料?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRequestDetail(row.离线请购单明细流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('移除成功')
            this.searchTable2()
            SeeDetail(this.purchasecardnumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('移除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消作废'
        })
      })
    },
    cancelEdit(row) { // 取消设置
      row.安全库存量 = row.原安全库存量
      row.changeSafeValue = false
      this.$message('取消修改')
    },
    confirmEdit(row) { // 确认设置
      Number(row.安全库存量) ? row.安全库存量 = Number(row.安全库存量) : row.安全库存量 = 0
      setSafeValue(row.物料流水号, parseInt(row.安全库存量)).then(response => {
        if (response.data[0].result === '1') {
          row.changeSafeValue = false
          this.$message.success('设置成功')
          this.searchTable1()
        } else {
          this.$message.error('设置失败')
        }
      })
    },
    exportCard(row) {
      this.dialogVisibleExport = true
      this.inbondCard[0] = row.请购单编号
      this.inbondCard[1] = row.姓名
      this.inbondCard[2] = row.仓库名称
      this.inbondCard[3] = row.请购时间.split(' ')[0]
      SeeDetail(this.purchasecardnumber).then(response => {
        this.tableData3 = response.data
      })
    },
    // 打印请购单
    printCard() {
      const htmlNode = $('#print').html() // #print为需要打印的dom节点id
      const body = $('body')
      body.children().hide()
      const printNode = $(htmlNode)
      body.append(printNode)
      window.print()
      body.children().not('script').show()
      printNode.remove()
    },
    // 传递请购单
    doneRequestCard() {
      this.$confirm('确定传递请购单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.partGroup = []
        this.numGroup = []
        for (let i = 0; i < this.tableData3.length; i++) {
          this.partGroup.push(this.tableData3[i].离线请购单明细流水号)
          this.numGroup.push(this.tableData3[i].数量)
        }
        alterNumber(this.purchasecardnumber, this.partGroup, this.numGroup).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('请购单生成成功')
            this.searchTable2()
            SeeDetail(this.purchasecardnumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('请购单生成失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:150px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .el-tag{
    margin: 0
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 5px;
  }
  .tg  {border-collapse:collapse;border-spacing:0;}
  .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
  .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
  .tg .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
</style>
