<template>
  <div class="invoicePool pagePadding">
    <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="8" style="width: 460px">
              <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="8" style="width: 290px">
              <el-input
                :size="searchSize"
                v-model="parmValue.invNo"
                placeholder="请输入发票编号"
              ></el-input>
            </el-col>
            <el-col :span="4" style="width: 66px" class="fr tr">
              <el-button
                v-if="powers.some((item) => item == '002')"
                :size="searchSize"
                type="primary"
                @click="searchList"
              >
                刷新
              </el-button>
            </el-col>
          </el-row>
          <el-row :gutter="10" style="padding-bottom: 10px">
            <el-col :span="7" style="width: 170px">
              <el-select
                style="width: 100%"
                :size="searchSize"
                v-model="parmValue.trade_status"
                clearable
                placeholder="关联资金状态"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in capitalType"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="6" style="width: 340px">
              <number-range
                :start="parmValue.total_low"
                :end="parmValue.total_up"
                :type="1"
                :width="'154px'"
                :title="'资金总额'"
                :size="searchSize"
                @numReturned="numReturned($event, 0)"
              />
            </el-col>
            <el-col :span="7" style="width: 170px">
              <el-select
                style="width: 100%"
                :size="searchSize"
                v-model="parmValue.type_check"
                clearable
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="开票类型"
              >
                <el-option
                  v-for="item in billType"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4" style="width: 66px" class="fr tr">
              <el-button
                type="warning"
                v-if="powers.some((item) => item == '024')"
                :size="searchSize"
                @click="restSearch"
              >
                重置
              </el-button>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="7" style="width: 170px">
              <el-select
                style="width: 100%"
                :size="searchSize"
                v-model="parmValue.order_status"
                clearable
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="关联确认单状态"
              >
                <el-option
                  v-for="item in orderType"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="6" style="width: 340px">
              <period-date-picker
                :start="parmValue.starttime"
                :end="parmValue.endtime"
                :type="1"
                :width="'154px'"
                :size="searchSize"
                @timeReturned="timeReturned($event)"
              />
            </el-col>
            <el-col :span="4" style="width: 54px" class="fr tr">
              <el-button
                :size="searchSize"
                type="primary"
                icon="el-icon-search"
                @click="searchList"
              />
            </el-col>
          </el-row>
        </div>
      </template>
      <template #trade_status="{ scope }">
        <el-tag size="mini" v-if="scope.row.trade_status == '1'" type="danger"
          >未核销</el-tag
        >
        <el-tag
          size="mini"
          v-else-if="scope.row.trade_status == '2'"
          type="warning"
          >部分核销</el-tag
        >
        <el-tag size="mini" v-else type="success">全部核销</el-tag>
      </template>
      <template #type_check="{ scope }">
        <el-tag
          size="mini"
          v-text="
            (billType.find((item) => item.code == scope.row.type_check) || {})
              .name || '--'
          "
        />
      </template>

      <template #order_status="{ scope }">
        <el-tag size="mini" v-if="scope.row.order_status == '1'" type="danger"
          >未核销</el-tag
        >
        <el-tag
          size="mini"
          v-else-if="scope.row.order_status == '2'"
          type="warning"
          >部分核销</el-tag
        >
        <el-tag size="mini" v-else type="success">全部核销</el-tag>
      </template>
      <template #operation="{ scope }">
        <el-link
          v-if="powers.some((item) => item == '007')"
          type="primary"
          :size="tablebtnSize"
          :underline="false"
          style="margin: 0 5px"
          @click="
            openModal(scope.row.id, scope.row.name, scope.row.contector, true)
          "
        >
          详情
        </el-link>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import ExTable from "@/components/ExTableNew.vue";
