<template>
  <div class="check pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <el-button
        type="primary"
        :size="'mini'"
        @click="exportGood('导出盘点商品')"
        >导出盘点商品
      </el-button>
      <!-- <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="6" style="width: 292px">
                  <period-date-picker
                    :start="parmValue.start"
                    :end="parmValue.end"
                    :type="1"
                    :width="'135px'"
                    :size="searchSize"
                    @timeReturned="timeReturned($event)"
                  />
                </el-col>
                <el-col :span="4" style="width: 180px; padding: 0 0 0 10px">
                  <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: 130px; padding: 0 0 0 10px">
                  <el-select
                    :size="searchSize"
                    v-model="parmValue.type"
                    filterable
                    clearable
                    placeholder="盘点类型"
                    style="width: 100%"
                    @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  >
                    <el-option
                      v-for="item in typeOptions"
                      :key="'type' + item.id"
                      :label="item.label"
                      :value="item.id"
                    />
                  </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>
            </el-row>
            <el-row style="padding: 10px 0 0 0">
              <el-col :span="24">
                <el-col :span="4" style="width: 260px">
                  <el-input
                    :size="searchSize"
                    v-model="parmValue.wsm_code"
                    :maxlength="40"
                    placeholder="仓库编号"
                  />
                </el-col>
                <el-col :span="4" style="width: 250px; padding: 0 0 0 10px">
                  <el-input
                    :size="searchSize"
                    v-model="parmValue.check_code"
                    :maxlength="40"
                    placeholder="盘点编号"
                  />
                </el-col>
                <el-col :span="4" style="width: 54px">
                  <el-button
                    :size="searchSize"
                    type="primary"
                    class="fr"
                    icon="el-icon-search"
                    @click="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                /></el-col>
                <el-col :span="4" style="width: 66px">
                  <el-button
                    type="warning"
                    class="fr"
                    :size="searchSize"
                    @click="restSearch"
                  >
                    重置
                  </el-button>
                </el-col>
               
                <el-col
                  :span="3"
                  style="width: 66px; float: right"
                  v-if="powers.some((item) => item == '003')"
                >
                  <el-button
                    @click="openModal('add', false, {})"
                    :size="searchSize"
                    type="success"
                    style="float: right; margin-left: 5px"
                  >
                    添加
                  </el-button>
                </el-col>
              </el-col>
            </el-row>
          </div>
        </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 #type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.type == '1' ? 'success' : ''"
            v-text="
              (typeOptions.find((item) => item.id == scope.row.type) || {})
                .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('checkDetail', { id: scope.row.id })"
            ></i>
          </el-tooltip>
        </template>
      </ex-table> -->
      <!-- <add-model
        :id="modelId"
        :sitem="sitem"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="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/stock/check";
import addModel from "./addModel";
import { getToken } from "@/utils/auth";
import { mapGetters } from "vuex";

export default {
  name: "check",
  mixins: [mixinPage, resToken],
  components: {
    addModel,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "check"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      sitem: null,
      // 节点状态
      statusOptions: [
        { id: "0", label: "待发起流程" },
        { id: "1", label: "待上传盘点结果" },
        { id: "2", label: "待审核盘点结果" },
        { id: "3", label: "待系统更新" },
        { id: "4", label: "盘点已结束" },
      ],
      // 盘点类型
      typeOptions: [
        { id: "1", label: "全盘" },
        { id: "2", label: "抽盘" },
      ],
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        wsm_code: "", // 盘点仓库
        check_code: "", // 	盘点编号
        type: "", //盘点类型
        status: "", //判断状态
        start: "",
        end: "",
      },
      tableData: [],
      passwordModel: false,
      passwordModelId: 0,
      isPasswordDetail: false,
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "check_code",
          label: "盘点编号",
          width: "155px",
        },

        {
          prop: "wsm_code",
          label: "仓库编码",
          width: "170px",
        },
        {
          prop: "name",
          label: "仓库名称",
        },

        {
          prop: "code",
          label: "公司编码",
          width: "140px",
        },
        {
          prop: "caname",
          label: "公司名称",
        },
        {
          prop: "status",
          label: "盘点状态",
          _slot_: "status",
          width: "120px",
        },

        {
          prop: "type",
          label: "盘点类型",
          _slot_: "type",
          width: "70px",
        },
        {
          prop: "addtime",
          label: "创建时间",
          width: "140px",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          _noset_: true,
          width: "50px",
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    // this.searchList();
  },

  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        wsm_code: "", // 盘点仓库
        check_code: "", // 	盘点编号
        type: "", //盘点类型
        status: "", //判断状态
        start: "",
        end: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },

    openModal(id, isDetail, sitem) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
      this.sitem = sitem;
    },
    async timeReturned(e) {
      if (e.startTime !== "") {
        this.parmValue.start = e.startTime;
      } else {
        this.parmValue.start = "";
      }

      if (e.endTime !== "") {
        this.parmValue.end = e.endTime;
      } else {
        this.parmValue.end = "";
      }
      if (this.parmValue.start !== "" && this.parmValue.end !== "") {
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    async exportGood(message) {
      // if (!this.loading) {
        this.loading = true;
        let httpType = `aplication/zip`;
        let model = {
          end: "2021-10-29",
          start: "2021-10-01",
          token: getToken(),
          // id: this.queryId,
          // token: getToken(),
        };
        axios({
          method: "post",
          //urlConfig.baseURL + "admin/checkexport"
          url: "http://inv.sitw.wanyuhengtong.com/downqrdinv",
          responseType: "blob",
          data: model,
          headers: {
            Accept: httpType,
          },
        })
          .then((res) => {
            if (res && res.status == 200 && res.data) {
              let blob = new Blob([res.data], {
                type: httpType,
              });
              let url = window.URL.createObjectURL(blob);
              let aLink = document.createElement("a");
              aLink.style.display = "none";
              aLink.href = url;
              aLink.setAttribute("download", `${message}.zip`);
              document.body.appendChild(aLink);
              aLink.click();
              document.body.removeChild(aLink); //下载完成移除元素
              window.URL.revokeObjectURL(url); //释放掉blob对象

              this.$message.success(`${message}导出成功！`);
              setTimeout(() => {
                this.loading = false;
              }, 500);
            } else {
              // this.$message.error(res.data.message);
              setTimeout(() => {
                this.loading = false;
              }, 500);
            }
          })
          .catch((error) => {
            console.log(error);
            this.loading = false;
          });
      // }
    },
    async searchList() {
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }

      this.loading = true;
      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>
<style lang="scss" scoped>
</style>
   