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

    <el-dialog
      :visible.sync="showSpec"
      :title="$t('采购详情产品特性值')"
      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">{{ $t('common.cancel') }}</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(this.$t('该采购单单已生成销售订单'));
      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(this.$t('common.confirm_del_data'))
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('common.del_success'));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
