<template>
  <div class="stockApply pagePadding">
    <div
      v-if=" powers.some((i) => i == '001')"
    >
      <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();
        "
      >
        <template #table-header="{}">
          <div style="width: 100%">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="24">
                <el-col :span="4" style="width: 295px">
                  <period-date-picker
                    :start="parmValue.start"
                    :end="parmValue.end"
                    :width="'135px'"
                    :size="searchSize"
                    @timeReturned="handleTime"
                  />
                </el-col>

                <el-col style="width: 200px; padding: 0 0 0 10px">
                  <el-select
                    clearable
                    :size="searchSize"
                    v-model="parmValue.status"
                    placeholder="备库申请状态"
                    @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </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-col>
            </el-row>
            <el-row style="padding-top: 10px">
              <el-col style="width: 400px">
                <el-input
                  placeholder="关键字"
                  v-model="input"
                  maxlength="40"
                  :size="searchSize"
                >
                  <el-select
                    v-model="select"
                    slot="prepend"
                    placeholder="关键字类型"
                    style="width: 130px"
                  >
                    <el-option label="备库申请编号" value="1" />
                    <el-option label="商品属性编号" value="2" />
                    <el-option label="商品名称" value="3" />
                    <el-option label="申请人" value="4" />
                    <el-option label="申请人部门" value="5" />
                  </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-col
                :span="3"
                style="width: 56px"
                class="fr"
                v-if="powers.some((i) => i == '003')"
              >
                <el-button
                  class="fl"
                  :size="searchSize"
                  type="success"
                  @click="
                    routeGoto('stockApplyDetail', {
                      id: 'add',
                      type: 'add',
                    })
                  "
                >
                  新建
                </el-button>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="
              scope.row.status == '0'
                ? 'warning'
                : scope.row.status == '3'
                ? 'success'
                : scope.row.status == '4'
                ? 'danger'
                : ''
            "
            v-text="
              (options.find((item) => item.value == scope.row.status) || {})
                .label || '--'
            "
          ></el-tag>
        </template>
        <template #operation="{ scope }">
          <el-tooltip
            v-if="powers.some((i) => i == '007')"
            effect="dark"
            content="详情"
            placement="top"
          >
            <i
              class="el-icon-view tb-icon"
              
              @click="getRouter('stockApplyDetail', scope.row.id )"
            ></i>
          </el-tooltip>
        </template>
      </ex-table>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
   <script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/stockApply";
import { mapGetters } from "vuex";
import { columns } from "./columns";
export default {
  name: "stockApply",
  mixins: [mixinPage, resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockApply"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      options: [
        {
          value: "1",
          label: "待采购反馈",
        },
        {
          value: "2",
          label: "已生成采购单",
        },
      ],
      select: "1",
      input: "",
      loading: true,
      parmValue: {
        good_code: "",
        bk_code: "",
        status: "",
        apply_name: "",
        start: "",
        end: "",
        good_name: "",
        page: 1,
        size: 15,
        company_name:"",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  mounted() {
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      console.log(this.parmValue)
      const {page,size}=this.parmValue;
      // this.parmValue.start = start || last_start;
      // this.parmValue.end = end || last_end;

      // if(this.parmValue.khNo.length>0){
      //     this.customerCode = [this.parmValue.khNo] ;
      // }
      
       this.pageInfo= {
        size: size,
        curr: page,
        total: 0,
      }
      //多选条件
    this.select = this.parmValue.select;
    // this.sselect = this.parmValue.sselect;
    this.input = this.parmValue.input

    }else{
       this.select = "1";
      //  this.sselect = "创建时间"
    }
    this.searchList();
  },

  methods: {
    getRouter(toRouter, queryId){
      if (toRouter && queryId) {
        let model = {
          id: queryId,
          type: 'view',
        };

        //有多选框的条件
        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("暂未找到相关流程！");
      }
    },
    // 选择时间操作
    handleTime(e) {
      if (e) {
        this.parmValue.start = e.startTime;
        this.parmValue.end = e.endTime;
      } else {
        this.parmValue.start = "";
        this.parmValue.end = "";
      }
      if (this.parmValue.start != "" && this.parmValue.end != "") {
        this.searchList();
      } else if (this.parmValue.start === "" && this.parmValue.end === "") {
        this.searchList();
      }
    },
    // 重置参数
    restSearch() {
      this.select = "";
      this.input = "";
      this.parmValue = {
        status: "",
        apply_name: "",
        good_code: "",
        good_name: "",
        start: "",
        end: "",
        bk_code: "", //备库编号
        page: 1, // 页码
        size: 15, // 每页显示条数
        company_name:"",
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      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.bk_code = this.select === "1" ? this.input : "";
      this.parmValue.good_code = this.select === "2" ? this.input : "";
      this.parmValue.good_name = this.select === "3" ? this.input : "";
      this.parmValue.apply_name = this.select === "4" ? this.input : "";
      this.parmValue.company_name = this.select === "5" ? this.input : "";
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        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;
    },
  },
};
</script>
   