<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店名称" prop="shopSn">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入门店名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="商户编号" prop="merchSn">-->
<!--        <el-input-->
<!--          v-model="queryParams.merchSn"-->
<!--          placeholder="请输入商户编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="商户名称" prop="merchName">-->
<!--        <el-input-->
<!--          v-model="queryParams.merchName"-->
<!--          placeholder="请输入商户名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="第三方商户代码" prop="thirdPartyMerchCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.thirdPartyMerchCode"-->
<!--          placeholder="请输入第三方商户代码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="第三方商户名称" prop="thirdPartyMerchName">-->
<!--        <el-input-->
<!--          v-model="queryParams.thirdPartyMerchName"-->
<!--          placeholder="请输入第三方商户名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="SKU" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入SKU"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="海关代码" prop="cusCode">-->
<!--        <el-select v-model="queryParams.cusCode" placeholder="请选择海关代码" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in cusCodeOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="保税仓代码" prop="bondedCode">-->
<!--        <el-select v-model="queryParams.bondedCode" placeholder="请选择保税仓代码" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in bondedCodeOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="仓库编号" prop="wareSn">-->
<!--        <el-input-->
<!--          v-model="queryParams.wareSn"-->
<!--          placeholder="请输入仓库编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="货品业务类型" prop="goodsBizType">-->
<!--        <el-select v-model="queryParams.goodsBizType" placeholder="请选择货品业务类型" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in goodsBizTypeOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否有效" prop="isValid">-->
<!--        <el-select v-model="queryParams.isValid" placeholder="请选择是否有效" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in isValidOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="cyan" 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"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['biz:shopinve:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['biz:shopinve:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['biz:shopinve:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:shopinve:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handlePull"
          v-hasPermi="['biz:storeinvemng:pull']"
        >拉取</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopinveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="门店库存编号" align="center" prop="shopInveSn" />-->
      <el-table-column label="门店序号" align="center" prop="shopSn" />
      <el-table-column label="门店名称" align="center" prop="shopName" />
<!--      <el-table-column label="商户编号" align="center" prop="merchSn" />-->
<!--      <el-table-column label="商户名称" align="center" prop="merchName" />-->
<!--      <el-table-column label="第三方商户代码" align="center" prop="thirdPartyMerchCode" />-->
<!--      <el-table-column label="第三方商户名称" align="center" prop="thirdPartyMerchName" />-->
      <el-table-column label="SKU" align="center" prop="sku" />
<!--      <el-table-column label="海关代码" align="center" prop="cusCode" :formatter="cusCodeFormat" />-->
<!--      <el-table-column label="保税仓代码" align="center" prop="bondedCode" :formatter="bondedCodeFormat" />-->
<!--      <el-table-column label="仓库编号" align="center" prop="wareSn" />-->
<!--      <el-table-column label="货品业务类型" align="center" prop="goodsBizType" :formatter="goodsBizTypeFormat" />-->
      <el-table-column label="门店库存" align="center" prop="shopInve" />
      <el-table-column label="门店可用数" align="center" prop="shopValid" />
      <el-table-column label="门店冻结数" align="center" prop="shopFreezeNum" />
      <el-table-column label="门店破损数" align="center" prop="shopDamageNum" />
      <el-table-column label="门店灭失数" align="center" prop="shopDestroyLostNum" />
      <el-table-column label="门店过期数" align="center" prop="shopExpireNum" />
      <el-table-column label="样品展示冻结数" align="center" prop="shopSampleFreezeNum" />
      <el-table-column label="门店减扣数" align="center" prop="shopMinus" />
      <el-table-column label="门店增补数" align="center" prop="shopSupp" />
      <el-table-column label="进场数量" align="center" prop="inQty" />
      <el-table-column label="出场数量" align="center" prop="outQty" />
      <el-table-column label="退货数量" align="center" prop="returnQty" />
      <el-table-column label="销售数量" align="center" prop="saleQty" />
      <el-table-column label="移仓转入数量" align="center" prop="locInQty" />
      <el-table-column label="移仓转出数量" align="center" prop="locOutQty" />
