<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <el-card>
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
            <el-form-item label="库记录类型" prop="recordType" v-if="pageRecordType === undefined">
              <el-select v-model="queryParams.recordType" placeholder="请选择库记录类型" clearable size="small">
                <el-option
                  v-for="dict in dict.type.goods_storage_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="类型" prop="typeRoot">
              <el-select v-model="queryParams.typeRoot" placeholder="请选择一级分类" @change="getChild">
                <el-option
                  v-for="item in goodsTypeOptions0"
                  :key="item.id"
                  :label="item.goodsTypeName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="分类" prop="typeBranch">
              <el-select v-model="queryParams.typeBranch" placeholder="请选择二级分类">
                <el-option
                  v-for="item in goodsTypeOptions1"
                  :key="item.id"
                  :label="item.goodsTypeName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="编号" prop="number">
              <el-input
                v-model="queryParams.number"
                placeholder="请输入编号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="规格型号" prop="productModel">
              <el-input
                v-model="queryParams.productModel"
                placeholder="请输入规格型号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="库存数量" prop="quantityInStock">
              <el-input
                v-model="queryParams.quantityInStock"
                placeholder="请输入库存数量"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="单位" prop="unit">
              <el-input
                v-model="queryParams.unit"
                placeholder="请输入单位"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="平均单价" prop="averageUnitPrice">
              <el-input
                v-model="queryParams.averageUnitPrice"
                placeholder="请输入平均单价"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="使用状态" prop="useStatus">
              <el-select v-model="queryParams.useStatus" placeholder="请选择使用状态" clearable size="small">
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" 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>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['erms:storage:add']"
              >新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['erms:storage:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['erms:storage:remove']"
              >删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                :loading="exportLoading"
                @click="handleExport"
                v-hasPermi="['erms:storage:export']"
              >导出
              </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
          <el-table v-loading="loading" :data="storageList" @selection-change="handleSelectionChange"
                    @row-click="getGoodsRecordList">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="库记录类型" align="center" prop="recordType" v-if="pageRecordType === undefined">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.goods_storage_type" :value="scope.row.recordType"/>
              </template>
            </el-table-column>
            <el-table-column label="类型" align="center" prop="typeRoot">
              <template slot-scope="scope">
                <el-select v-model="scope.row.typeRoot" disabled>
                  <el-option
                    v-for="item in goodsTypeOptions0"
                    :key="item.id"
                    :label="item.goodsTypeName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="分类" align="center" prop="typeBranch">
              <template slot-scope="scope">
                <el-select v-model="scope.row.typeBranch" disabled>
                  <el-option
                    v-for="item in tableGoodsTypeOptions"
                    :key="item.id"
                    :label="item.goodsTypeName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="编号" align="center" prop="number"/>
            <el-table-column label="规格型号" align="center" prop="productModel"/>
            <el-table-column label="库存数量" align="center" prop="quantityInStock"/>
            <el-table-column label="单位" align="center" prop="unit"/>
            <el-table-column label="平均单价" align="center" prop="averageUnitPrice"/>
            <el-table-column label="使用状态" align="center" prop="useStatus">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.useStatus"/>
              </template>
            </el-table-column>
            <el-table-column label="说明" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['erms:storage:edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['erms:storage:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <goodsRecord ref="goodsRecord"></goodsRecord>
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改物资库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="库记录类型" prop="recordType" v-if="pageRecordType === undefined">
          <el-select v-model="form.recordType" placeholder="请选择库记录类型">
            <el-option
              v-for="dict in dict.type.goods_storage_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="typeRoot">
          <el-select v-model="form.typeRoot" placeholder="请选择一级分类" @change="getChild">
            <el-option
              v-for="item in goodsTypeOptions0"
              :key="item.id"
              :label="item.goodsTypeName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="typeBranch">
          <el-select v-model="form.typeBranch" placeholder="请选择二级分类">
            <el-option
              v-for="item in goodsTypeOptions1"
              :key="item.id"
              :label="item.goodsTypeName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入编号"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="规格型号" prop="productModel">
          <el-input v-model="form.productModel" placeholder="请输入规格型号"/>
        </el-form-item>
        <el-form-item label="库存数量" prop="quantityInStock">
          <el-input v-model="form.quantityInStock" placeholder="请输入库存数量"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="平均单价" prop="averageUnitPrice">
          <el-input v-model="form.averageUnitPrice" placeholder="请输入平均单价"/>
        </el-form-item>
        <el-form-item label="使用状态">
          <el-radio-group v-model="form.useStatus">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入说明"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listStorage, getStorage, delStorage, addStorage, updateStorage, exportStorage} from "@/api/erms/storage";
import {listGoodsType, typeListGoodsType} from "@/api/erms/goodsType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import goodsRecord from '@/views/erms/goodsRecord'

export default {
  name: "Storage",
  dicts: ['goods_storage_type', 'sys_normal_disable'],
  components: {
    Treeselect, goodsRecord
  },
  data() {
    return {
      goodsTypeOptions0: [],
      goodsTypeOptions1: [],
      tableGoodsTypeOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资库表格数据
      storageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordType: null,
        typeRoot: null,
        typeBranch: null,
        number: null,
        name: null,
        productModel: null,
        quantityInStock: null,
        unit: null,
        averageUnitPrice: null,
        useStatus: null,
      },
      pageRecordType: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.pageRecordType = this.$root._route.query.type
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询物资库列表 */
    getList() {
      this.loading = true;
      this.queryParams.recordType = this.pageRecordType
      typeListGoodsType('').then(response => {
        this.goodsTypeOptions0 = response.data
      });
      listGoodsType(null).then(response => {
        this.tableGoodsTypeOptions = response.data
      });
      listStorage(this.queryParams).then(response => {
        this.storageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getGoodsRecordList(row) {
      this.$refs.goodsRecord.queryParams.goodsId = row.id
      this.$refs.goodsRecord.getList()
    },
    /** 查询物资类库下拉树结构 */
    getChild(id) {
      this.goodsTypeOptions1 = []
      this.form.typeBranch = ''
      typeListGoodsType(this.form.typeRoot).then(response => {
        this.goodsTypeOptions1 = response.data
        this.$forceUpdate()
      });
    },
    /** 转换物资类库数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.goodsTypeName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recordType: null,
        typeRoot: null,
        typeBranch: null,
        number: null,
        name: null,
        productModel: null,
        quantityInStock: null,
        unit: null,
        averageUnitPrice: null,
        useStatus: "0",
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物资库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStorage(id).then(response => {
        this.form = response.data;
        typeListGoodsType(this.form.typeRoot).then(response => {
          this.goodsTypeOptions1 = response.data
          this.$forceUpdate()
        });
        this.open = true;
        this.title = "修改物资库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStorage(this.form).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorage(this.form).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物资库编号为"' + ids + '"的数据项？').then(function () {
        return delStorage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有物资库数据项？').then(() => {
        this.exportLoading = true;
        return exportStorage(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
