<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <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="inGoodsShipStatus">
        <el-select v-model="queryParams.inGoodsShipStatus" placeholder="请选择发货状态" clearable size="small">
          <el-option
            v-for="dict in inGoodsShipStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货类型" prop="deliveryType">
        <el-select v-model="queryParams.deliveryType" placeholder="请选择发货类型" clearable size="small">
          <el-option
            v-for="dict in deliveryTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="结转类型" prop="carryOverType">-->
<!--        <el-select v-model="queryParams.carryOverType" placeholder="请选择结转类型" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in carryOverTypeOptions"-->
<!--            :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>
        <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="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:adjustmentorder: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:adjustmentorder:pull']"
        >拉取结转记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          :disabled="single"
          @click="handlePullDetail"
          v-hasPermi="['biz:adjustmentorder:pulldetail']"
        >拉取结转明细</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adjustmentorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="入库调整单编号" align="center" prop="adjustmentOrderSn" />-->
      <el-table-column label="入库调整单编号" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/warehouse/adjustmentitem/index/' + scope.row.adjustmentOrderSn" class="link-type">
            <span>{{ scope.row.adjustmentOrderSn }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="商户名称" align="center" prop="merchName" />
      <el-table-column label="调整原因" align="center" prop="adjustmentReason" />
      <el-table-column label="发货状态" align="center" prop="inGoodsShipStatus" :formatter="inGoodsShipStatusFormat" />
      <el-table-column label="发货类型" align="center" prop="deliveryType" :formatter="deliveryTypeFormat" />
      <el-table-column label="结转类型" align="center" prop="carryOverType" :formatter="carryOverTypeFormat" />
    </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="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="inGoodsShipStatus">
          <el-select v-model="form.inGoodsShipStatus" placeholder="请选择发货状态">
            <el-option
              v-for="dict in inGoodsShipStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货类型" prop="deliveryType">
          <el-select v-model="form.deliveryType" placeholder="请选择发货类型">
            <el-option
              v-for="dict in deliveryTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结转类型" prop="carryOverType">
          <el-select v-model="form.carryOverType" placeholder="请选择结转类型">
            <el-option
              v-for="dict in carryOverTypeOptions"
              :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>
      <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 { listAdjustmentorder, getAdjustmentorder, delAdjustmentorder, addAdjustmentorder, updateAdjustmentorder, exportAdjustmentorder,pullAdjustmentorder,pullAdjustmentorderDetail } from "@/api/warehouse/adjustmentorder";

export default {
  name: "Adjustmentorder",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存内调结转出入库记录表格数据
      adjustmentorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发货状态字典
      inGoodsShipStatusOptions: [],
      // 发货类型字典
      deliveryTypeOptions: [],
      // 结转类型字典
      carryOverTypeOptions: [],
      // 保税仓代码字典
      bondedCodeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchSn: null,
        inGoodsShipStatus: null,
        deliveryType: null,
        carryOverType: null,
        bondedCode: null,
        wareSn: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("ship_status").then(response => {
      this.inGoodsShipStatusOptions = response.data;
    });
    this.getDicts("delivery_type").then(response => {
      this.deliveryTypeOptions = response.data;
    });
    this.getDicts("carry_over_type").then(response => {
      this.carryOverTypeOptions = response.data;
    });
    this.getDicts("bonded_code").then(response => {
      this.bondedCodeOptions = response.data;
    });
  },
  methods: {
    /** 查询库存内调结转出入库记录列表 */
    getList() {
      this.loading = true;
      listAdjustmentorder(this.queryParams).then(response => {
        this.adjustmentorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 发货状态字典翻译
    inGoodsShipStatusFormat(row, column) {
      return this.selectDictLabel(this.inGoodsShipStatusOptions, row.inGoodsShipStatus);
    },
    // 发货类型字典翻译
    deliveryTypeFormat(row, column) {
      return this.selectDictLabel(this.deliveryTypeOptions, row.deliveryType);
    },
    // 结转类型字典翻译
    carryOverTypeFormat(row, column) {
      return this.selectDictLabel(this.carryOverTypeOptions, row.carryOverType);
    },
    // 保税仓代码字典翻译
    bondedCodeFormat(row, column) {
      return this.selectDictLabel(this.bondedCodeOptions, row.bondedCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        adjustmentOrderSn: null,
        merchSn: null,
        merchName: null,
        adjustmentReason: null,
        issuedToOrder: null,
        inGoodsShipStatus: null,
        deliveryType: null,
        carryOverType: null,
        bondedCode: null,
        wareSn: 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.adjustmentOrderSn)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存内调结转出入库记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const adjustmentOrderSn = row.adjustmentOrderSn || this.ids
      getAdjustmentorder(adjustmentOrderSn).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存内调结转出入库记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.adjustmentOrderSn != null) {
            updateAdjustmentorder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdjustmentorder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const adjustmentOrderSns = row.adjustmentOrderSn || this.ids;
      this.$confirm('是否确认删除库存内调结转出入库记录编号为"' + adjustmentOrderSns + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAdjustmentorder(adjustmentOrderSns);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有库存内调结转出入库记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAdjustmentorder(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 pullAdjustmentorder(queryParams,loading,_this);
      })
    },
    /** 拉取明细按钮操作 */
    handlePullDetail(row) {
      const adjustmentOrderSns = row.adjustmentOrderSn || this.ids;

      // 添加遮罩
      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 pullAdjustmentorderDetail(adjustmentOrderSns,loading,_this);
      })
    }
  }
};
</script>
