<template>
  <base-list>
    <template slot="n_header">
      <el-form v-model="pagerDto" inline>
        <el-form-item label="货位">
          <el-input v-model="pagerDto._Co_GridNumber" />
        </el-form-item>
        <el-form-item label="系统Sku">
          <el-input v-model="pagerDto._Co_SkuCode" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="pagerSerach(1)">搜索</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template slot="n_option">
      <div v-if="this.urlParam.op != 'Detail'">
        <label>总盘点：{{ urlParam.TaskNewInventoryCount }}</label
        >&nbsp;&nbsp; <label>总应有：{{ urlParam.TaskOldInventoryCount }}</label
        >&nbsp;&nbsp; <label>总盈亏：{{ urlParam.TaskProfit }}</label
        >&nbsp;&nbsp;
        <el-button v-if="this.urlParam.op == 'audit_40'" :loading="ConfirmLoading" @click="ConfirmNumber">
          确认数量
        </el-button>
        <el-button v-else-if="this.urlParam.op == 'audit_50'" :loading="ConfirmLoading" @click="ConfirmEnd">
          任务完成
        </el-button>
      </div>
      <div v-else>
        <label>货位盘点：{{ urlParam.GridNewInventoryCount }}</label
        >&nbsp;&nbsp;
        <label>货位应有：{{ urlParam.GridOldInventoryCount }}</label
        >&nbsp;&nbsp; <label>盈亏：{{ urlParam.GridProfit }}</label
        >&nbsp;&nbsp;
      </div>
    </template>
    <template>
      <el-table
        v-loading="dataLoading"
        :data="data"
        :highlight-current-row="true"
        :select-on-indeterminate="true"
        width="100%"
      >
        <el-table-column prop="SkuCode" label="系统SKU" />
        <el-table-column prop="SkuNo" label="客户SKU" />
        <el-table-column prop="GridNumber" label="货位" />
        <el-table-column prop="OldInventoryCount" label="应有数量" />
        <el-table-column prop="NewInventoryCount" label="盘点数量" />
        <el-table-column prop="GoodProfit" label="盈亏" />
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 40, 100, 400]"
        :current-page="pagerDto.PageIndex"
        :page-size.sync="pagerDto.PageSize"
        background
        layout="prev, pager, next ,total ,jumper,sizes,slot "
        :total="pagerDto.total"
        @size-change="changePager"
        @current-change="pagerSerach"
      />
    </template>
  </base-list>
</template>
<script>
import BaseList from "@/components/Xyy/baselist";
import { QueryDetailPager,EditInventoryStatus,ConfirmEnd } from "@/api/adminStock/InventoryTask";
export default {
  name: "InventoryTaskDetail",
  components: { "base-list": BaseList },
  data() {
    return {
      data: [],
      dataLoading: false,
      ConfirmLoading:false,
      urlParam: {},
      pagerDto: {
        PageIndex: 0,
        PageSize: 10,
        total: 0,
        QuerySort: [{ Field: "GridId", IsDesc: true }],
      },
    };
  },
  created() {},
  methods: {
    getParam(data) {
      this.pagerDto._Eq_TaskId = data.TaskId;
      this.urlParam.op = data.op;
      if (this.pagerDto._Eq_TaskId > 0) {
        this.pagerSerach(1);
        if (data.gridId > 0) {
          this.pagerDto._Co_GridNumber = data.gridId;
        }
      } else {
        return;
      }
    },
    ConfirmNumber() {
      this.ConfirmLoading=true;
      this.$confirm("是否确认当前盘点数据正确", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          EditInventoryStatus(this.pagerDto._Eq_TaskId,"countVerify").then(() => {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.$emit('refreshTable')
          });
        })
        .catch(() => {})
        .finally(()=>{
          this.ConfirmLoading=false;
        });
    },
    ConfirmEnd() {
      this.ConfirmLoading=true;
      this.$confirm("确认当前任务完成，将更新库存数据，是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          ConfirmEnd(this.pagerDto._Eq_TaskId).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.$emit('refreshTable')
          });
        })
        .catch(() => {})
        .finally(()=>{
          this.ConfirmLoading=false;
        });
    },
    //列表查询
    pagerSerach(index) {
      if (this.pagerDto._Eq_TaskId <= 0) {
        return;
      }
      this.dataLoading = true;
      if (index) {
        this.pagerDto.PageIndex = index;
      }
      QueryDetailPager(this.pagerDto)
        .then((res) => {
          this.data = res.Result;
          this.pagerDto.total = res.TotalCount;
          if (this.pagerDto.total > 0) {
            let GridOldInventoryCount = res.Result[0].GridOldInventoryCount;
            let GridNewInventoryCount = res.Result[0].GridNewInventoryCount;
            let GridProfit = res.Result[0].GridProfit;
            let TaskOldInventoryCount = res.Result[0].TaskOldInventoryCount;
            let TaskNewInventoryCount = res.Result[0].TaskNewInventoryCount;
            let TaskProfit = res.Result[0].TaskProfit;
            this.urlParam.GridOldInventoryCount =
              GridOldInventoryCount == null ? 0 : GridOldInventoryCount;
            this.urlParam.GridNewInventoryCount =
              GridNewInventoryCount == null ? 0 : GridNewInventoryCount;
            this.urlParam.GridProfit = GridProfit == null ? 0 : GridProfit;
            this.urlParam.TaskOldInventoryCount =
              TaskOldInventoryCount == null ? 0 : TaskOldInventoryCount;
            this.urlParam.TaskNewInventoryCount =
              TaskNewInventoryCount == null ? 0 : TaskNewInventoryCount;
            this.urlParam.TaskProfit = TaskProfit == null ? 0 : TaskProfit;
          }
        })
        .finally((res) => {
          this.dataLoading = false;
        });
    },
    changePager(v) {
      this.PageSize = v;
      this.pagerSerach();
    },
  },
};
</script>
