<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="入库单号" prop="asnNo">
        <el-input
          v-model="queryParams.asnNo"
          placeholder="请输入入库单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库类型" prop="asnType">
        <el-select v-model="queryParams.asnType" placeholder="请选择入库类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.asn_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="asnStatus">
        <el-select v-model="queryParams.asnStatus" placeholder="请选择单据状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.asn_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采购单号" prop="asnReference1">
        <el-input
          v-model="queryParams.asnReference1"
          placeholder="请输入采购单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预期到货时间" prop="expectedArriveTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.expectedArriveTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择预期到货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </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="['docAsn:docAsn:add']"
        >新增</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="['docAsn:docAsn:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['docAsn:docAsn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="docAsnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库单号" align="center" prop="asnNo" />
      <el-table-column label="入库类型" align="center" prop="asnType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asn_type" :value="scope.row.asnType"/>
        </template>
      </el-table-column>
      <el-table-column label="单据状态" align="center" prop="asnStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asn_status" :value="scope.row.asnStatus"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="客户" align="center" prop="customerId" />-->
      <el-table-column label="客户单号" align="center" prop="customerNo" />
      <el-table-column label="采购单号" align="center" prop="asnReference1" />
      <!--<el-table-column label="asn参考信息" align="center" prop="asnReference2" />-->
      <el-table-column label="预期到货时间" align="center" prop="expectedArriveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedArriveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <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="['docAsn:docAsn:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['docAsn:docAsn: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"
    />
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList=""></add-or-update>
  </div>
</template>

<script>
import { listDocAsn, getDocAsn, delDocAsn, addDocAsn, updateDocAsn } from "@/api/docAsn/docAsn";
import AddOrUpdate from './docAsnHeader-add-or-update'

export default {
  name: "DocAsn",
  dicts: ['asn_status', 'asn_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsDocAsnDetails: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库单表格数据
      docAsnList: [],
      // 入库单体表格数据
      wmsDocAsnDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //明细编辑
      addOrUpdateVisible: false,
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        asnNo: null,
        asnType: null,
        asnStatus: null,
        asnReference1: null,
        asnReference2: null,
        releaseStatus: null,
        expectedArriveTime: null,
        notes: null,
        supplierId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询入库单列表 */
    getList() {
      this.loading = true;
      listDocAsn(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.docAsnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        asnNo: null,
        asnType: null,
        asnStatus: "0",
        customerId: null,
        customerNo: null,
        asnReference1: null,
        asnReference2: null,
        releaseStatus: "0",
        expectedArriveTime: null,
        paNo: null,
        qcNo: null,
        notes: null,
        supplierId: null,
        userdefine1: null,
        userdefine2: null,
        userdefine3: null,
        userdefine4: null,
        userdefine5: null,
        createUserId: null,
        createDate: null,
        editUserId: null,
        editDate: null,
        activeFlag: null,
        hEdi01: null,
        hEdi02: null,
        hEdi03: null,
        hEdi04: null,
        hEdi05: null,
        hEdi06: null,
        hEdi07: null,
        hEdi08: null,
        hEdi09: null,
        hEdi10: null,
        hEdi11: null,
        hEdi12: null,
        hEdi13: null,
        hEdi14: null,
        hEdi15: null
      };
      this.wmsDocAsnDetailsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.asnNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init('undefine')
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row.asnNo)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wmsDocAsnDetailsList = this.wmsDocAsnDetailsList;
          if (this.form.asnNo != null) {
            updateDocAsn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocAsn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const asnNos = row.asnNo || this.ids;
      this.$modal.confirm('是否确认删除入库单编号为"' + asnNos + '"的数据项？').then(function() {
        return delDocAsn(asnNos);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 入库单体序号 */
    rowWmsDocAsnDetailsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 入库单体添加按钮操作 */
    handleAddWmsDocAsnDetails() {
      let obj = {};
      obj.sku = "";
      obj.expectedQty = "";
      obj.expectedQtyEach = "";
      obj.receivedQty = "";
      obj.receivedQtyEach = "";
      obj.lineStatus = "";
      obj.holdRejectreason = "";
      obj.notes = "";
      obj.packId = "";
      obj.locationZone = "";
      obj.lotatt01 = "";
      obj.lotatt02 = "";
      obj.lotatt03 = "";
      obj.lotatt04 = "";
      obj.lotatt05 = "";
      obj.lotatt06 = "";
      obj.lotatt07 = "";
      obj.lotatt08 = "";
      obj.lotatt09 = "";
      obj.lotatt10 = "";
      obj.lotatt11 = "";
      obj.lotatt12 = "";
      obj.lotatt13 = "";
      obj.lotatt14 = "";
      obj.lotatt15 = "";
      obj.lotatt16 = "";
      obj.lotatt17 = "";
      obj.lotatt18 = "";
      obj.createUserId = "";
      obj.createDate = "";
      obj.editUserId = "";
      obj.editDate = "";
      obj.locationId = "";
      obj.documentId = "";
      obj.dEdi01 = "";
      obj.dEdi02 = "";
      obj.dEdi03 = "";
      obj.dEdi04 = "";
      obj.dEdi05 = "";
      obj.dEdi06 = "";
      obj.dEdi07 = "";
      obj.dEdi08 = "";
      obj.dEdi09 = "";
      obj.dEdi10 = "";
      obj.dEdi11 = "";
      obj.dEdi12 = "";
      obj.dEdi13 = "";
      obj.dEdi14 = "";
      obj.dEdi15 = "";
      this.wmsDocAsnDetailsList.push(obj);
    },
    /** 入库单体删除按钮操作 */
    handleDeleteWmsDocAsnDetails() {
      if (this.checkedWmsDocAsnDetails.length == 0) {
        this.$modal.msgError("请先选择要删除的入库单体数据");
      } else {
        const wmsDocAsnDetailsList = this.wmsDocAsnDetailsList;
        const checkedWmsDocAsnDetails = this.checkedWmsDocAsnDetails;
        this.wmsDocAsnDetailsList = wmsDocAsnDetailsList.filter(function(item) {
          return checkedWmsDocAsnDetails.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWmsDocAsnDetailsSelectionChange(selection) {
      this.checkedWmsDocAsnDetails = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('docAsn/docAsn/export', {
        ...this.queryParams
      }, `docAsn_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
