<template>
  <el-dialog
    title="销售单详情"
    v-loading="loading"
    :center="true"
    align="left"
    top="2vh"
    width="1080px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <ex-table
        v-loading="loading"
        :table="table"
        :data="tableData"
        :columns="columns"
        :page="pageInfo"
        :size="size"
        @page-curr-change="handlePageChange"
        @page-size-change="handleSizeChange"
        @selection-change="list => selected = list"
        @screen-reset="
          pageInfo.curr = 1;
          parmValue.page = 1;
          searchList();
        "
        @screen-submit="
          pageInfo.curr = 1;
          parmValue.page = 1;
          searchList();
        "
      >
        <template #table-header="{  }">
          <div style="width: 100%">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="6" style="width: 293px; padding: 0 0 0 0px">
                <period-date-picker
                  :type="1"
                  :width="'135px'"
                  :size="searchSize"
                  :start="parmValue.start"
                  :end="parmValue.end"
                  @timeReturned="handleTime"
                />
              </el-col>
              <el-col :span="4" style="width: 148px; padding: 0 0 0 8px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.status"
                  filterable
                  clearable
                  placeholder="订单状态"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="'status' + item.id"
                    :label="item.label"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
              <el-col :span="4" style="width: 150px; padding: 0 0 0 9px">
                <el-switch
                  v-model="parmValue.use_type"
                  :active-value="usetypeOtions[1].value"
                  :inactive-value="usetypeOtions[0].value"
                  :active-text="usetypeOtions[1].label"
                  :inactive-text="usetypeOtions[0].label"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                />
              </el-col>

              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right; margin-left: 5px"
                  @click="searchList"
                >刷新</el-button>
              </el-col>
            </el-row>
            <el-row style="padding: 10px 0 0 0">
              <el-col :span="6" style="width: 371px">
                <search-customer
                  :value="customerCode"
                  :size="searchSize"
                  :names="parmValue.supplierName"
                  :is-detail="true"
                  :placeholder="'企业客户'"
                  @searchChange="customerChange"
                />
              </el-col>
              <el-col :span="4" style="width: 150px; padding: 0 0 0 9px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.order_source"
                  filterable
                  clearable
                  placeholder="订单来源"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in xs_order_source_options"
                    :key="'orderstatus' + item.id"
                    :label="item.label"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
              <el-col :span="4" style="width: 150px; padding: 0 0 0 9px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.order_type"
                  filterable
                  clearable
                  placeholder="商品类型"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in xs_order_type_options"
                    :key="'orderstatus' + item.id"
                    :label="item.label"
                    :value="item.id"
                  />
                </el-select>
              </el-col>

              <el-col :span="4" style="width: 66px; float: right">
                <el-button type="warning" class="fr" :size="searchSize" @click="restSearch">重置</el-button>
              </el-col>
              <!-- 暂时注释，下版需要导出 -->
              <el-col :span="4" style="width: 66px; float: right">
                <el-button
                  type="primary"
                  style="margin-left: 30px"
                  @click="submit(selection)"
                  :size="searchSize"
                  class="fr"
                >提交</el-button>
              </el-col>
            </el-row>

            <el-row style="padding: 10px 0 0 0">
              <el-col :span="6" style="width: 426px">
                <el-input
                  clearable
                  placeholder="关键字"
                  v-model="input"
                  maxlength="40"
                  :size="searchSize"
                  @blur="searchList"
                  class="input-with-select"
                >
                  <el-select
                    v-model="select"
                    style="width: 100px"
                    slot="prepend"
                    placeholder="关键字类型"
                    @change="searchList"
                  >
                    <el-option label="订单编号" value="orderCode"></el-option>
                    <el-option label="商品名称" value="good_name"></el-option>
                    <el-option label="创建人" value="apply_name"></el-option>
                    <el-option label="创建人部门" value="company_name"></el-option>
                  </el-select>
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  ></el-button>
                </el-input>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #good_name="{ scope }">
          <span>{{ scope.row.good_name }}</span>
          <span v-for="(si, i) in scope.row.speclist" :key="si.spec_id + i">
            <span v-if="i !== 0">-</span>
            <span v-else>_</span>
            <span>{{ si.spec_name }}[{{ si.spec_value || si.spec_value_name }}]</span>
          </span>
        </template>

        <template #use_type="{ scope }">
          <el-tag size="mini">
            {{
            (usetypeOtions.find(({ value }) => value === scope.row.use_type) || {})
            .label || "---"
            }}
          </el-tag>
        </template>

        <template #cgd_status="{ scope }">
          <el-tag size="mini">
            <template v-if="String(scope.row.order_type) === '1'">--</template>
            <template v-else>
              {{
              (
              purchaseStatusOptions.find(
              ({ value }) => value === scope.row.cgd_status
              ) || {}
              ).label || "---"
              }}
            </template>
          </el-tag>
        </template>

        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.status == '0' ? 'warning' : ''"
            v-text="
              (statusOptions.find((item) => item.id == scope.row.status) || {}).label ||
              '--'
            "
          ></el-tag>
        </template>
        <template #order_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            v-text="
              (
                xs_order_type_options.find((item) => item.id == scope.row.order_type) ||
                {}
              ).label || '--'
            "
          ></el-tag>
        </template>
        <template #order_source="{ scope }">
          <el-tag
            :size="tablebtnSize"
            v-text="
              (
                xs_order_source_options.find(
                  (item) => item.id == scope.row.order_source
                ) || {}
              ).label || '--'
            "
          ></el-tag>
        </template>
      </ex-table>
    </el-card>
  </el-dialog>
