<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" :inline="true">
        <el-form-item class="query-form-item" label="盘点单号:">
          <el-input v-model="filterQuery.orderId" placeholder="请输入盘点单号"
                    clearable="true"></el-input>
        </el-form-item>
        <el-form-item class="query-form-item" label="领用仓库:">
          <el-select v-model="filterQuery.invCode" placeholder="请选择仓库" clearable="true"
                     @change="invChange"
          >
            <el-option
              v-for="item in invList"
              :key="item.name"
              :label="item.name"
              :value="item.code">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="query-form-item" label="领用货位:">
          <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择货位" clearable="true"
          >
            <el-option
              v-for="item in spaceList"
              :key="item.name"
              :label="item.name"
              :value="item.code">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group style="margin-left: 10px;display:flex;">
            <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="addCountOrder(0)">新增整库盘点</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="addCountOrder(1)">新增货位盘点</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="addCountOrder(2)">新增部分盘点</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="盘点单号" prop="orderId" width="300"></el-table-column>
        <el-table-column label="仓库" prop="invName" width="300"></el-table-column>
        <el-table-column label="货位" prop="invSpaceName" width="300"></el-table-column>
        <el-table-column label="盘点类型" prop="countType" width="150">
          <template slot-scope="scope">
            <span>{{ countTypeMap[scope.row.countType] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="盘点人" prop="createUser" width="200"></el-table-column>
        <el-table-column label="盘点时间" prop="createTime"></el-table-column>
        <el-table-column label="状态" prop="status" width="120">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="printOrder(scope.row)"
            >打印
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="editOrder(scope.row)"
            >编辑
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="submitAudit(scope.row)"
            >提交审核
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteDialog(scope.row.id)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

    <el-dialog
      :title="formMap[formName]"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="85%"
      v-if="formVisible"
    >
      <invCountOrderModify
        :closeDialog="closeDialog"
        :countOrder="countOrder"
        :getCountOrderList="getList"
        :countType="countType"
      ></invCountOrderModify>
    </el-dialog>

  </div>
</template>

<script>
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {
  getCountOrderList,
  deleteCountOrder,
  submitAudit,
  verifyTemplateFile,
  printOrder
} from "@/api/inventory/invCountOrder";
import invCountOrderModify from "@/views/inventory/InvCountOrderModify.vue";

export default {
  name: "InvCountOrderNew",
  data() {
    return {
      filterQuery: {
        orderId: null,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        status: 0,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      invList: [],
      spaceList: [],
      loading: false,
      formVisible: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      formName: null,
      formMap: {
        add_0: "新增整库盘点单",
        add_1: "新增货位盘点单",
        add_2: "新增部分盘点单",
        edit_0: "编辑整库盘点单",
        edit_1: "编辑货位盘点单",
        edit_2: "编辑部分盘点单"
      },
      countOrder: {},
      countType: null,
      countTypeMap: {
        0: "整库盘点",
        1: "货位盘点",
        2: "部分盘点"
      }
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        status: 0,
        page: 1,
        limit: 20,
      };
      this.spaceList = [];
      this.getList();
      this.getSpaceList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getCountOrderList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    invChange() {
      this.spaceList = [];
      this.filterQuery.fromInvSpaceCode = null;
      this.getSpaceList();
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
      this.getSpaceList();
    },
    getSpaceList() {
      let params = {invWarehouseCode: this.filterQuery.invCode, status: 1};
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
        this.getList();
      })
    },
    addCountOrder(type) {
      this.formVisible = true;
      this.formName = 'add_' + type;
      this.countOrder = {};
      this.countType = type;
    },
    printOrder(row) {
      let query = {moduleId: 6};
      this.loading = true;
      verifyTemplateFile(query).then((res) => {
        if (res.code === 20000) {
          let printParams = {
            templateId: res.data,
            orderId: row.orderId
          };
          printOrder(printParams).then((response) => {
            //将pdf文件转换为url。
            const binaryData = [];
            binaryData.push(response);
            //获取blob链接。
            let url = window.URL.createObjectURL(
              new Blob(binaryData, {type: "application/pdf"})
            );
            this.loading = false;
            window.open(url);//打开新标签页，预览pdf。
          })
        } else {
          this.loading = false;
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
      })
    },
    editOrder(row) {
      this.formVisible = true;
      this.formName = "edit_" + row.countType;
      this.countOrder = row;
    },
    submitAudit(row) {
      let params = {id: row.id};
      submitAudit(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("提交成功！");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该盘点单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: rowId};
        this.loading = true;
        deleteCountOrder(params).then((res) => {
          this.loading = false;
          if (res.code === 20000) {
            this.$message.success("删除成功！");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },
  },
  components: {
    invCountOrderModify
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getInvList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

