<template>
  <div class="purchaseOrder pagePadding">
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @selection="selection_change"
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 0 80px" :gutter="10">
            <el-col :span="4" style="width: 303px; padding: 0 0 0 10px">
              <period-date-picker
                :type="1"
                :width="'135px'"
                :size="searchSize"
                :start="timeOBJ.start"
                :end="timeOBJ.end"
                @timeReturned="handleTime"
              />
            </el-col>

            <el-col :span="4">
              <el-input
                size="mini"
                placeholder="报备单号"
                v-model="parmValue.fillCode"
                @blur="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              />
            </el-col>

            <el-col :span="4">
              <el-input
                size="mini"
                placeholder="商品名称"
                v-model="parmValue.good_name"
                @blur="
                  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-col :span="4" style="width: 66px; float: right">
              <el-button type="warning" class="fr" :size="searchSize" @click="restSearch">重置</el-button>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="3" class="fr" style="width: 66px; margin-top: 10px">
              <el-button
                v-if="powers.includes('003')"  :size="searchSize" type="success" style="float: right"
                @click="routeGoto('/sellOut/businessFilingDetail', { id: 'add', type: 'add' })"
              >添加</el-button>
            </el-col>

            <el-col :span="4" style="margin-top: 15px">
              <search-customer
                size="mini"
                :names="khName"
                :is-detail="type !== 'add'"
                :value="parmValue.customerCode"
                placeholder="客户公司"
                @searchChange="handleCustomerChange"
              />
            </el-col>

            <el-col :span="4" style="margin-top: 15px; margin-left: 10px">

              <el-select
                clearable
                size="mini"
                placeholder="状态"
                v-model="parmValue.status"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="status in statusOptions"
                  :key="status.value"
                  :value="status.value"
                  :label="status.label"
                />
              </el-select>
            </el-col>
          </el-row>
        </div>
      </template>

      <template #is_compliance="{ scope }">
        <el-tag size="mini">
          {{scope.row.is_compliance === "0" ? "未填写" : scope.row.is_compliance === "1" ? "合规" : "不合规"}}
        </el-tag>
      </template>

      <template #good_img="{ scope }">
        <img
          v-viewer
          style="width: 23px; height: 23px; margin: 0 5px 0 0"
          class="fl"
          :src="scope.row.good_img"
          alt
        />
      </template>
      <template #cat_info="{ scope }">
        {{ scope.row.cat.search }}
      </template>
      <template #good_img="{ scope }">
        <div v-if="scope.row.good_img" style="width: 20px; height: 20px" class="hover" v-viewer>
          <img
            :src="scope.row.good_img"
            style="display: inline-block; width: 100%; height: 100%"
            alt
          />
        </div>
      </template>
      <template #customer="{ scope }">
          <el-tooltip :content="'企业客户编码 : ' + (scope.row.customerCode || '--')" placement="top">
            <i
              v-if="scope.row.customerName"
              class="el-icon-warning-outline"
              style="margin-right:2px;cursor:pointer"
            />
          </el-tooltip>
          {{scope.row.customerName}}
      </template>
      <template #company="{ scope }">
          <el-tooltip :content="'业务公司编码 : ' + (scope.row.companyNo || '--')" placement="top">
            <i
              v-if="scope.row.companyName"
              class="el-icon-warning-outline"
              style="margin-right:2px;cursor:pointer"
            />
          </el-tooltip>
          {{scope.row.companyName}}
      </template>
      <template #supplier="{scope}">
          <el-tooltip :content="'供应商公司编码 : ' + (scope.row.supplierNo || '--')" placement="top">
            <i class="el-icon-warning-outline" style="margin-right:2px;cursor:pointer" />
          </el-tooltip>
          {{scope.row.supplierName}}
      </template>
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          v-text="
            (statusOptions.find((item) => item.value == scope.row.status) || {}).label || '--'
          "
        ></el-tag>
      </template>
      <template #operation="{ scope }">
        <el-tooltip effect="dark" content="详情" placement="top">
          <i class="el-icon-view tb-icon" @click="getRouter('/sellOut/businessFilingDetail', scope.row.id)"></i>
        </el-tooltip>
      </template>
    </ex-table>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/businessFiling";
import { cg_order_type_options } from "@/assets/js/statusList";
import { columns, statusOptions } from "./config/columns";
import mixinPage from "@/mixins/elPaginationHandle";
import companyHelper from "@/mixins/companyHelper";
import urlConfig from "@/apis/url-config";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";


