<template>
  <el-dialog
    id="dialog-container"
    :title="title"
    :visible.sync="open"
    width="1000px"
    height="480px"
    append-to-body
  >
    <div class="dialog-body" style="height: 430px; padding: 0px !important">
      <div
        class="search-container"
        ref="searchPanel"
        style="padding-left: 5px; padding-right: 5px"
      >
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="mini"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="物料编号" prop="goodsId">
            <el-input
              v-model="queryParams.goodsId"
              placeholder="请输入物料编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料名称" prop="goodsName">
            <el-input
              v-model="queryParams.goodsName"
              placeholder="请输入物料名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料简称" prop="goodsShortName">
            <el-input
              v-model="queryParams.goodsShortName"
              placeholder="请输入物料简称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="助记码" prop="pyName">
            <el-input
              v-model="queryParams.pyName"
              placeholder="请输入助记码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="条形码" prop="barcode">
            <el-input
              v-model="queryParams.barcode"
              placeholder="请输入条形码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料分类" prop="goodsTypeId">
            <!-- <el-input v-model="queryParams.goodsTypeId" placeholder="请输入物料分类" clearable @keyup.enter.native="handleQuery" /> -->
            <el-select
              v-model="queryParams.goodsTypeId"
              filterable
              placeholder="请选择物料单位"
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in dict.type.goods_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="供应商编号" prop="providerId">
            <el-input
              v-model="queryParams.providerId"
              placeholder="请输入供应商编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" size="mini" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >刷新</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="table-container">
        <div ref="mainTablePanel">
          <el-table
            ref="goodsTable"
            v-loading="loading"
            border
            size="mini"
            height="300"
            :data="goodsList"
            @selection-change="handleSelectionChange"
            @row-click="handleClick"
            @row-dblclick="handleDblClick"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="物料编号" align="center" prop="goodsId" width="140"/>
            <el-table-column label="物料名称" align="center" prop="goodsName" width="150"/>
            <el-table-column
              label="物料简称"
              align="center"
              prop="goodsShortName"
            />
            <el-table-column label="助记码" align="center" prop="pyName" />
            <el-table-column label="条形码" align="center" prop="barcode" width="100"/>
            <el-table-column label="分类" align="center" prop="goodsTypeId" />
            <el-table-column label="规格" align="center" prop="specification" width="150"/>
            <el-table-column label="型号" align="center" prop="model" />
            <el-table-column label="物料描述" align="center" prop="recoil" />
            <el-table-column label="体积" align="center" prop="gvolume" />
            <el-table-column
              label="商品重量单位"
              align="center"
              prop="scaleUnit"
            />
            <el-table-column label="物料单位" align="center" prop="unit" />
            <el-table-column label="库存上限" align="center" prop="warnUp" />
            <el-table-column label="库存下限" align="center" prop="warnDown" />
            <el-table-column label="产地" align="center" prop="produceArea" />
            <el-table-column label="毛重" align="center" prop="grossWeight" />
            <el-table-column label="重量" align="center" prop="netWeight" />
            <el-table-column
              label="供应商编号"
              align="center"
              prop="providerId"
            />
            <el-table-column label="海关编码 " align="center" prop="hsnumber" />
            <el-table-column label="ABC分类" align="center" prop="abc" />
            <el-table-column label="危险品" align="center" prop="hazardous" />
            <el-table-column
              label="存储条件"
              align="center"
              prop="storageConditions"
            />
            <el-table-column label="增值税" align="center" prop="addtax" />
            <el-table-column label="季节" align="center" prop="season" />
            <el-table-column
              label="关税税率"
              align="center"
              prop="tariffrate"
            />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.common_status"
                  :value="scope.row.status"
                />
              </template>
            </el-table-column>
            <el-table-column label="图片" align="center" prop="img" />
            <el-table-column
              label="是否是批号管理"
              align="center"
              prop="scFlag"
            >
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.sys_yes_no"
                  :value="scope.row.scFlag"
                />
              </template>
            </el-table-column>
            <el-table-column label="计划金额" align="center" prop="costPrice" />
            <el-table-column label="中标方" align="center" prop="customerId" />
            <el-table-column label="是否开放" align="center" prop="isOpen">
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.sys_yes_no"
                  :value="scope.row.isOpen"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="默认存储库区"
              align="center"
              prop="defaultStorageAreaId"
            />
            <el-table-column
              label="物料属性"
              align="center"
              prop="materialCharacteristics"
            />
            <el-table-column
              label="默认存储仓库"
              align="center"
              prop="defaultStorageId"
            />
            <el-table-column label="所属仓库" align="center" prop="storageId" />
            <el-table-column
              label="复检单生成提前天数"
              align="center"
              prop="reinspectionDays"
            />
          </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>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listGoods } from "@/api/system/goods";

export default {
  name: "GoodsSelect",
  dicts: [
    "sys_yes_no",
    "common_status",
    "goods_unit",
    "goods_type",
    "shelf_life_unit",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: false,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料表格数据
      goodsList: [],
      // 选择的物料
      selectItem: null,
      // 弹出层标题
      title: "选择物料",
      mainTableHeight: "0px",
      mainTableDivHeight: "0px",
      // 是否显示弹出层
      open: false,
      // 选择回调函数
      callBack: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        goodsId: null,
        goodsName: null,
        goodsShortName: null,
        pyName: null,
        barcode: null,
        goodsTypeId: null,
        specification: null,
        model: null,
        recoil: null,
        gvolume: null,
        warnUp: null,
        warnDown: null,
        produceArea: null,
        grossWeight: null,
        netWeight: null,
        providerId: null,
        hsnumber: null,
        abc: null,
        hazardous: null,
        storageConditions: null,
        addtax: null,
        season: null,
        tariffrate: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          { required: true, message: "物料编号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    /** 查询物料列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsName: null,
        goodsShortName: null,
        pyName: null,
        barcode: null,
        goodsTypeId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = [];

      if (Array.isArray(selection) && selection.length > 1) {
        this.$refs.goodsTable.toggleRowSelection(selection[0], false);
        this.$refs.goodsTable.toggleRowSelection(selection[1], true);
      }

      this.selectItem = selection[selection.length - 1];
    },
    /** 提交按钮 */
    submitForm() {
      if (this.selectItem == null) {
        this.$modal.console.error("请选择一个物料");
        return;
      }
      this.callBack(this.selectItem);
      this.open = false;
    },
    showModal(newCallBack) {
      this.open = true;
      this.callBack = newCallBack;
      this.getList();
    },
    handleDblClick(row) {
      this.callBack(row);
      this.open = false;
    },
    handleClick(row) {
      this.$refs.goodsTable.toggleRowSelection(row);
      this.selectItem = row;
    },
  },
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}

#dialog-container {
  .el-dialog__body {
    padding: 5px !important;
  }

  .pagination-container {
    background: none !important;
    margin-top: 5px !important;
    .el-pagination {
      left: 0;
    }
  }
}
</style>
