<template>
  <div class="stock pagePadding">
    <tableAuth :show="powers.some((item) => item == '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;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row>
            <el-col :span="24">
              <el-col :span="4" style="width: 115px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.status"
                  filterable
                  clearable
                  placeholder="订单状态"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in statusList"
                    :key="'status' + item.id"
                    :label="item.label"
                    :value="item.id"
                  />
                </el-select>
              </el-col>

              <el-col :span="4" style="width: 312px; padding: 0 0 0 10px">
                <numBerRange
                  :lower="parmValue.stock_low"
                  :online="parmValue.stock_up"
                  :type="1"
                  :title="'可用库存'"
                  :size="searchSize"
                  :width="'140px'"
                  @numReturned="numReturned($event)"
                />
              </el-col>
              <el-col :span="4" style="width: 318px; padding: 0 0 0 10px">
                <el-input
                  :size="searchSize"
                  v-model="input"
                  :maxlength="40"
                  placeholder="关键字"
                >
                  <el-select
                    v-model="select"
                    slot="prepend"
                    style="width: 95px"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, index) in selectList"
                      :label="item.name"
                      :value="item.code"
                      :key="index + 'se'"
                    ></el-option>
                  </el-select>

                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click.native="searchList"
                  ></el-button>
                </el-input>
              </el-col>

              <el-col
                :span="4"
                style="width: 80px; padding: 0 0 0 10px"
                v-if="powers.some((item) => item == '008')"
              >
                <el-button
                  type="warning"
                  :size="searchSize"
                  @click="restSearch"
                >
                  重置
                </el-button>
              </el-col>
              <el-col
                :span="3"
                v-if="powers.some((item) => item == '002')"
                style="width: 80px; padding: 0 0 0 10px; float: right"
              >
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right"
                  @click="searchList"
                >
                  刷新
                </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="
            (statusList.find((item) => item.id == scope.row.status) || {})
              .label || '--'
          "
        ></el-tag>
      </template>

      <template #operation="{ scope }">
        <el-tooltip
          v-if="powers.some((item) => item == '005')"
          class="item"
          effect="dark"
          content="修改"
          placement="top"
        >
          <i
            class="el-icon-edit tb-icon"
            @click="openModal(scope.row, true)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="powers.some((item) => item == '010')"
          class="item"
          effect="dark"
          content="修改记录"
          placement="top"
        >
          <i
            class="el-icon-data-analysis tb-icon"
            @click="openModal(scope.row.id, false)"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
    </tableAuth>
    <!-- 弹窗 新增/修改 -->
    <add-edit
      :id="modelId"
      :show-model="showModel"
      @refresh="searchList"
      @cancel="showModel = false"
    />
    <!-- 库存修改记录 -->
    <edit-record
      :id="modelId"
      :show-model="showRecordModel"
      @refresh="showRecordModel = false"
      @cancel="showRecordModel = false"
    />
  </div>
</template>
<script>
import ExTable from "@/components/ExTableNew.vue";
import addEdit from "./addEdit";
import editRecord from "./editRecord";
import asyncRequest from "@/apis/service/mobile/stock";
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import numBerRange from "@/components/numberRange";
export default {
  name: "stock",
  mixins: [mixinPage, resToken],
  components: {
    ExTable,
    addEdit,
    numBerRange,
    editRecord,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stock"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      selectList: [
        { code: "1", name: "账号" },
        { code: "2", name: "姓名" },
        { code: "3", name: "手机号" },
      ],

      select: "1",
      input: "",
      loading: true,
      showModel: false,
      showRecordModel:false,
      modelId: 0,
      parmValue: {
        username: "", // 账户名
        nickname: "", // 用户名
        status: "", //
        mobile: "",
        stock_low: "",
        stock_up: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 状态
      statusList: [
        { id: "0", label: "未激活" },
        { id: "1", label: "已激活" },
        { id: "2", label: "已过期" },
      ],
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        // _defaultHeader_: ["setcol"]
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "id",
          label: "账户ID",
          width: "60",
        },

        {
          prop: "username",
          label: "账户名",
        },
        {
          prop: "status",
          label: "账户状态",

          _slot_: "status",
        },
        {
          prop: "nickname",
          label: "用户名",
        },
        {
          prop: "mobile",
          label: "联系方式",
        },
        {
          prop: "stock_balance",
          label: "可用库存",
        },

        {
          prop: "stock_update",
          label: "库存更新时间",
          sortable: true,
          width: "140",
        },

        {
          prop: "",
          label: "操作",
          fixed: "right",
          _noset_: true,
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.select = "1";
    this.input = "";
    this.searchList();
  },
  methods: {
    restSearch() {
      this.select = "1";
      this.input = "";
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        username: "", // 账户名
        nickname: "", // 用户名
        status: "", //
        mobile: "",
        stock_low: "",
        stock_up: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },
    // 新建/编辑/详情
    openModal(id, isDetail) {
      this.modelId = id;
      // this.showRecordModel = true;
      if (isDetail == true) {
        this.showModel = true;
      } else {
        this.showRecordModel = true;
      }
    },

    async numReturned(e) {
      this.parmValue.stock_low = e.lower;
      this.parmValue.stock_up = e.online;
    },
    // 刷新表格
    async searchList() {
      this.loading = true;
      this.parmValue.username = this.select === "1" ? this.input : "";
      this.parmValue.nickname = this.select === "2" ? this.input : "";
      this.parmValue.mobile = this.select === "3" ? this.input : "";
      const {code,data,message} = await asyncRequest.list(this.parmValue);
      if (code === 0) {
        const {list,count}=data
        this.tableData = list;
        this.tableData.forEach((v1) => {
          v1.stock_balance += "kg";
        });
        this.pageInfo.total = Number(count);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

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