<template>
  <div class="returnTicket 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>
            <el-col :span="24" style="padding: 0 0 10px 80px">
              <el-col :span="12" style="width: 450px">
                <el-select
                  v-model="code"
                  multiple
                  filterable
                  remote
                  :multiple-limit="1"
                  reserve-keyword
                  :size="searchSize"
                  style="width: 100%"
                  placeholder="公司名称"
                  :remote-method="remoteMethod"
                  :loading="selectLoading"
                  @change="selectChange"
                >
                  <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: 290px; padding-left: 10px">
                <el-input
                  :size="searchSize"
                  placeholder="请输入发票编号"
                  v-model="parmValue.relaNo"
                  class="input-with-select"
                >
                  <el-button
                    slot="append"
                    @click="searchList"
                    icon="el-icon-search"
                  ></el-button>
                </el-input>
              </el-col>

              <el-col :span="4" style="width: 66px; float: right">
                <el-button
                  v-if="powers.some((item) => item == '002')"
                  :size="searchSize"
                  type="primary"
                  style="float: right"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
              <el-col :span="4" style="width: 66px; float: right">
                <el-button
                  type="warning"
                  style="float: right"
                  v-if="powers.some((item) => item == '024')"
                  :size="searchSize"
                  @click="restSearch"
                >
                  重置
                </el-button>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="4" style="width: 190px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.inv_type"
                  placeholder="原发票开票类型"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <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: 190px; padding: 0 0 0 10px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.refund_status"
                  placeholder="退票状态"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="4" style="width: 190px; padding: 0 0 0 10px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.back_type"
                  placeholder="退票类型"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in billType3"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="4" style="width: 66px; float: right">
                <el-button
                  v-if="powers.some((item) => item == '003')"
                  type="success"
                  :size="searchSize"
                  style="float: right"
                  @click="openModal('add', '003')"
                >
                  添加
                </el-button>
              </el-col>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #refund_status="{ scope }">
        <el-tag
          :type="scope.row.refund_status == '4' ? 'success' : 'warning'"
          size="mini"
          v-text="
            (
              statusOptions.find(
                (item) => item.code == scope.row.refund_status
              ) || {}
            ).name || '--'
          "
        ></el-tag>
      </template>
      <template #inv_type="{ scope }">
        <el-tag
          size="mini"
          v-text="
            (billType.find((item) => item.code == scope.row.inv_type) || {})
              .name || '--'
          "
        />
      </template>
      <template #operation="{ scope }">
        <el-tooltip
          v-if="
            powers.some((item) => item == '028') &&
            scope.row.refund_status === '1'
          "
           
          effect="dark"
          content="业务审核"
          placement="top"
        >
          <i
            class="el-icon-finished tb-icon"
            @click="
              openModal(scope.row.refundNo, '028', scope.row.refund_status)
            "
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            powers.some((item) => item == '029') &&
            scope.row.refund_status === '2'
          "
           
          effect="dark"
          content="财务审核"
          placement="top"
        >
          <i
            class="el-icon-finished tb-icon"
            @click="
              openModal(scope.row.refundNo, '029', scope.row.refund_status)
            "
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            powers.some((item) => item == '048') &&
            scope.row.refund_status === '3'
          "
           
          effect="dark"
          content="财务核销发票"
          placement="top"
        >
          <i
            class="el-icon-finished tb-icon"
            @click="
              openModal(scope.row.refundNo, '048', scope.row.refund_status)
            "
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            powers.some((item) => item == '027') &&
            scope.row.refund_status !== '4'
          "
           
          effect="dark"
          content="取消申请"
          placement="top"
        >
          <i
            class="el-icon-error tb-icon"
            @click="changeStatus(scope.row.refundNo)"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
    <!-- 新建 /详情 /审核-->

    <add-edit
      :id="modelId"
      :show-model="showModel"
      :is-detail="isDetail"
      :dstatus="dstatus"
      @refresh="searchList"
      @cancel="showModel = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/InvoiceSales/returnTicket";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import urlConfig from "@/apis/url-config";
