<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2022-10-24 15:45:01
-->
<template>
  <div class="page">
    <topBar v-if="topList.length > 0"
            :topList="topList"
            :moren="moren"></topBar>
    <div class="main">
      <div class="form_all">
        <el-form ref="select"
                 :model="select"
                 label-width="auto">
          <el-row :gutter="10">
            <el-col :span="4">
              <el-form-item label="入库编号">
                <el-input clearable
                          v-model="select.orderCode"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="4">
              <el-form-item label="采购人">
                <el-input clearable
                          v-model="select.pkSalesman"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="编号">
                <el-input clearable
                          v-model="select.orderCode"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="4">
              <el-form-item label="供应商">
                <el-select clearable
                           v-model="select.pkCubasdoc"
                           :placeholder="'请选择'">
                  <el-option v-for="(item, index) in supplierList"
                             :key="index"
                             :label="item.custName"
                             :value="item.pkId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'订单状态'">
                <el-select
                  clearable
                  v-model="select.orderStatus"
                  :placeholder="'请选择'"
                >
                  <el-option
                    v-for="(item, index) in orderStatusList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="采购公司">
                <el-select clearable
                           v-model="select.pkCorp"
                           :placeholder="'请选择'">
                  <el-option v-for="(item, index) in corpList"
                             :key="index"
                             :label="item.corpName"
                             :value="item.pkId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="产品名称">
                <el-input clearable
                          v-model="select.productName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
            <el-form-item label="入库日期"
                          prop="orderDate">
              <el-date-picker v-model="orderDate"
              @change="getTime"
                              value-format="yyyy-MM-dd"
                              type="daterange">
              </el-date-picker>
            </el-form-item>
          </el-col>
            <el-col :span="4">
              <el-button type="primary"
                         @click="getDataList"> {{ '搜索' }}</el-button>
              <el-button type=""
                         @click="reset"> {{ '重置' }}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="small"
                     @click="openDig"
                     v-hasButtons="['ProcureInCz']"
                     type="primary">参照</el-button>
          <el-button size="small"
                     :disabled="selRow.length==0||selRow.length>1"
                     v-hasButtons="['ProcureInUp']"
                     @click="goQc"
                     type="success">{{'提交'}}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table :data="tableData"
        v-loading="loading"
                  style="width: 100%"
                  height="350px"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  :row-class-name="tableRowClassName"
                  @row-click="selectRow"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection"
                           width="55"> </el-table-column>
          <el-table-column align="center"
                           prop="orderCode"
                           label="入库编号">
          </el-table-column>

          <el-table-column align="center"
                           prop="managerName"
                           label="管理员">
          </el-table-column>
          <el-table-column align="center"
                           prop="cubasdocName"
                           label="供应商">
          </el-table-column>
          <el-table-column align="center"
                           prop="corpName"
                           label="采购公司">
          </el-table-column>
          <el-table-column align="center"
                           prop="deptName"
                           label="采购部门">
          </el-table-column>
          <el-table-column align="center"
                           prop="totalNum"
                           label="总数量">
          </el-table-column>
          <el-table-column align="center"
                           prop="orderStatusVal"
                           label="订单状态">
          </el-table-column>
          <el-table-column align="center"
                           prop="printStatusVal"
                           label="打印状态">
          </el-table-column>
          <el-table-column align="center"
                           prop="remark"
                           label="备注">
          </el-table-column>
          <el-table-column align="center"
                           prop="orderDate"
                           label="入库日期">
          </el-table-column>
          <el-table-column align="center"
                           prop="time"
                           width="150"
                           :label="'操作'"
                           fixed="right">
            <template slot-scope="scope">
              <el-button @click="toBack(scope.row)"
                         v-if="scope.row.orderStatus==3||scope.row.orderStatus==4||scope.row.orderStatus==5||scope.row.orderStatus==6"
                         type="text"
                         v-hasButtons="['ProcureInBack']"
                         style="color:#F3A900"
                         size="small">
                退货
              </el-button>
              <el-button @click="toFixed(scope.row)"
                         type="text"
                         v-if="scope.row.orderStatus == 1"
                         v-hasButtons="['ProcureInEdit']"
                         style="color:#F3A900"
                         size="small">
                {{'修改'}}
              </el-button>
              <el-button @click="handleDelete(scope.row.pkId)"
              v-hasButtons="['ProcureInDel']"
                         type="text"
                         size="small"
                         v-if="scope.row.orderStatus == 1"
                         style="color: #C8161D">
                {{'删除'}}
              </el-button>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="maintable">
        <el-table :data="tableDataDetails"
        v-loading="loading2"
                  style="width: 100%"
                  height="350px"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  :row-class-name="tableRowClassName">
          <!-- <el-table-column type="selection"
                           width="55"> </el-table-column> -->
          <el-table-column align="center"
                           prop="productCode"
                           label="产品编号">
          </el-table-column>

          <el-table-column align="center"
                           prop="productName"
                           label="产品名称">
          </el-table-column>
          <el-table-column align="center"
                           prop="specsName"
                           label="产品规格">
          </el-table-column>
          <el-table-column align="center"
                           prop="unitName"
                           label="单位">
          </el-table-column>
          <el-table-column align="center"
                           prop="storehouseName"
                           label="仓库">
          </el-table-column>
          <el-table-column align="center"
                           prop="nnum"
                           label="数量">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigprice"
                           label="无税单价">
          </el-table-column>
          <el-table-column align="center"
                           prop="ntaxrate"
                           label="税率">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigtaxprice"
                           label="含税单价">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigmny"
                           label="无税金额">
          </el-table-column>
          <el-table-column align="center"
                           prop="ntax"
                           label="税额">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigtaxmny"
                           label="价税合计">
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="naccumstorenum"
                           label="累计入库数量">
          </el-table-column>
          <el-table-column align="center"
                           prop="nbackarrvnum"
                           label="累计退货数量">
          </el-table-column> -->
          <el-table-column align="center"
                           prop="batchCode"
                           label="批次号">
          </el-table-column>
          <el-table-column align="center"
                           prop="isGiftVal"
                           label="赠品">
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getDataList" />
    <el-dialog title="参照"
               :visible.sync="dialogVisible"
               width="60%"
               top="5%"
               :before-close="handleClose">
      <el-form ref="obj"
               :model="obj"
               label-width="auto">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="订单编号">
              <el-input clearable
                        v-model="obj.orderCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商">
              <el-select clearable
                         v-model="obj.pkCubasdoc"
                         :placeholder="'请选择'">
                <el-option v-for="(item, index) in supplierList"
                           :key="index"
                           :label="item.custName"
                           :value="item.pkId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购公司">
              <el-select clearable
                           v-model="obj.pkCorp"
                           :placeholder="'请选择'">
                  <el-option v-for="(item, index) in corpList"
                             :key="index"
                             :label="item.corpName"
                             :value="item.pkId"></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary"
                       @click="getDigList"> {{ '搜索' }}</el-button>
            <el-button type=""
                       @click="resetDig"> {{ '重置' }}</el-button>
          </el-col>
        </el-row>
      </el-form>
      <div class="maintable">
        <el-table :data="tableObj"
                  style="width: 100%"
                  height="250px"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  ref="multipleTable0"
                  :row-class-name="tableRowClassName"
                  @row-click="selectRowObj"
                  @select="handleSelectionChange1">
          <el-table-column type="selection"
                           width="55"> </el-table-column>
          <el-table-column align="center"
                           prop="orderCode"
                           label="订单编号">
          </el-table-column>

          <el-table-column align="center"
                           prop="managerName"
                           label="管理员">
          </el-table-column>
          <el-table-column align="center"
                           prop="cubasdocName"
                           label="供应商">
          </el-table-column>
          <el-table-column align="center"
                           prop="corpName"
                           label="采购公司">
          </el-table-column>
          <el-table-column align="center"
                           prop="deptName"
                           label="采购部门">
          </el-table-column>
          <el-table-column align="center"
                           prop="totalNum"
                           label="总数量">
          </el-table-column>
          <el-table-column align="center"
                           prop="orderStatusVal"
                           label="订单状态">
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="printStatus"
                           label="打印状态">
          </el-table-column> -->
          <el-table-column align="center"
                           prop="remark"
                           label="备注">
          </el-table-column>
          <el-table-column align="center"
                           prop="orderDate"
                           label="入库日期">
          </el-table-column>
        </el-table>
      </div>
      <div class="maintable">
        <el-table :data="tableObjDetails"
                  style="width: 100%"
                  height="300px"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  ref="multipleTable"
                  :row-class-name="tableRowClassName"
                  @select="handleSelectionChange2">
          <el-table-column type="selection"
                           width="55"> </el-table-column>
          <el-table-column align="center"
                           prop="productCode"
                           label="产品编号">
          </el-table-column>

          <el-table-column align="center"
                           prop="productName"
                           label="产品名称">
          </el-table-column>
          <el-table-column align="center"
                           prop="specsName"
                           label="产品规格">
          </el-table-column>
          <el-table-column align="center"
                           prop="unitName"
                           label="单位">
          </el-table-column>
          <el-table-column align="center"
                           prop="storehouseName"
                           label="仓库">
          </el-table-column>
          <el-table-column align="center"
                           prop="nnum"
                           label="数量">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nnum"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="norigprice"
                           label="无税单价">
          </el-table-column>
          <el-table-column align="center"
                           prop="ntaxrate"
                           label="税率">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigtaxprice"
                           label="含税单价">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigmny"
                           label="无税金额">
          </el-table-column>
          <el-table-column align="center"
                           prop="ntax"
                           label="税额">
          </el-table-column>
          <el-table-column align="center"
                           prop="norigtaxmny"
                           label="价税合计">
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="naccumstorenum"
                           label="累计入库数量">
          </el-table-column>
          <el-table-column align="center"
                           prop="nbackarrvnum"
                           label="累计退货数量">
          </el-table-column> -->
          <!-- <el-table-column align="center"
                           prop="planDate"
                           label="批次号">
          </el-table-column> -->
          <el-table-column align="center"
                           prop="isGiftVal"
                           label="赠品">
          </el-table-column>
        </el-table>
      </div>
      <pagination v-show="totalObj>0"
                  :total="totalObj"
                  :page.sync="queryParamsObj.pageNum"
                  :limit.sync="queryParamsObj.pageSize"
                  @pagination="getDigList" />
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="goDetails">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="退货"
               :visible.sync="dialogVisible1"
               width="30%">
      <el-input type="textarea"
                :rows="4"
                placeholder="请输入退货原因"
                v-model="returnReason">
      </el-input>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary"
                   @click="goBack">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import * as api from '@/api/supply.js'
