<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="客户" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择单据状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.so_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择单据类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.so_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货时间" prop="shipmentTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.shipmentTime"
                        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="['docOrder:docOrder: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="['docOrder:docOrder: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="['docOrder:docOrder: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="['docOrder:docOrder:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="docOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出库单号" align="center" prop="orderNo" />
      <el-table-column label="货主代码" align="center" prop="customerId" />
      <el-table-column label="单据状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.so_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.so_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="客户单号1" align="center" prop="soReference1" />
      <el-table-column label="客户单号2" align="center" prop="soReference2" />
      <el-table-column label="客户单号3" align="center" prop="soReference3" />
      <!--<el-table-column label="单据操作优先级" align="center" prop="priority" />-->
      <!--<el-table-column label="收货单位名称" align="center" prop="consigneeName" />-->
      <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="['docOrder:docOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['docOrder:docOrder: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 { listDocOrder, getDocOrder, delDocOrder, addDocOrder, updateDocOrder } from "@/api/docOrder/docOrder";
import AddOrUpdate from './docOrderHeader-add-or-update'

export default {
  name: "DocOrder",
  dicts: ['so_status', 'so_type'],
  data() {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库单头表格数据
      docOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        orderStatus: null,
        orderType: null,
        soReference1: null,
        soReference2: null,
        soReference3: null,
        priority: null,
        consigneeName: null,
        shipmentTime: '',
        createTime: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出库单头列表 */
    getList() {
      this.loading = true;
      listDocOrder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.docOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNo: null,
        customerId: null,
        orderStatus: null,
        orderType: null,
        soReference1: null,
        soReference2: null,
        soReference3: null,
        priority: null,
        consigneeId: null,
        consigneeName: null,
        cusCountry: null,
        cusProvince: null,
        cusCity: null,
        cusDistrict: null,
        cusAddress: null,
        cusContact: null,
        cusPostcode: null,
        cusEmail: null,
        carrierId: null,
        shipmentTime: null,
        carrierName: null,
        deliveryNo: null,
        ediSendFlag: null,
        remark: null,
        warehouseId: null,
        userdefine1: null,
        userdefine2: null,
        userdefine3: null,
        userdefine4: null,
        userdefine5: null,
        userdefine6: null,
        addTime: null,
        addWho: null,
        editTime: null,
        editWho: 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.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderNo)
      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();
      const orderNo = row.orderNo || this.ids
      getDocOrder(orderNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出库单头";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderNo != null) {
            updateDocOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderNos = row.orderNo || this.ids;
      this.$modal.confirm('是否确认删除出库单头编号为"' + orderNos + '"的数据项？').then(function() {
        return delDocOrder(orderNos);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('docOrder/docOrder/export', {
        ...this.queryParams
      }, `docOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