</template>

<script>
import asyncRequest from "@/apis/service/dataCorrection/cgMessage";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";

import { columns } from "./_columns";
import {
  cg_order_type_options,
  xs_order_type_options,
  xs_order_source_options,
  usetypeOtions
} from "@/assets/js/statusList";

export default {
  mixins: [mixinPage, resToken],
  props: ["showModel"],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"])
  },
  watch: {
    showModel: function(val) {
      this.showModelThis = val;
      if (val) {
        this.restSearch();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    }
  },
  data() {
    return {
      usetypeOtions,
      showModelThis: this.showModel,
      select: "1",
      input: "",
      timeOBJ: {
        start: "", //起始时间
        end: "" // 结束时间
      },
      options: ["创建时间", "最晚入库时间"],
      sselect: "创建时间",
      xs_order_source_options,
      xs_order_type_options,
      // 状态
      statusOptions: [
        { id: "0", label: "待发货" },
        { id: "1", label: "待发货完成" },
        { id: "2", label: "发货已完成" },
        { id: "3", label: "订单已取消" }
      ],
      /* 采购单状态 **/
      purchaseStatusOptions: [
        { value: "0", label: "待与供应商确认" },
        { value: "1", label: "待入库" },
        { value: "2", label: "部分入库" },
        { value: "3", label: "入库完成" },
        { value: "4", label: "已取消订单" }
      ],
      cg_order_type_options,
      selected: [],

      loading: false,
      showModel: false,
      parmValue: {
        use_type: "",
        order_source: "",
        order_type: "", //订单来源
        orderCode: "", //销售订单code
        apply_name: "", // 申请人
        good_name: "", // 产品名称
        good_code: "", // 产品属性编号
        status: "", //状态
        page: 1, // 页码
        size: 15, // 每页显示条数
        supplierNo: "", //供应商编号
        customer_code: "", //客户编号
        start: "", //新建起始时间
        end: "", // 新建结束时间
        company_name: "", //创建人部门
        use_type: "1"
      },
      tableData: [],
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"]
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0
      },
      // 表格 - 列参数
      columns: columns
    };
  },

  methods: {
    //提交
    async submit() {
      if (this.selected.length === 0) {
        this.$message.warning("至少选择一条销售单！");
        return;
      }

      if (this.selected.length != 1) {
        this.$message.warning("只能选中一条数据！");
        return;
      }

      const { id } = this.selected[0];
      this.showModelThis = false;
      // 刷新
      this.$emit("refresh", id || "");
    },

    restSearch() {
      this.select = "orderCode";
      this.input = "";
      this.customerCode = [];
      this.parmValue = {
        order_type: "",
        orderCode: "", //销售订单code
        apply_name: "", // 申请人
        good_name: "", // 产品名称
        good_code: "", // 产品属性编号
        status: "", //状态
        page: 1, // 页码
        size: 15, // 每页显示条数
        supplierNo: "", //供应商编号
        customer_code: "", //客户编号
        start: "", //新建起始时间
        end: "", // 新建结束时间
        company_name: "", //部门
        use_type: "1"
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0
      };
      this.searchList();
    },
    async handleClick(e) {
      this.sselect = e;
      this.parmValue.start = e === "创建时间" ? this.timeOBJ.start : "";
      this.parmValue.end = e === "创建时间" ? this.timeOBJ.end : "";
      this.parmValue.last_start = e !== "创建时间" ? this.timeOBJ.start : "";
      this.parmValue.last_end = e !== "创建时间" ? this.timeOBJ.end : "";
      if (
        !(
          (this.timeOBJ.start !== "" && this.timeOBJ.end === "") ||
          (this.timeOBJ.start === "" && this.timeOBJ.end !== "")
        )
      ) {
        this.parmValue.page = 1;
        this.pageInfo.curr = 1;
        await this.searchList();
      }
    },

    async searchList() {
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      this.parmValue.orderCode = this.select === "orderCode" ? this.input : "";
      this.parmValue.good_name = this.select === "good_name" ? this.input : "";
      this.parmValue.apply_name =
        this.select === "apply_name" ? this.input : "";
      this.parmValue.company_name =
        this.select === "company_name" ? this.input : "";

      const res = await asyncRequest.saleList({
        ...this.parmValue,
        needRela: true
      });

      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach(v => {
          v.good_class = "";
          if (v.can && v.can.length > 0) {
            v.can.forEach((x, i) => {
              v.good_class += i === 0 ? x.name : "_" + x.name;
            });
          }
        });
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    // 时间
    async handleTime(e) {
      if (e.startTime !== "") {
        this.timeOBJ.start = e.startTime;
      } else {
        this.timeOBJ.start = "";
      }
      if (e.endTime !== "") {
        this.timeOBJ.end = e.endTime;
      } else {
        this.timeOBJ.end = "";
      }
      await this.handleClick(this.sselect);
    }
  }
};
</script>

<style lang="scss" scoped></style>
