<template>
  <div class="bargainList pagePadding">
     <ex-table
        v-loading="loading"
        :table="table"
        :data="tableData"
        :columns="columns"
        :page="pageInfo"
        :size="size"
        @page-curr-change="handlePageChange"
        @page-size-change="handlePageChange"
        @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 style="padding: 0 0 0 80px">
              <el-col :span="4" style="width: 301px; padding: 0 0 0px 0px">
                <period-date-picker
                  :start="parmValue.createStart"
                  :end="parmValue.createEnd"
                  :width="'134px'"
                  :size="searchSize"
                  @timeReturned="handleTime"
                />
              </el-col>

              <el-col :span="3" style="margin-right:10px">
                <el-select
                  style="width:100%"
                  :size="searchSize"
                  v-model="parmValue.status"
                  clearable
                  placeholder="申请状态"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in statusList"
                    :label="item.label"
                    :value="item.id"
                    :key="item.id"
                  />
                </el-select>
              </el-col>

              <el-col :span="4">
                <!-- <el-select
                  style="width:100%"
                  :size="searchSize"
                  v-model="parmValue.customer_id"
                  clearable
                  placeholder="需求企业"
                  @change="pageInfo.curr = 1;parmValue.page = 1;searchList();"
                >
                  <el-option />
                </el-select> -->
                <search-customer 
                  size="mini" 
                  placeholder="需求企业" 
                  :value.sync="parmValue.customer_id"
                  @change="pageInfo.curr = 1;parmValue.page = 1;searchList();"
                />
              </el-col>

              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right; margin-left: 5px"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>

              <el-col :span="4" class="fr" style="width: 66px">
                <el-button
                  type="warning"
                  class="fr"
                  :size="searchSize"
                  @click="restSearch"
                >
                  重置
                </el-button>
              </el-col>
            </el-row>

            <el-row  style="margin-top: 10px">
              <el-col :span="4" style="margin-right:10px">
                <el-input
                  clearable
                  :size="searchSize"
                  placeholder="开票申请编号"
                  v-model="parmValue.invNo"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                />
              </el-col>

              <el-col :span="4" style="margin-right:10px">
                <el-input
                  clearable
                  :size="searchSize"
                  placeholder="需求订单编号"
                  v-model="parmValue.reqCode"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                />
              </el-col>

              <el-col :span="4">
                <el-input
                  clearable
                  :size="searchSize"
                  placeholder="发票号码"
                  v-model="parmValue.invoiceNumber"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                /> 
              </el-col>

              <div class="fr">
                <el-button type="success" size="mini" @click="linkToCreateInvoice">添加</el-button>
              </div>
            </el-row>

            <el-row style="margin-top:10px">
              <el-col :span="4">
                <el-select
                  style="width:100%"
                  :size="searchSize"
                  v-model="parmValue.invoiceType"
                  clearable
                  placeholder="发票类型"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option />
                </el-select>
              </el-col>
            </el-row>
          </div>
       </template>
       <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="(statusList.find((item) => item.id == scope.row.status) || {}).type || ''"
            v-text="(statusList.find((item) => item.id == scope.row.status) || {}).label || '--'"
          ></el-tag>
       </template>

       <template #invoice_type="{ scope: { row } }">
        <el-tag size="mini">
          {{  
            (invoiceTypeList.find(({value}) => value === String(row.invoice_type)) || {}).label || '--'
          }}
        </el-tag>
       </template>
       <template #operation="{ scope }">
          <el-tooltip
            effect="dark"
            content="详情"
            placement="top"
          >
            <i
              class="el-icon-view tb-icon"
              
               @click="getRouter('invoiceApplyDetail', scope.row.id )"
            ></i>
          </el-tooltip>
       </template>
     </ex-table>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/customerService/invoiceApply";
import { invoiceTypeList } from '@/assets/js/statusList';
import companyHelper from "@/mixins/companyHelper"
import { listCol, statusList } from "./columns";
import { mapGetters } from "vuex";