import asyncRequest from "@/apis/service/InvoiceSales/invoicePool";
import mixinPage from "@/mixins/elPaginationHandle";
import PeriodDatePicker from "@/components/PeriodDatePicker";
import numberRange from "@/components/numberRange";
import { capitalType, orderType } from "@/assets/js/linkType";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "invoicePool",
  mixins: [mixinPage, resToken],
  components: {
    ExTable,
    PeriodDatePicker,
    numberRange,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "invoicePool"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      orderType: orderType,
      capitalType: capitalType,
      billType: [
        {
          code: "1",
          name: "确认单开票",
        },
        {
          code: "2",
          name: "资金开票-资金未认领",
        },
        {
          code: "3",
          name: "资金开票-资金已认领",
        },
      ],
      code: [],
      selectLoading: false,
      activeOptions: [],
      name: "",
      contector: "",
      loading: false,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        companyNo: "",
        order_status: "",
        trade_status: "",
        total_low: "",
        total_up: "",
        starttime: "",
        endtime: "",
        type_check: "",
        status: "",
        invNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "invNo",
          label: "发票编号",
          fixed: "left",
          width: "180px",
        },
        {
          prop: "inv_in",
          label: "公司编码",
          width: "130px",
        },
        {
          prop: "company",
          label: "公司名称",
          "min-width": "170px",
        },
         {
          prop: "kp_company",
          label: "销售方开票公司名称",
          "min-width": "180px",
        },
        {
          prop: "type_check",
          label: "开票类型",
          _slot_: "type_check",
          width: "150px",
        },

        {
          prop: "inv_value",
          label: "发票额度",
          width: "120px",
        },
        {
          prop: "atrade_fee",
          label: "资金已核销金额",
          width: "120px",
        },
        {
          prop: "fund_used",
          label: "资金处理中的金额",
          width: "120px",
        },
        {
          prop: "wtrade_fee",
          label: "资金待核销金额",
          width: "120px",
        },
        {
          prop: "trade_status",
          label: "关联资金状态",
          _slot_: "trade_status",
          width: "120px",
        },
        {
          prop: "aorder_fee",
          label: "确认单已核销金额",
          width: "120px",
        },
        {
          prop: "order_used",
          label: "确认单处理中的金额",
          width: "130px",
        },
        {
          prop: "worder_fee",
          label: "确认单待核销金额",
          width: "120px",
        },
        {
          prop: "order_status",
          label: "关联确认单状态",
          _slot_: "order_status",
          width: "120px",
        },
        {
          prop: "addtime",
          label: "创建时间",
          sortable: true,
          width: "150px",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      this.code = [];
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        companyNo: "",
        order_status: "",
        trade_status: "",
        total_low: "",
        total_up: "",
        starttime: "",
        endtime: "",
        type_check: "",
        status: "",
        invNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },
    // 新建/编辑/详情
    openModal(id, name, contector, isDetail) {
      this.contector = contector;
      this.name = name;
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    /**
     * 启用/禁用
     * @param {String} id id
     * @param {String} status 0-禁用 1-启用
     * @param {Boolean} type 是否有权限操作
     */
    async changeStatus(id, status, type) {
      if (!type) {
        return;
      }
      await this.$confirm(`确定要改为${status === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            roleid: id,
            status: status === "1" ? "0" : "1",
          };
          const res = await asyncRequest.delete(model);
          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 searchList() {
      if (
        (this.parmValue.starttime !== "" && this.parmValue.endtime === "") ||
        (this.parmValue.starttime === "" && this.parmValue.endtime !== "")
      ) {
        this.$message.warning("时间区间有误！");
        return;
      }
      if (
        (this.parmValue.total_low !== "" && this.parmValue.total_up === "") ||
        (this.parmValue.total_low === "" && this.parmValue.total_up !== "")
      ) {
        this.$message.warning("发票总额区间有误！");
        return;
      }
      this.loading = true;
      this.parmValue.companyNo = this.code.join(",");
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        // this.tableData.map((v1) => {
        //   v1.addtime = v1.addtime.replace(".000", "");
        //   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;
    },
  },
};
</script>

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