export default {
  name: "purchaseOrder",
  mixins: [mixinPage, resToken, companyHelper],
  components: {},
  computed: {
    ...mapGetters([
      "business_companyNo",
      "tablebtnSize",
      "isSupertube",
      "searchSize",
      "size"
    ]),
    powers() {
      const { btnList } = this.$store.getters;
      const tran = btnList.find(item => item.menu_route == "businessFiling") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    _process() {
      const { roleProcess } = this.$store.getters;
      const tran = roleProcess.find(i => i.process_type === "BBD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    maxNo() {
      return this.business_companyNo;
    }
  },

  watch: {
    maxNo(val, oldval) {
      this.restSearch();
    }
  },
  data() {
    return {
      //入库公司
      supplierName: "",
      cg_order_type_options,
      //入库仓库
      stockName: "",
      changeList: [],
      fileUrl: urlConfig.baseURL,
      sitem: null,
      select: "1",
      input: "",
      timeOBJ: {
        start: "", //起始时间
        end: "" // 结束时间
      },
      // 状态
      statusOptions: statusOptions,
      loading: false,
      showModel: false,
      isDetail: false,
      modelId: 0,
      wsm_code: [],
      wsm_supplierNo: [],
      parmValue: {
        fillCode: "",
        good_name: "",
        customerCode: [],
        status: "",
        start: "", //新建起始时间
        end: "" // 新建结束时间
      },
      tableData: [],
      passwordModel: false,
      passwordModelId: 0,
      isPasswordDetail: false,
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"]
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        total: 0,
        curr: 1
      },
      // 表格 - 列参数
      columns: columns
    };
  },
  mounted() {
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      console.log(this.parmValue);
      const { page, size, start, end } = this.parmValue;

      this.pageInfo = {
        size: this.pageInfo.size,
        curr: page,
        total: 0
      };
      //多选条件
      this.select = this.parmValue.select;
    } else {
      this.select = "1";
    }
    this.searchList();
  },

  methods: {
    async handleCancel(id) {
      const { code, message } = await asyncRequest.cancel({ id });
      if (Number(code) === 0) {
        this.searchList();
      } else {
        this.$message.warning(message);
      }
    },
    async supplierChange(e) {
      const { code, label } = e;
      this.parmValue.supplierNo = code ? [code] : [];
      this.searchList();
    },
    getRouter(toRouter, queryId, type) {
      if (toRouter && queryId) {
        let model = { id: queryId, type };
        //有多选框的条件
        this.parmValue.select = this.select;
        this.parmValue.sselect = this.sselect;
        this.parmValue.input = this.input;
        let routerModel = { options: JSON.parse(JSON.stringify(this.parmValue)), router: this.$route.path };
        model.preModel = JSON.stringify(routerModel);
        this.routeGoto(toRouter, model);
      } else {
        this.$message.warning("暂未找到相关流程！");
      }
    },
    async handleCancel(id) {
      const { code, message } = await asyncRequest.cancel({ id });
      if (Number(code) === 0) {
        this.searchList();
      } else {
        this.$message.warning(message);
      }
    },
    //选中触发函数
    selection_change(e) {
      const { list } = e;
      //选中的数组集合
      this.changeList = list.length > 0 ? JSON.parse(JSON.stringify(list)) : [];
    },
    showal(list, message, code) {
      let htmlList = "<ul>";
      list.forEach(v => { htmlList += `<li>${code !== "" ? v[code] : v}</li>`});
      htmlList += "</ul>";
      this.$notify({ title: message, dangerouslyUseHTMLString: true, message: htmlList });
    },
    restSearch() {
      this.select = "1";
      this.input = "";
      this.wsm_code = [];
      this.wsm_supplierNo = [];
      this.timeOBJ = {
        start: "", //起始时间
        end: "" // 结束时间
      };
      this.parmValue = {
        start: "", //新建起始时间
        end: "", // 新建结束时间
        fillCode: "", // 报备单编码
        good_name: "", // 产品名称
        customerCode: [],
        status: "", //状态
        page: 1, // 页码
        size: 15 // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0
      };
      this.searchList();
    },
    handleCustomerChange(e) {
      this.parmValue.customerCode = e.code ? [e.code] : "";
      this.customerName = e.label;
      this.searchList();
    },
    async searchList() {
      if (this.loading) return;

      if (
        (this.timeOBJ.start !== "" && this.timeOBJ.end === "") ||
        (this.timeOBJ.start === "" && this.timeOBJ.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }

      this.loading = true;
      const res = await asyncRequest.list({
        ...this.parmValue,
        ...this.timeOBJ,
        ...this.pageInfo,
        noRelation: true,
        companyNo: this.currentCompany,
        supplierNo: Array.isArray(this.parmValue.supplierNo) ? this.parmValue.supplierNo[0] : this.parmValue.supplierNo,
        customerCode: Array.isArray(this.parmValue.customerCode) ? this.parmValue.customerCode[0] : this.parmValue.customerCode
      });
      const { code, data } = await this.useResHandle(res);
      if (code === 0) {
        const { list, count } = data;
        this.tableData = list;
        this.pageInfo.total = Number(count);
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    // 时间
    async handleTime(e) {
      this.timeOBJ.start = e?.startTime ?? "";
      this.timeOBJ.end = e?.endTime ?? "";

      if (e.startTime && e.endTime) {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        this.searchList();
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.purchaseOrder {
  // text-align: right;
}
</style>