<!--      <el-table-column label="是否有效" align="center" prop="isValid" :formatter="isValidFormat" />-->
<!--      <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="['biz:shopinve:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['biz:shopinve: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-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门店序号" prop="shopSn">
          <el-input v-model="form.shopSn" placeholder="请输入门店序号" />
        </el-form-item>
        <el-form-item label="商户编号" prop="merchSn">
          <el-input v-model="form.merchSn" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="商户名称" prop="merchName">
          <el-input v-model="form.merchName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="第三方商户代码" prop="thirdPartyMerchCode">
          <el-input v-model="form.thirdPartyMerchCode" placeholder="请输入第三方商户代码" />
        </el-form-item>
        <el-form-item label="第三方商户名称" prop="thirdPartyMerchName">
          <el-input v-model="form.thirdPartyMerchName" placeholder="请输入第三方商户名称" />
        </el-form-item>
        <el-form-item label="SKU" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入SKU" />
        </el-form-item>
        <el-form-item label="海关代码" prop="cusCode">
          <el-select v-model="form.cusCode" placeholder="请选择海关代码">
            <el-option
              v-for="dict in cusCodeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保税仓代码" prop="bondedCode">
          <el-select v-model="form.bondedCode" placeholder="请选择保税仓代码">
            <el-option
              v-for="dict in bondedCodeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库编号" prop="wareSn">
          <el-input v-model="form.wareSn" placeholder="请输入仓库编号" />
        </el-form-item>
        <el-form-item label="货品业务类型" prop="goodsBizType">
          <el-select v-model="form.goodsBizType" placeholder="请选择货品业务类型">
            <el-option
              v-for="dict in goodsBizTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </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 { listShopinve, getShopinve, delShopinve, addShopinve, updateShopinve, exportShopinve,pullStoreInvemng } from "@/api/warehouse/shopinve";
import {pullInvemng} from "@/api/warehouse/invemng";

export default {
  name: "Shopinve",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户店面库存，包括跨境，一般贸易表格数据
      shopinveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 海关代码字典
      cusCodeOptions: [],
      // 保税仓代码字典
      bondedCodeOptions: [],
      // 货品业务类型字典
      goodsBizTypeOptions: [],
      // 是否有效字典
      isValidOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopSn: null,
        merchSn: null,
        merchName: null,
        thirdPartyMerchCode: null,
        thirdPartyMerchName: null,
        sku: null,
        cusCode: null,
        bondedCode: null,
        wareSn: null,
        goodsBizType: null,
        isValid: null,
        shopName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("cus_code").then(response => {
      this.cusCodeOptions = response.data;
    });
    this.getDicts("bonded_code").then(response => {
      this.bondedCodeOptions = response.data;
    });
    this.getDicts("goods_biz_type").then(response => {
      this.goodsBizTypeOptions = response.data;
    });
    this.getDicts("valid").then(response => {
      this.isValidOptions = response.data;
    });
  },
  methods: {
    /** 查询商户店面库存，包括跨境，一般贸易列表 */
    getList() {
      this.loading = true;
      listShopinve(this.queryParams).then(response => {
        this.shopinveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 海关代码字典翻译
    cusCodeFormat(row, column) {
      return this.selectDictLabel(this.cusCodeOptions, row.cusCode);
    },
    // 保税仓代码字典翻译
    bondedCodeFormat(row, column) {
      return this.selectDictLabel(this.bondedCodeOptions, row.bondedCode);
    },
    // 货品业务类型字典翻译
    goodsBizTypeFormat(row, column) {
      return this.selectDictLabel(this.goodsBizTypeOptions, row.goodsBizType);
    },
    // 是否有效字典翻译
    isValidFormat(row, column) {
      return this.selectDictLabel(this.isValidOptions, row.isValid);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopInveSn: null,
        shopSn: null,
        merchSn: null,
        merchName: null,
        thirdPartyMerchCode: null,
        thirdPartyMerchName: null,
        sku: null,
        cusCode: null,
        bondedCode: null,
        wareSn: null,
        goodsBizType: null,
        shopInve: null,
        shopValid: null,
        shopFreezeNum: null,
        shopDamageNum: null,
        shopDestroyLostNum: null,
        shopExpireNum: null,
        shopSampleFreezeNum: null,
        shopMinus: null,
        shopSupp: null,
        inQty: null,
        outQty: null,
        returnQty: null,
        saleQty: null,
        locInQty: null,
        locOutQty: null,
        isValid: null,
        createrSn: null,
        createTime: null,
        moderSn: null,
        modTime: null,
        tstm: 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.shopInveSn)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户店面库存，包括跨境，一般贸易";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopInveSn = row.shopInveSn || this.ids
      getShopinve(shopInveSn).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户店面库存，包括跨境，一般贸易";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shopInveSn != null) {
            updateShopinve(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShopinve(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopInveSns = row.shopInveSn || this.ids;
      this.$confirm('是否确认删除商户店面库存，包括跨境，一般贸易编号为"' + shopInveSns + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delShopinve(shopInveSns);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商户店面库存?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportShopinve(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    /** 拉取按钮操作 */
    handlePull() {
      const queryParams = this.queryParams;
      // 添加遮罩
      const _this=this
      this.$confirm('是否确认拉取保税仓所有库存?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        const loading = _this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        return pullStoreInvemng(queryParams,loading,_this);
      })
    }
  }
};
</script>