export default {
  name: "returnTicket",
  mixins: [mixinPage, resToken],
  components: {
    "add-edit": () => import("./addEdit"),
    "ex-table": () => import("@/components/ExTableNew.vue"),
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "returnTicket"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      showModel: false,
      isDetail: false,
      status: "",
      selection: [],
      invNo: "0",
      status: "0",
      modelId: 0,
      selectLoading: false,
      billType: [
        {
          code: "1",
          name: "确认单开票",
        },
        {
          code: "2",
          name: "资金开票-资金未认领",
        },
        {
          code: "3",
          name: "资金开票-资金已认领",
        },
      ],

      billType3: [
        {
          code: "1",
          name: "原发票",
        },
        {
          code: "2",
          name: "红冲发票",
        },
      ],
      activeOptions: [],
      // 状态
      statusOptions: [
        {
          code: "1",
          name: "待业务审核",
        },
        {
          code: "2",
          name: "待财务审核",
        },
        {
          code: "3",
          name: "待财务核销票",
        },
        {
          code: "4",
          name: "退票已完成",
        },
      ],

      name: "",
      downLoading: false,
      code: [],
      loading: false,

      formValue: {
        company: "", // 账户
        page: 1, // 页码
        size: 100, // 每页显示条数
      },
      fileUrl:
        urlConfig.baseURL +
        "%E5%8F%91%E7%A5%A8%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xls",
      parmValue: {
        companyNo: "",
        type: "2",
        page: 1, // 页码
        size: 15, // 每页显示条数
        relaNo: "",
        inv_type: "",
        back_type: "",
        refund_status: "",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        { prop: "refundNo", label: "退票编号", width: "180px", fixed: "left" },
        {
          prop: "invNo",
          label: "原发票编号",
          width: "180px",
        },
        {
          prop: "customerNo",
          label: "客户企业编码",
          width: "130px",
        },
        {
          prop: "customer",
          label: "客户企业名称",
          "min-width": "180px",
        },
        {
          prop: "refund_fee",
          label: "退票额度",
          width: "120px",
        },
        // {
        //   prop: "contactor",
        //   label: "客户联系人",
        //   width: "85px",
        // },
        {
          prop: "inv_type",
          label: "开票类型",
          _slot_: "inv_type",
          width: "150px",
        },
        {
          prop: "apply_name",
          label: "申请人",
          width: "85px",
        },
        {
          prop: "refund_status",
          label: "退票状态",
          _slot_: "refund_status",
          width: "120px",
        },
        {
          prop: "addtime",
          label: "申请时间",
          sortable: true,
          width: "150px",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          _noset_: true,
           width: "110px",
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    async selectChange() {
      let arr = JSON.parse(JSON.stringify(this.code));
      this.parmValue.companyNo = arr.join(",");
      this.parmValue.page = 1;
      await this.searchList();
    },
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        companyNo: "",
        type: "2",
        page: 1, // 页码
        size: 15, // 每页显示条数
        relaNo: "",
        inv_type: "",
        back_type: "",
        refund_status: "",
      };
      this.searchList();
    },
    // 新建/编辑/详情
    openModal(id, isDetail, status) {
      this.modelId = id;
      this.dstatus = status ? status : "";
      this.isDetail = isDetail;
      this.showModel = true;
    },
    /**
     * 取消申请
     * @param {String} refundNo refundNo
     */
    async changeStatus(refundNo) {
      await this.$confirm(`确定要取消申请？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            refundNo: refundNo,
          };
          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() {
      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;
    },
    async remoteMethod(query) {
      this.selectLoading = true;
      if (query !== "") {
        this.activeOptions = [];
        this.formValue.company = query;
        const res = await asyncRequest.clist(this.formValue);
        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>