import * as del from "@/api/delivery.js";

export default {
  name: 'Bzpz',
  components: {
    topBar,
  },
  data() {
    return {
      obj: {
      
      },
      select: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      addOrEdit: '',
      total: 0,
      queryParamsObj: {
        pageNum: 1,
        pageSize: 50,
      },
      totalObj: 0,
      dialogVisible: false,
      tableData: [],
      tableDataDetails: [],
      loading:false,
      loading2:false,
      tableObj: [],
      tableObjDetails: [],
      supplierList: [],
      corpList:[],
      moren: 'procureIn',
      topList: [
        {
          name: '采购入库',
          path: 'procureIn',
        },
      ],
      selRow: [],
      orderStatusList:[],
      orderDate:[],
      backId:'',
      dialogVisible1:false,
      returnReason:''
    }
  },
  mounted() {
    // 获取列表
    this.getDataList()
    this.getData()
  },
  methods: {
    // storeList
    goQc() {
      this.$router.push({
        path: 'procureInZxqc',
        query: {
          data: JSON.stringify(this.selRow[0]),
          index: 801,
        },
      })
    },
    getData() {
      api.supplierList().then((res) => {
        this.supplierList = res.rows
      })
      api.corpList().then((res) => {
        this.corpList = res.rows
      })
      api.getOrderStatus().then((res) => {
        this.orderStatusList = res.data;
      });
    },
    getTime(val){
      this.select.startDate = val?val[0]:'';
      this.select.endDate = val?val[1]:'';
    },
    reset() {
      this.select = {}

      this.orderDate=[]
      this.getDataList()
    },
    resetDig() {
      this.obj = {}
    },
    goBack() {
      api
        .purOutBack({
          pkId: this.backId,
          returnReason: this.returnReason,
        })
        .then((res) => {
          if (res.code == 200) {
            this.dialogVisible1 = false
            this.$message({
              message: res.msg,
              type: 'success',
            })
            this.getDataList()
          }
        }).catch((err) => {
          this.$message({
              message: err.msg,
              type: 'warning',
            })
        })
    },
     // 点击修改
     toBack(row) {
      // this.backId = row.pkId
      // this.dialogVisible1 = true
      this.$router.push({
        path: 'procureInDetails',
        query: {
          data: JSON.stringify([row]),
          isBack:1
        },
      })
    },
    // 点击修改
    toFixed(row) {
      this.$router.push({
        path: 'procureInDetails',
        query: {
          data: JSON.stringify([row]),
          isBack:0
        },
      })
    },
    selectRow(row) {
      this.tableDataDetails = row.icPurchaseInbList
    },

    handleClose() {
      this.dialogVisible = false
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        api.purchaseDel(id).then((res) => {
          this.getDataList()
        })
      })
    },
    openDig() {
      this.dialogVisible = true
    },
    getDataList() {
      for (let key in this.select) {
        if (this.select[key] == '') {
          delete this.select[key]
        }
      }
      this.loading = true
      this.loading2 = true
      api
        .purchaseOrderList(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          this.tableData = res.rows
          // this.tableDataDetails = res.rows[0].icPurchaseInbList
          if(res.rows.length>0){
            this.tableDataDetails = res.rows[0].icPurchaseInbList
          }else{
            this.tableDataDetails = []
          }
          this.total = res.total
          this.loading = false
          this.loading2 = false
        })
    },
    getDigList() {
      api
        .storeList(Object.assign({}, this.queryParamsObj, this.obj))
        .then((res) => {
          res.rows.forEach((item) => {
            item.checkEd = false
            item.scmReturnbVoList.forEach((ctem) => {
              ctem.checkEd = false
            })
          })
          this.tableObj = res.rows
          this.tableObjDetails = res.rows[0].scmReturnbVoList
          this.totalObj = res.total
        })
    },
    handleSelectionChange(val) {
      this.selRow = val
    },
    goDetails() {
      let a = this.tableObj.some((item) => item.checkEd == true)
      let b = false
      this.tableObj.forEach((item) => {
        item.scmReturnbVoList.forEach((ctem) => {
          if (ctem.checkEd == true) {
            b = true
          }
        })
      })
      // 订单和产品必选一个
      if (a & b) {
        // 过滤未选择的
        let arr = this.tableObjitem
        arr = this.removeElement(this.tableObj)
        arr.forEach((item) => {
          item.scmReturnbVoList = this.removeElement(item.scmReturnbVoList)
        })
        arr.forEach((item) => {
          item.scmReturnbVoList.forEach((ctem) => {
            ctem.pkPoOrder = item.pkId
            ctem.pkPoOrderItems = ctem.pkId
            ctem.bizDate = ''
            ctem.pkBatchCode = ''
          })
        })
        let childArr = []
        arr.forEach((item) => {
          item.scmReturnbVoList.forEach((ctem) => {
            childArr.push(ctem)
          })
        })
        arr[0].childArr = childArr
        // 跳转
        this.$router.push({
          path: 'procureInDetails',
          query: {
            data: JSON.stringify(arr),
            addOrEdit: true,
          },
        })
      } else {
        this.$message({
          message: '请勾选订单及产品',
          type: 'warning',
        })
      }
    },
    // 移除指定数组
    removeElement(arr) {
      return arr.filter((item) => item.checkEd !== false)
    },
    selectRowObj(row) {
      this.tableObjDetails = row.scmReturnbVoList
      this.$nextTick(() => {
        this.tableObjDetails.forEach((item) => {
          if (item.checkEd) {
            this.$refs.multipleTable.toggleRowSelection(item, true)
          }
        })
      })
    },
    handleSelectionChange1(val, row) {
      this.tableObjDetails = row.scmReturnbVoList
      let a = val.some((item) => item.pkId == row.pkId)
      if (a) {
        this.tableObj.forEach((item) => {
          if (item.pkId == row.pkId) {
            this.$set(item, 'checkEd', true)
            item.scmReturnbVoList.forEach((ctem) => {
              this.$set(ctem, 'checkEd', true)
            })
          }
        })
      } else {
        this.tableObj.forEach((item) => {
          if (item.pkId == row.pkId) {
            this.$set(item, 'checkEd', false)
            item.scmReturnbVoList.forEach((ctem) => {
              this.$set(ctem, 'checkEd', false)
            })
          }
        })
      }
      this.$nextTick(() => {
        this.tableObjDetails.forEach((item) => {
          if (item.checkEd) {
            this.$refs.multipleTable.toggleRowSelection(item, true)
          } else {
            this.$refs.multipleTable.toggleRowSelection(item, false)
          }
        })
      })
    },
    handleSelectionChange2(val, row) {
      let a = val.some((item) => item.pkId == row.pkId)
      if (a) {
        this.tableObj.forEach((item) => {
          item.scmReturnbVoList.forEach((ctem) => {
            if (ctem.pkId == row.pkId) {
              this.$set(ctem, 'checkEd', true)
              this.$set(item, 'checkEd', true)
            }
          })
        })
      } else {
        this.tableObj.forEach((item) => {
          item.scmReturnbVoList.forEach((ctem) => {
            if (ctem.pkId == row.pkId) {
              this.$set(ctem, 'checkEd', false)
            }
          })
        })
      }
      this.$nextTick(() => {
        this.tableObj.forEach((item) => {
          if (item.checkEd) {
            this.$refs.multipleTable0.toggleRowSelection(item, true)
          }
        })
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    },
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .form_all {
      padding: 0px 20px 0 20px;
      // margin-bottom: 20px;
      background: #fff;
      border-radius: 8px;
    }
    .maintop {
      display: flex;
      padding: 0px 0px;
      justify-content: space-between;
      align-items: center;

      background: #f8f8f8;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  text-align: right;
  margin-right: 10px;
  color: #3181e5;
}
.dizhi {
  ::v-deep .el-cascader {
    width: 100%;
  }
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto;
}
::v-deep .el-table .el-table__header-wrapper .el-checkbox {
  display: none;
}
</style>