/**
 *@props
 createStart//创建开始时间
 createEnd //创建结束时间
 customer_id //需求企业
 invoiceNumber //发票号码
 invoiceType //发票类型
 invNo //发票申请编号
 reqCode //需求订单编号
 status //申请状态
*/
export default {
  name: "bargainList",
  mixins: [mixinPage, resToken, companyHelper],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "invoiceApply"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      invoiceTypeList,
      customerCode: [], //客户公司code
      statusList,
      sitem: null,
      loading: false,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        createStart:"",//创建开始时间
        createEnd:"", //创建结束时间
        customer_id:"", //需求企业
        invoiceNumber:"", //发票号码
        invoiceType:"", //发票类型
        invNo:"", //发票申请编号
        reqCode:"", //需求订单编号
        status:"", //申请状态
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: listCol,

      resultValue: "", //处理结果
    };
  },
  mounted() {
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      console.log(this.parmValue)
      const { page, size } = this.parmValue;
      // this.parmValue.createStart = start || last_start;
      // this.parmValue.createEnd = end || last_end;

      // if(this.parmValue.customer_code.length>0){
      //     this.customerCode = [this.parmValue.customer_code] ;
      // }
      
       this.pageInfo = {
        size: size,
        curr: page,
        total: 0,
      }
      //多选条件
    this.select = this.parmValue.select;
    // this.sselect = this.parmValue.sselect;
    this.s_input = this.parmValue.s_input

    }else{
       this.select = "1";
      //  this.sselect = "创建时间"
    }
    this.searchList();
  },

  methods: {
    linkToCreateInvoice(){
      this.$router.push(`/customerService/invoiceApplyDetail?id=add`)
    },
    getRouter(toRouter, queryId){
      this.$router.push(toRouter + '?id=' + queryId)
        // let routerModel = {
        //   options: JSON.parse(JSON.stringify(this.parmValue)),
        //   router: this.$route.path,
        // };

        // model.preModel = JSON.stringify(routerModel);
        
        // this.routeGoto(toRouter, model);
    },
    restSearch() {
      this.customerCode = [];
      this.select = "1";
      this.s_input = "";
      this.parmValue = {
        createStart:"",//创建开始时间
        createEnd:"", //创建结束时间
        customer_id:"", //需求企业
        invoiceNumber:"", //发票号码
        invoiceType:"", //发票类型
        invNo:"", //发票申请编号
        reqCode:"", //需求订单编号
        status:"", //申请状态
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },

    openModal(id, isDetail, sitem) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
      this.sitem = sitem;
    },
    async deleteById(id, status) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
          const model = {
            id: id,
            status: status + '' === "1" ? "0" : "1",
          };
          const res = await asyncRequest.status(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.createStart !== "" && this.parmValue.createEnd === "") ||
        (this.parmValue.createStart === "" && this.parmValue.createEnd !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }

      this.loading = true;
      
      const res = await asyncRequest.list(this.parmValue);

      if (res && res.code === 1 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },

    async statusConfirm(id, status) {
      let str = status + '' === "1" ? "禁用" : "启用";
      await this.$confirm("确定要改为" + str + "?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          const model = {
            id: id,
            status: status + '' === "1" ? "0" : "1",
          };
          const res = await asyncRequest.status(model);
          if (res && res.code === 0) {
            this.loading = false;
            this.$notify.success({
              title: "状态修改成功！",
              message: "",
            });
            await this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    // 时间选择事件
    async handleTime(e) {
      if (e.startTime !== "") {
        this.parmValue.createStart = e.startTime;
      } else {
        this.parmValue.createStart = "";
      }
      if (e.endTime !== "") {
        this.parmValue.createEnd = e.endTime;
      } else {
        this.parmValue.createEnd = "";
      }
      if (this.parmValue.createStart !== "" && this.parmValue.createEnd !== "") {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    async customerChange(e) {
      const { code } = e;
      this.parmValue.customer_code = code ? [code] : [];
      this.parmValue.supplierName = e.label;
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
  },
};
</script>
   <style lang="scss" scoped>
.bargainList {
}
</style>
   