<template>
  <div class="relation pagePadding">
    <!--新建 编辑 详情 审核 -->
    <add-view
      :id="modelId"
      :showModel="showModel"
      :is-detail="isDetail"
      :newTime="newTime"
      @refresh="(showModel = false), searchList()"
      @cancel="showModel = false"
    />
    <ex-table
      v-loading="loading"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
      :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 :gutter="10" style="padding: 0 0 10px 80px">
            <el-col :span="7" style="width: 180px">
              <el-select
                style="width: 100%"
                :size="searchSize"
                v-model="parmValue.type"
                clearable
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="关联类型"
              >
                <el-option
                  v-for="item in RQList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="7" style="width: 170px">
              <el-select
                style="width: 100%"
                :size="searchSize"
                v-model="parmValue.status"
                clearable
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="关联状态"
              >
                <el-option
                  v-for="item in relationType"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-col>

            <el-col :span="6" style="width: 320px">
              <period-date-picker
                :start="parmValue.start"
                :end="parmValue.end"
                :type="1"
                :width="'144px'"
                :size="searchSize"
                @timeReturned="timeReturned($event)"
              />
            </el-col>
            <el-col :span="4" style="width: 66px" class="fr">
              <el-button
                v-if="powers.some((item) => item == '002')"
                :size="searchSize"
                type="primary"
                class="fr"
                @click="searchList"
              >
                刷新
              </el-button>
            </el-col>
            <el-col :span="4" style="width: 66px" class="fr">
              <el-button
                type="warning"
                v-if="powers.some((item) => item == '024')"
                :size="searchSize"
                @click="restSearch"
              >
                重置
              </el-button>
            </el-col>
            <el-col :span="4" style="width: 56px" class="fr">
              <el-button
                :size="searchSize"
                type="primary"
                icon="el-icon-search"
                @click="searchList"
              />
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="6" style="width: 470px">
              <el-select
                v-model="code"
                multiple
                filterable
                :size="searchSize"
                remote
                style="width: 100%"
                :multiple-limit="1"
                reserve-keyword
                placeholder="请输入公司名称"
                :remote-method="remoteMethod"
                :loading="selectLoading"
                class="setWidth"
              >
                <el-option
                  v-for="item in activeOptions"
                  :key="item.companyNo"
                  :label="item.companyName"
                  :value="item.companyNo"
                />
              </el-select>
            </el-col>
            <el-col :span="4" style="width: 330px">
              <el-input
                placeholder="请输入编号"
                v-model="parmValue.codeNo"
                :size="searchSize"
                class="input-with-select"
              >
                <el-select
                  style="width: 105px"
                  :size="searchSize"
                  v-model="parmValue.codeType"
                  slot="prepend"
                >
                  <el-option
                    v-for="(item, index) in selectList"
                    :key="item.code + index"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-input>
            </el-col>
            <el-col :span="4" style="width: 66px" class="fr">
              <el-button
                v-if="powers.some((item) => item == '003')"
                type="success"
                class="fr"
                style="margin-left: 0px"
                :size="searchSize"
                @click="openModal('add', '003')"
              >
                添加
              </el-button>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #orderNo="{ scope }">
        <el-popover
          placement="top"
          width="180"
          v-if="scope.row.orderNo_length > 1"
          trigger="hover"
        >
          <ul class="my-tag-ul-main" style="list-style-type: disc">
            <li
              style="
                font-size: 12px;
                line-height: 20px;
                list-style-type: disc;
                list-style-position: inside;
                display: list-item;
              "
              v-for="(key1, ki1) in scope.row.orderNo_showMain"
              :key="ki1 + scope.row.orderNo + key1"
            >
              {{ key1 }}
            </li>
          </ul>
          <p
            slot="reference"
            style="display: inline-block; vertical-align: top; cursor: pointer"
          >
            <span style="display: inline-block; vertical-align: top">{{
              scope.row.orderNo
            }}</span
            ><span
              style="
                display: inline-block;
                vertical-align: top;
                padding: 0 0 0 2px;
              "
              ><i
                class="el-icon-more"
                style="
                  font-size: 12px;
                  height: 23px;
                  display: inline-block;
                  line-height: 30px;
                "
              ></i>
            </span>

            <!-- {{ scope.row.orderNo }} -->
          </p>
        </el-popover>
        <p v-else>{{ scope.row.orderNo }}</p>
      </template>
      <template #traNo="{ scope }">
        <el-popover
          placement="top"
          width="200"
          v-if="scope.row.traNo_length > 1"
          trigger="hover"
        >
          <ul style="list-style-type: disc; list-style-position: inside">
            <li
              style="
                font-size: 12px;
                line-height: 20px;
                list-style-type: disc;
                list-style-position: inside;
                display: list-item;
              "
              v-for="(key1, ki1) in scope.row.traNo_showMain"
              :key="ki1 + scope.row.traNo + key1"
            >
              {{ key1 }}
            </li>
          </ul>
          <p
            slot="reference"
            style="display: inline-block; vertical-align: top; cursor: pointer"
          >
            <span style="display: inline-block; vertical-align: top">{{
              scope.row.traNo
            }}</span
            ><span
              style="
                display: inline-block;
                vertical-align: top;
                padding: 0 0 0 2px;
              "
              ><i
                class="el-icon-more"
                style="
                  font-size: 12px;
                  height: 23px;
                  display: inline-block;
                  line-height: 30px;
                "
              ></i>
            </span>
          </p>
        </el-popover>
        <p v-else>{{ scope.row.traNo }}</p>
      </template>
      <template #invNo="{ scope }">
        <el-popover
          placement="top"
          width="200"
          v-if="scope.row.invNo_length > 1"
          trigger="hover"
        >
          <ul style="list-style-type: disc; list-style-position: inside">
            <li
              style="
                font-size: 12px;
                line-height: 20px;
                list-style-type: disc;
                list-style-position: inside;
                display: list-item;
              "
              v-for="(key1, ki1) in scope.row.invNo_showMain"
              :key="ki1 + scope.row.invNo + key1"
            >
              {{ key1 }}
            </li>
          </ul>
          <p
            slot="reference"
            style="display: inline-block; vertical-align: top; cursor: pointer"
          >
            <span style="display: inline-block; vertical-align: top">{{
              scope.row.invNo
            }}</span
            ><span
              style="
                display: inline-block;
                vertical-align: top;
                padding: 0 0 0 2px;
              "
              ><i
                class="el-icon-more"
                style="
                  font-size: 12px;
                  height: 23px;
                  display: inline-block;
                  line-height: 28px;
                "
              ></i>
            </span>
          </p>
        </el-popover>
        <p v-else>{{ scope.row.invNo }}</p>
      </template>
      <template #rela_form="{ scope }">
        <el-tag
          size="mini"
          :type="''"
          v-text="
            (RQList.find((item) => item.code == scope.row.rela_form) || {})
              .name || '--'
          "
        ></el-tag>
      </template>
      <template #status="{ scope }">
        <el-tag
          size="mini"
          :type="
            scope.row.status == '3'
              ? 'success'
              : scope.row.status == '2'
              ? 'warning'
              : ''
          "
          v-text="
            (relationType.find((item) => item.code == scope.row.status) || {})
              .name || '--'
          "
        ></el-tag>
      </template>

      <template #operation="{ scope }">
        <el-tooltip
          v-if="
            (powers.some((item) => item == '007') &&
              scope.row.rela_form === '2') ||
            (powers.some((item) => item == '007') &&
              scope.row.rela_form === '3')
          "
           
          effect="dark"
          content="详情"
          placement="top"
        >
          <i
            class="el-icon-view tb-icon"
            @click="openModal(scope.row.id, '007')"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            (powers.some((item) => item == '027') &&
              scope.row.status !== '3' &&
              scope.row.rela_form === '2') ||
            (powers.some((item) => item == '027') &&
              scope.row.status !== '3' &&
              scope.row.rela_form === '3')
          "
           
          effect="dark"
          content="申请取消"
          placement="top"
        >
          <i
            class="el-icon-video-pause tb-icon"
            @click="changeStatus(scope.row.id)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            (powers.some((item) => item == '029') &&
              scope.row.status === '1' &&
              scope.row.rela_form === '2') ||
            (powers.some((item) => item == '029') &&
              scope.row.status === '1' &&
              scope.row.rela_form === '3')
          "
           
          effect="dark"
          content="财务审核"
          placement="top"
        >
          <i
            class="el-icon-s-check tb-icon"
            @click="openModal(scope.row.id, '029')"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import ExTable from "@/components/ExTableNew.vue";
