<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel" class="toolPanel">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <div class="toolTitle">物料盘点</div>
        </el-col>
        <el-col :span="1.5">
          <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button plain icon="el-icon-delete" size="mini" :disabled="single" @click="handleDelete">删除</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
        </el-col>
        <right-toolbar v-if="rightToolbarShow" :showSearch.sync="showSearch" @queryTable="getList"
          showColumnsType="transfer" :columns="columns" :businessCode="businessCodeTrk"
          :tableNo="tableNo"></right-toolbar>
      </el-row>
    </div>
    <div class="search-container" ref="searchPanel">
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="仓库编号" prop="storageId">
          <el-select v-model="queryParams.storageId" placeholder="请选择仓库">
            <el-option v-for="item in options" :key="item.storageId" :label="item.storageName" :value="item.storageId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库区编号" prop="storageAreaId">
          <el-input v-model="queryParams.storageAreaId" placeholder="请输入库区编号" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="盘点单号" prop="panId">
          <el-input v-model="queryParams.panId" placeholder="请输入盘点单号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <div ref="mainTablePanel" :style="{ height: mainTableDivHeight }">
        <el-table v-loading="loading" border size="mini" :height="mainTableHeight" :data="panOrderList"
          @selection-change="handleSelectionChange" @row-dblclick="editHandler">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="盘点单号" width="120" align="center" prop="panId" />
          <el-table-column label="仓库编号" align="center" prop="storageId" />

          <el-table-column label="库区编号" align="center" prop="storageAreaId" />

          <el-table-column label="状态" align="center" prop="status" width="100">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pan_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="确认状态" align="center" prop="confirmStatus" width="100">

            <template slot-scope="scope">
              <dict-tag :options="dict.type.is_audit" :value="scope.row.confirmStatus" />
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="createBy" width="120" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180" :formatter="dateFormat">
          </el-table-column>
        </el-table>
        <div class="pagePanel">
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </div>
    </div>
    <detail :visible="showDetail" :title="title" :record="record" :action="action" @close="detailClose"></detail>
  </div>
</template>

<script>
import { listCOLUMN } from "@/api/system/column";
import {
  addPanOrder,
  delPanOrder,
  getPanOrder,
  getWarehouse,
  importCurrStock,
  listPanOrder,
  updatePanOrder
} from "@/api/system/panOrder";
import DictData from "@/components/DictData";
import detail from "./detail";
DictData.install();
export default {
  name: "Pan",
  components: { detail },
  dicts: ["pan_status", "is_audit"],
  data () {
    return {
      action: "new",
      record: null,
      options: null,
      showDetail: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘点单表格数据
      panOrderList: [],

      // 弹出层标题
      title: "",
      mainTableHeight: "0px",
      mainTableDivHeight: "0px",
      mainTableHeight1: "0px",
      mainTableDivHeight1: "0px",
      businessCodeTrk: "pan",
      rightToolbarShow: false,
      tableNo: 0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "createTime",
        isAsc: "descending",
        panId: null,
        orderId: null,
        storageId: null,
        storageAreaId: null,
        total: null,
        muchNum: null,
        lessNum: null,
        justNum: null,
        status: null,
        auditMan: null,
        auditDate: null,
        confirmStatus: null,
        recordPerson: null,
        recordDate: null,
        taskId: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        panId: [
          { required: true, message: "盘点单号不能为空", trigger: "blur" },
        ],
      },
      columns: [
        {
          key: 4,
          label: `库区编号`,
          columnName: "storage_area_id",
        },
        {
          key: 13,
          label: `确认状态`,
          columnName: "confirm_status",
        },
        {
          key: 14,
          label: `操作人`,
          columnName: "record_person",
        },
        {
          key: 15,
          label: `操作时间`,
          columnName: "record_date",
        },
        {
          key: 17,
          label: `创建时间`,
          columnName: "create_time",
        },
        {
          key: 18,
          label: `更新时间`,
          columnName: "update_time",
        },
      ],
    };
  },
  created () {
    this.getList();
    this.getWarehouse();
  },
  mounted () {
    this.computedTableHeight();
  },
  watch: {
    showSearch: function () {
      this.computedTableHeight();
    },
  },
  methods: {
    dateFormat (row) {
      return row.createTime;
    },
    editHandler (row) {
      this.showDetail = true;
      this.record = row;
      this.title = "物料盘点";
      this.action = "edit";
    },
    detailClose () {
      this.showDetail = false;
      this.getList();
    },
    importCurrentStock () {
      importCurrStock({
        storageId: this.form.storageId,
        storageAreaId: this.form.storageAreaId,
      }).then((res) => {
        this.panDetailList = res.rows;
        this.total1 = res.total;
        console.log(res);
      });
    },
    /** 查询用户展示字段定义管理列表 */
    getHideColumn () {
      var queryHideColumnParams = {
        pageNum: 1,
        pageSize: 100,
        businessCode: this.businessCodeTrk,
        tableNo: this.tableNo,
      };

      listCOLUMN(queryHideColumnParams).then((response) => {
        if (response.rows.length > 0) {
          var row = response.rows[0];
          var columnHide = row.columnHide.split(",");
          if (columnHide.length > 0) {
            for (let item in this.columns) {
              if (columnHide.indexOf(this.columns[item].columnName) > -1) {
              }
            }
          }
        }
        this.rightToolbarShow = true;
      });
    },
    computedTableHeight () {
      this.$nextTick(() => {
        var totalHeight = this.$refs.appPanel.clientHeight;
        var toolHeight = this.$refs.toolPanel.clientHeight;
        var searchHeight = this.$refs.searchPanel.clientHeight;
        var tablePanelHeight = totalHeight - toolHeight - searchHeight - 14 - 5;
        this.mainTableDivHeight = tablePanelHeight + "px";
        this.mainTableHeight = tablePanelHeight - 25 + "px";
      });
    },
    /** 查询盘点单列表 */
    getList () {
      this.loading = true;
      listPanOrder(this.queryParams).then((response) => {
        this.panOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
        // this.getHideColumn();
      });
    },
    /**
     * 获取当前用户有权操作的仓库列表
     */
    getWarehouse () {
      getWarehouse({}).then((response) => {
        this.options = response.data;
      });
    },

    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        panId: null,
        orderId: null,
        storageId: null,
        storageAreaId: null,
        total: null,
        muchNum: null,
        lessNum: null,
        justNum: null,
        status: "0",
        createBy: null,
        auditMan: null,
        auditDate: null,
        confirmStatus: "0",
        recordPerson: null,
        recordDate: null,
        taskId: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);

      if (selection.length == 1) {
        if (selection[0].status == "0") this.single = false;
        else this.single = true;
      } else this.single = true;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.showDetail = true;
      this.action = "new";
      this.title = "创建盘点单";
      // this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      getPanOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改盘点单";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePanOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPanOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除盘点单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPanOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },

    /** 用户状态修改 */
    handleStatusChange (row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '"id为"' + row.id + '"数据项？')
        .then(function () {
          return changeStatus(row.id, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 启用按钮操作 */
    handleEnable (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认启用盘点单编号为"' + ids + '"的数据项？')
        .then(function () {
          return enabled(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        })
        .catch(() => { });
    },
    /** 禁用按钮操作 */
    handleDisabled (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认禁用盘点单编号为"' + ids + '"的数据项？')
        .then(function () {
          return disabled(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("禁用成功");
        })
        .catch(() => { });
    },

    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "system/pan/export",
        {
          ...this.queryParams,
        },
        `pan_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.app-container {
  height: calc(100vh - 84px);
}
</style>
