<template>
  <div class="storeManage pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <ex-table
        v-loading="false"
        :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: 130px; padding-left: 10px">
                  <search-account
                    :value="apply_name"
                    :disabled="false"
                    :is-detail="false"
                    :size="searchSize"
                    :placeholder="'申请人'"
                    @searchChange="handleResignName"
                  />
                </el-col>
                <el-col style="width: 110px; padding: 0 0 0 10px">
                  <el-input
                    v-model="parmValue.status"
                    placeholder="节点查询"
                    :size="searchSize"
                  />
                </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="3"
                  style="width: 56px"
                  class="mr10 fr"
                  v-if="powers.some((item) => item == '003')"
                >
                  <el-button
                    class="fl"
                    :size="searchSize"
                    type="success"
                    @click="openModal('add', false)"
                  >
                    新建
                  </el-button>
                </el-col>
              </el-col>
            </el-row>
            <el-row>
              <el-col style="width: 190px; padding-top: 10px">
                <el-input
                  :size="searchSize"
                  placeholder="商品名称"
                  v-model="parmValue.good_name"
                  clearable
                  @blur="searchList"
                />
              </el-col>
              <el-col :span="4" style="width: 195px; padding: 10px 0 0 10px">
                <el-input
                  placeholder="商品编码"
                  :size="searchSize"
                  v-model="parmValue.good_code"
                />
              </el-col>
              <el-col :span="4" style="width: 195px; padding: 10px 0 0 10px">
                <el-input
                  :size="searchSize"
                  v-model="parmValue.bk_code"
                  :maxlength="40"
                  placeholder="申请编码"
                  @blur="searchList"
                />
              </el-col>
              <el-col :span="4" style="width: 54px; padding-top: 10px">
                <el-button
                  :size="searchSize"
                  type="primary"
                  class="fr"
                  icon="el-icon-search"
                  @click="searchList"
              /></el-col>

              <el-col :span="4" style="width: 66px; padding-top: 10px">
                <el-button
                  type="warning"
                  class="fr"
                  :size="searchSize"
                  @click="restSearch"
                >
                  重置
                </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="
              (statusOptions.find((item) => item.id == scope.row.status) || {})
                .label || '--'
            "
          ></el-tag>
        </template>
        <template #operation="{ scope }">
          <el-tooltip
            v-if="powers.some((item) => item == '007')"
            effect="dark"
            content="详情"
            placement="top"
          >
            <i
              class="el-icon-view tb-icon"
              @click="routeGoto('storeManageFlow', { id: scope.row.id })"
            ></i>
          </el-tooltip>
        </template>
      </ex-table>
      <add-model
        :id="modelId"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @closeModel="showModel = false"
      />
    </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/purchaseIn/storeManage";
import addModel from "./addModel.vue";
import { mapGetters } from "vuex";
import columns from "./columns";
export default {
  name: "storeManage",
  mixins: [mixinPage, resToken],
  components: {
    addModel,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "storeManage"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      statusOptions: [
        { id: "0", label: "待申请" },
        { id: "1", label: "待反馈" },
        { id: "2", label: "已反馈" },
        { id: "3", label: "同意反馈" },
        { id: "4", label: "拒绝反馈" },
      ],
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      apply_name: [],
      parmValue: {
        good_code: "",
        bk_code: "",
        status: "",
        apply_name: "",
        start: "",
        end: "",
        good_name: "",
        page: 1,
        size: 15,
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  mounted() {
    this.searchList();
  },

  methods: {
    // 选择时间操作
    handleTime(e) {
      console.log(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();
      }
      console.log(this.parmValue);
    },
    // 重置参数
    restSearch() {
      this.apply_name = [];
      this.parmValue = {
        status: "",
        apply_name: "",
        good_code: "",
        good_name: "",
        start: "",
        end: "",
        bk_code: "", //备库编号
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },

    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    // 列表搜索
    async searchList() {
      this.loading = true;
      let { status, apply_name, bk_code, start, end, page, size, good_name } =
        this.parmValue;
      if ((start !== "" && end === "") || (start == "" && end != "")) {
        this.$message.warning("开始时间和结束时间不能为空");
        return;
      }
      let model = {
        status,
        apply_name,
        start,
        end,
        page,
        size,
        good_name,
        bk_code,
      };
      const res = await asyncRequest.list(model);
      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;
    },
    async handleResignName(e) {
      console.log(e);
      this.parmValue.apply_name = e && e.id ? e.label : "";
      this.apply_name = e && e.id ? [e.id] : [];
      this.parmValue.page = 1;
      await this.searchList();
    },
  },
};
</script>
   