<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="clientId">
        <el-select
          v-model="queryParams.clientId"
          placeholder="请选择客户"
          clearable
          filterable
          remote
          :remote-method="getClientList"
        >
          <el-option
            v-for="item in clientList"
            :key="item.clientId"
            :value="item.clientId"
            :label="item.clientName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="交货日期" prop="deliveryDate">
        <el-date-picker
          clearable
          v-model="queryParams.deliveryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择交货日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源文件" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源文件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:purchaseOrder:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column label="订单编号" prop="orderNo" width="160" />
      <el-table-column label="客户编号" prop="clientNo" width="120" />
      <el-table-column
        label="客户名称"
        prop="clientName"
        width="240"
        show-overflow-tooltip
      />
      <el-table-column label="交货日期" prop="deliveryDate" width="180">
        <template slot-scope="{ row }">
          <span>{{ parseTime(row.deliveryDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" min-width="200" />
      <el-table-column label="订单状态" prop="state" width="100">
        <template slot-scope="{ row }">
          {{
            dict.type.sales_order_state.find((item) => item.value == row.state)
              .label
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="来源文件"
        prop="source"
        width="300"
        show-overflow-tooltip
      />
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="{ row }">
          <el-button
            v-hasPermi="['md:order:query']"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="onShowDetail(row)"
            >查看</el-button
          >
          <el-button
            v-hasPermi="['md:order:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="toSalesEdit(row)"
            >生成订单</el-button
          >
          <el-button
            v-hasPermi="['md:order:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            >删除</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 :visible.sync="showDetail" title="采订单详情" width="1200px">
      <el-table :data="detailList" max-height="600" size="mini">
        <el-table-column
          label="产品名称"
          prop="itemName"
          show-overflow-tooltip
          width="300"
        />
        <el-table-column
          label="客户单号"
          prop="poOrderNo"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="客户产品代码"
          width="140"
          prop="poProductName"
          show-overflow-tooltip
        />
        <el-table-column
          label="客户产品名称"
          prop="poProductNo"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="宝绅内部品名"
          prop="productName"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="客户单位"
          prop="customerUnit"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="指令号"
          width="120"
          prop="cmdNo"
          show-overflow-tooltip
        />
        <el-table-column
          label="型体号"
          prop="featureNo"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="配色号"
          prop="poColorNo"
          show-overflow-tooltip
        />
        <el-table-column
          label="客户颜色"
          prop="poColor"
          show-overflow-tooltip
        />
        <el-table-column label="客户单重" prop="poKg" show-overflow-tooltip />
        <el-table-column label="销线" prop="salePath" show-overflow-tooltip />
        <el-table-column label="段别" prop="segment" show-overflow-tooltip />
        <el-table-column label="版面" prop="layout" show-overflow-tooltip />
        <el-table-column label="季度" prop="quarter" show-overflow-tooltip />
        <el-table-column
          label="整/半码"
          prop="wholeYard"
          show-overflow-tooltip
        />
        <el-table-column
          label="特殊SIZE"
          prop="specialSize"
          show-overflow-tooltip
        />
        <el-table-column label="PO号" prop="poNo" show-overflow-tooltip />
        <el-table-column label="WIP号" prop="wipNo" show-overflow-tooltip />
        <el-table-column
          label="装箱数"
          prop="packingNum"
          show-overflow-tooltip
        />
        <el-table-column label="产地" prop="region" show-overflow-tooltip />
        <el-table-column label="周期" prop="cycle" show-overflow-tooltip />
        <el-table-column
          width="120"
          label="SK编号"
          prop="skNo"
          show-overflow-tooltip
        />
        <el-table-column label="专利号" prop="patentNo" show-overflow-tooltip />
        <el-table-column
          label="材质描述"
          prop="materialDesc"
          show-overflow-tooltip
        />
        <el-table-column label="签收人" prop="" show-overflow-tooltip />
        <el-table-column
          label="shipToId"
          prop="shipToId"
          show-overflow-tooltip
        />
        <el-table-column
          label="交货日期"
          prop="deliveryDate"
          show-overflow-tooltip
        />
        <el-table-column label="备注" prop="remark" show-overflow-tooltip />
        <el-table-column
          label="尺码组"
          prop="sizeGroup"
          show-overflow-tooltip
        />
        <el-table-column label="尺码" prop="size" show-overflow-tooltip />
        <el-table-column label="订单量" prop="orderNum" show-overflow-tooltip />
        <el-table-column label="条码" prop="barCode" show-overflow-tooltip />
        <el-table-column
          label="印刷方向"
          prop="printingDirection"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品规格"
          prop="productSize"
          show-overflow-tooltip
        />
        <el-table-column label="操作" fixed="right">
          <template slot-scope="{ row }">
            <el-button type="text" @click="onShowSpec(row)">特性值</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="showDetail = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="showSpec"
      title="采购详情产品特性值"
      width="650px"
    >
      <el-form label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12" v-for="spec in specList" :key="spec.specKey">
            <el-form-item :label="spec.specKey">
              <el-input v-model="spec.specVal" readonly />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button @click="showSpec = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { delOrder, listOrder } from "@/api/mes/od/purchaseOrder";
import { listDetail } from "@/api/mes/od/purchaseOrderDetail";
import { listClient } from "@/api/mes/md/client";

export default {
  name: "PurchaseOrder",
  dicts: ["mes_client_type", "sys_yes_no", "sales_order_state"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户采购订单表格数据
      orderList: [],
      clientList: [],
      detailList: [],
      specList: [],
      // 是否显示弹出层
      showDetail: false,
      showSpec: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        clientId: null,
        deliveryDate: null,
        state: null,
        source: null,
      },
    };
  },
  created() {
    this.getList();
    this.getClientList();
  },
  methods: {
    /** 查询客户采购订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getClientList(key) {
      listClient({ clientName: key, pageNum: 1, pageSize: 100 }).then(
        (res) => (this.clientList = res.rows)
      );
    },
    /** 搜索按钮操作 */
    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;
    },
    toSalesEdit(row) {
      if (row.state != 0)
        return this.$message.error("该采购单单已生成销售订单");
      let id = row ? row.id : this.ids[0];
      this.$router.push("/order/sales/detail?purchaseId=" + id);
    },
    onShowDetail(row) {
      let id = row ? row.id : this.ids[0];
      this.showDetail = true;
      listDetail({ orderId: id }).then((res) => (this.detailList = res.rows));
    },
    onShowSpec(row) {
      this.showSpec = true;
      this.specList = JSON.parse(row.specJson);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除客户采购订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
