<template>
  <div class="adjust pagePadding">
    <div
      v-if="powers && powers.length > 0 && 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="
          parmValue.page = 1;
          pageInfo.curr = 1;
          searchList();
        "
        @screen-submit="
          parmValue.page = 1;
          pageInfo.curr = 1;
          searchList();
        "
      >
        <template #table-header="{}">
          <div style="width: 100%">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="5" style="width: 100px;">
                <el-select
                  v-model="parmValue.level"
                  style="width: 100%"
                  :size="searchSize"
                  clearable
                  @change="
                    parmValue.page = 1;
                    pageInfo.curr = 1;
                    searchList();
                  "
                  placeholder="优先级"
                >
                  <el-option
                    v-for="item in levelOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="5" style="width: 140px; padding: 0 0 0 10px">
                <el-select
                  v-model="parmValue.type"
                  style="width: 100%"
                  :size="searchSize"
                  clearable
                  @change="
                    parmValue.page = 1;
                    pageInfo.curr = 1;
                    searchList();
                  "
                  placeholder="需求类型"
                >
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    clearable
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="5" style="width: 301px; padding: 0 0 0 10px">
                <period-date-picker
                  :start="parmValue.start"
                  :end="parmValue.end"
                  :width="'134px'"
                  :size="searchSize"
                  @timeReturned="handleTime"
                />
              </el-col>

              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  type="primary"
                  :size="searchSize"
                  style="float: right"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
            </el-row>
            <el-row style="padding: 12px 0 0 0">
              <el-col :span="5" style="width: 180px">
                <el-select
                  v-model="parmValue.status"
                  style="width: 100%"
                  clearable
                  @change="
                    parmValue.page = 1;
                    pageInfo.curr = 1;
                    searchList();
                  "
                  :size="searchSize"
                  placeholder="状态筛选"
                >
                  <el-option
                    v-for="item in seachStatus"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="10" style="width: 410px; padding: 0 0 0 10px">
                <el-input
                  placeholder="关键字"
                  v-model="input"
                  :size="searchSize"
                  class="input-with-select"
                >
                  <el-select
                    v-model="select"
                    slot="prepend"
                    style="width: 100px"
                    placeholder="筛选类型"
                  >
                    <el-option label="需求编号" value="1" />
                    <el-option label="需求标题" value="2" />
                    <el-option label="申请人" value="3" />
                  </el-select>
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="
                      parmValue.page = 1;
                      pageInfo.curr = 1;
                      searchList();
                    "
                  ></el-button>
                </el-input>
              </el-col>
              <el-col
                :span="3"
                style="width: 66px; float: right"
                v-if="powers.some((item) => item == '003')"
              >
                <el-button
                  :size="searchSize"
                  type="success"
                  style="float: right"
                  @click="showModel = true"
                >
                  添加
                </el-button>
              </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.value == scope.row.status) ||
                {}
              ).label || '--'
            "
          ></el-tag>
        </template>
             <template #company_name="{ scope }">
          <el-tag
            style="margin: 0 5px 0 0"
            :size="tablebtnSize"
            type="info"
            v-for="(ditem, dindex) in scope.row.company_name"
            :key="ditem + dindex"
          >
            <span
              v-for="(cItem, cindex) in ditem"
              :key="cItem + dindex + cindex"
              :style="{'marginLeft':cindex>0?'0px':'0px'}"
            >
              <span v-show="cindex > 0">/</span><span>{{ cItem.name }}</span>
            </span>
            </el-tag
          >
        </template>
        <template #level="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.level == '0' ? 'warning' : ''"
            v-text="
              (levelOptions.find((item) => item.value == scope.row.level) || {})
                .label || '--'
            "
          ></el-tag>
        </template>
        <template #types="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.type == '0' ? 'warning' : ''"
            v-text="
              (typeOptions.find((item) => item.value == 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('adjustDetail', { id: scope.row.id })"
            ></i>
          </el-tooltip>
        </template>
      </ex-table>
      <!-- 弹窗 新增/修改 -->
      <add-edit
        :show-model="showModel"
        @refresh="searchList"
        @cancel="showModel = false"
      />
  
    </div>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseAndSale/adjust";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import addEdit from "./addEdit";

import {
  seachStatus,
  statusOptions,
  levelOptions,
  typeOptions,
  columnsDeploy,
} from "./columns";
export default {
  name: "adjust",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
  
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "adjust"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      select: "1",
      input: "",
      seachStatus: seachStatus,
      statusOptions: statusOptions,
      levelOptions: levelOptions,
      typeOptions: typeOptions,
      showModel1: false,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        apply_name: "",
        title: "",
        type: "",
        level: "",
        bugNo: "",
        status: "",
        deal_name: "",
        start: "",
        end: "",
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columnsDeploy,
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },
    // 新建/编辑/详情
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    // 时间
    async handleTime(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;
        this.pageInfo.curr = 1;
        await this.searchList();
      }
    },
    // 刷新表格
    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>