import addView from "./addView";
import asyncRequest from "@/apis/service/InvoiceSales/relation";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import { relationType, RQList } from "@/assets/js/linkType";
import PeriodDatePicker from "@/components/PeriodDatePicker";
import resToken from "@/mixins/resToken";
export default {
  name: "relation",
  mixins: [mixinPage, resToken],
  components: {
    ExTable,
    addView,
    PeriodDatePicker,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "relation"
        ) || {};
      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: "资金单号",
        },
      ],
      newTime: 0,
      code: [],
      relationType: relationType,
      RQList: RQList,
      selectLoading: false,
      activeOptions: [],
      name: "",
      contector: "",
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        companyNo: "",
        type: "",
        status: "",
        start: "",
        end: "",
        codeType: "1",
        codeNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "id",
          label: "ID",
          fixed: "left",
          width: "70px",
        },
        {
          prop: "companyNo",
          label: "公司编码",
          width: "130px",
        },

        {
          prop: "companyName",
          label: "公司名称",
          "min-width": "230px",
        },
        {
          prop: "cancel_total",
          label: "核销额度",
          width: "120px",
        },
        {
          prop: "rela_form",
          label: "关联类型",
          _slot_: "rela_form",
          width: "120px",
        },
        {
          prop: "orderNo",
          label: "订单编号",
          width: "180px",
          _slot_: "orderNo",
        },
        {
          prop: "traNo",
          label: "资金编号",
          width: "250px",
          _slot_: "traNo",
        },
        {
          prop: "invNo",
          label: "发票编号",
          width: "200px",
          _slot_: "invNo",
        },
        {
          prop: "status",
          label: "状态",
          _slot_: "status",
          width: "110px",
        },
        {
          prop: "addtime",
          label: "创建时间",
          sortable: true,
          width: "145px",
        },
        {
          prop: "",
          label: "操作",
          width: "120px",
          fixed: "right",
          _noset_: true,
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.showModel = false;
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        companyNo: "",
        type: "",
        status: "",
        start: "",
        end: "",
        codeType: "1",
        codeNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },
    // 新建/编辑/详情
    openModal(id, isDetail) {
      this.showModel = true;
      this.changea();
      this.modelId = id;
      this.isDetail = isDetail;
    },
    /**
     * 启用/禁用
     * @param {String} id id
     */
    async changeStatus(id) {
      await this.$confirm(`确定要取消申请？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await asyncRequest.delete({ id: id });
          if (res && res.code === 0) {
            this.$notify.success({
              title: "取消申请成功",
              message: "",
            });
            this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    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 searchList() {
      this.loading = true;
      this.parmValue.companyNo = this.code.join(",");
      let obj = JSON.parse(JSON.stringify(this.parmValue));
      const res = await asyncRequest.list(obj);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map((v1) => {
          let arr1 = v1.orderNo.split(",");
          console.log(arr1);
          v1.orderNo = arr1[0];
          v1.orderNo_length = arr1.length;
          // v1.orderNo_show = false;
          v1.orderNo_showMain = arr1;

          let arr2 = v1.traNo.split(",");
          v1.traNo = arr2[0];
          v1.traNo_length = arr2.length;
          // v1.traNo_show = false;
          v1.traNo_showMain = arr2;

          let arr3 = v1.invNo.split(",");
          v1.invNo = arr3[0];
          v1.invNo_length = arr3.length;
          // v1.invNo_show = false;
          v1.invNo_showMain = arr3;

          return v1;
        });
        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 remoteMethod(query) {
      this.selectLoading = true;
      if (query !== "") {
        this.activeOptions = [];
        const res = await asyncRequest.clist({
          page: 1,
          size: 100,
          company: query,
        });
        if (res && res.code === 0 && res.data && res.data.list) {
          this.activeOptions = res.data.list;
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      } else {
        this.activeOptions = [];
      }
      this.selectLoading = false;
    },
    changea() {
      this.newTime = new Date().valueOf();
    },
  },
};
</script>

<style lang="scss" scoped>
.relation {
  position: absolute;
  width: 100%;
  height: calc(100% - 84px);
  box-sizing: border-box;
  overflow: scroll;
}
</style>
