<template>
  <div class="adjust pagePadding">
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="level === '1' ? columns : Pcolumns"
      :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 level === '1' ? seachStatus : statusOptions"
                  :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.company_type"
                style="width: 100%"
                :size="searchSize"
                clearable
                @change="
                  parmValue.page = 1;
                  pageInfo.curr = 1;
                  searchList();
                "
                placeholder="系统类型"
              >
                <el-option
                  v-for="item in company_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  clearable
                >
                </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="0" />
                  <el-option label="工单标题" value="2" />
                  <el-option label="申请人" value="3" />
                  <el-option label="处理人" value="4" v-if="level == '1'" />
                </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 == '001')"
            >
              <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="
            (levelOptions.find((item) => item.value == scope.row.level) || {}).type ||
            'success'
          "
          v-text="
            (levelOptions.find((item) => item.value == scope.row.level) || {}).label ||
            '--'
          "
        ></el-tag>
      </template>

      <template #company_type="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.type == '0' ? 'success' : ''"
          v-text="
            (company_type.find((item) => item.value == scope.row.company_type) || {})
              .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 effect="dark" content="详情" placement="top">
          <i class="el-icon-view tb-icon" @click="getRouter('detail', scope.row.id)"></i>
        </el-tooltip>
      </template>
    </ex-table>
    <!-- 弹窗 新增/修改 -->
    <add-edit
      :show-model="showModel"
      :type="type"
      @refresh="searchList"
      @cancel="showModel = false"
    />
  </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 "@/components/addEdit";

import {
  company_type,
  seachStatus,
  statusOptions,
  levelOptions,
  typeOptions,
  columns,
  Pcolumns,
  parmValue,
} from "@/views/pucColumns";
export default {
  name: "adjust",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size", "level"]),
    powers() {
      return this.$store.getters.btnList;
    },
  },
  data() {
    return {
      select: "1",
      input: "",
      type: 1,
      company_type,
      seachStatus,
      statusOptions,
      levelOptions,
      typeOptions,
      // 表格 - 列参数
      columns, //超管
      Pcolumns, //普通角色
      showModel1: false,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {},

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
    };
  },

  async mounted() {
    this.restSearch(1);
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      const { page, size, select, input } = this.parmValue;
      this.select = select || "";
      this.input = input || "";
      delete this.parmValue["select"];
      delete this.parmValue["input"];
      this.pageInfo = {
        size: size,
        curr: page,
        total: 0,
      };
    }
    this.searchList();
  },
  methods: {
    getRouter(toRouter, queryId) {
      if (toRouter && queryId) {
        let model = {
          type: "view",
          id: queryId,
        };
        let item = JSON.parse(JSON.stringify(this.parmValue));
        item.select = this.select;
        item.input = this.input;

        let routerModel = {
          options: item,
          router: this.$route.path,
        };
        model.preModel = JSON.stringify(routerModel);
        this.routeGoto(toRouter, model);
      } else {
        this.$message.warning("暂未找到相关流程！");
      }
    },
    restSearch(type) {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        ...parmValue,
      };
      if (type !== 1) {
        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;
      }
      console.log(this.level + "");
      this.loading = true;
      this.parmValue.noteNo = this.select === "0" ? this.input : "";
      this.parmValue.bugNo = this.select === "1" ? this.input : "";
      this.parmValue.title = this.select === "2" ? this.input : "";
      this.parmValue.apply_name = this.select === "3" ? this.input : "";
      this.parmValue.deal_name = this.select === "4" ? this.input : "";
      this.parmValue.is_created = this.level + "" === "1" ? "0" : "1";

      const { code, data, message } = await asyncRequest.list(this.parmValue);
      if (code === 0) {
        const { list, count } = data;
        this.tableData = list;
        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>
