<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container"></div>
    <!-- 搜索区 -->
    <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true">
      <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="客户名称：" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入客户名称" style="width: 150px" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="税号：" prop="socialCreditCode">
          <el-input v-model="queryParams.socialCreditCode" style="width: 150px" placeholder="请输入税号" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="客户负责人：" prop="leaderName">
          <el-input v-model="queryParams.leaderName" placeholder="请输入客户负责人" style="width: 150px" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-form>
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" v-if="flag">
      <el-tab-pane label="全部" name="first">
      </el-tab-pane>
      <el-tab-pane label="我的" name="second">
      </el-tab-pane>
    </el-tabs>
    <!-- 列表区 -->
    <el-table v-loading="loading" :data="companyList" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column label="客户名称" min-width="230" align="center" prop="name" />
      <el-table-column label="税号" min-width="200" align="center" prop="socialCreditCode" />
      <el-table-column label="注册省-市-区" min-width="200" align="center" prop="province">
        <template #default="scope">
          <span>{{
            scope.row.province + "-" + scope.row.city + "-" + scope.row.district
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年收入" align="center" prop="annualIncome" />
      <el-table-column label="纳税类型" align="center" min-width="120" prop="taxType" />
      <el-table-column label="是否零申报" width="80" align="center" prop="isZero">
        <template #default="scope">
          <span>{{
            scope.row.isZero === 1 ? "是" : scope.row.isZero === 0 ? "否" : ""
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客服" align="center" min-width="110" prop="adviserName" />
      <el-table-column label="负责人" align="center" min-width="110" prop="leaderName" />
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 表单 -->
  </div>
</template>

<script setup name="ProductionCompany">
  import {
    listProductionCompany,
    delCompany, demo
  } from "@/api/business/production/productionCompany";
  const { proxy } = getCurrentInstance();
  import useUserStore from "@/store/modules/user";
  /** 字典数组区 */
  /** 查询 对象 */
  const flag = ref(checkPermission(['business:customer:person']))
  const flag2 = ref(checkPermission(['business:customer:myself']))
  const activeName = flag.value ? ref('first') : ref('second');
  const companyList = ref([]);
  const loading = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const tenant = useUserStore().tenant;
  const oneself = ref(false);
  /** 查询对象 */
  const queryParams = ref({
    pageNum: 1,
    pageSize: 20,
    orderByColumn: "create_time",
    category: null,
    code: null,
    name: null,
    shortName: null,
    oldName: null,
    owner: null,
    phone: null,
    email: null,
    contactAddress: null,
    source: null,
    type: null,
    socialCreditCode: null,
    mainBusiness: null,
    legalRepresentative: null,
    foundationDate: null,
    licenceDate: null,
    businessStartDate: null,
    businessEndDate: null,
    isPermanentlyEffective: null,
    registerMoney: null,
    registerMoneyUnit: null,
    provinceCode: null,
    province: null,
    cityCode: null,
    city: null,
    districtCode: null,
    district: null,
    address: null,
    businessField: null,
    taxType: null,
    isZero: null,
    competentTaxAuthority: null,
    taxCollectorName: null,
    taxCollectorPhone: null,
    taxMonth: null,
    openingBank: null,
    bankAccount: null,
    annualIncome: null,
    governmentAccountNo: null,
    governmentPassword: null,
    socialSecurityAccountNo: null,
    socialSecurityPassword: null,
    employeePassword: null,
    housingFundPassword: null,
    housingFundUnitAccount: null,
    housingFundDeductionPassword: null,
    collectionMethod: null,
    quotaAmount: null,
    isPayOnWindow: null,
    isFirstSocialSecurity: null,
    isFirstHousingFund: null,
    leaderId: null,
    adviserId: null,
    leaderName: null,
    adviserName: null,
    principal: tenant.versionId !== '4' && !flag.value ? useUserStore().user.userId : null,
  });

  /***********************  方法区  ****************************/
  onActivated(() => {
    // 你的逻辑
    getList();
  });
  /** 查询company列表 */
  function getList() {
    loading.value = true;
    listProductionCompany(queryParams.value).then((response) => {
      companyList.value = response.rows;
      total.value = response.total;
      loading.value = false;
      console.log(response);
    });
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  function aaa() {
    demo().then(res => {

    })
  }
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    queryParams.value.name = "";
    queryParams.value.socialCreditCode = "";
    queryParams.value.leaderName = "";
    if (activeName.value != null && activeName.value === 'second') {
      queryParams.value.principal = useUserStore().user.userId;
    }
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  /** 是否为自己负责 */
  function handleOneself() {
    if (oneself.value) {
      queryParams.value.principal = useUserStore().user.userId;
    } else {
      queryParams.value.principal = null;
    }
    getList();
  }
  function checkPermission(templatePermission) {
    const all_permission = "*:*:*";
    const permissions = useUserStore().permissions;
    const hasPermissions = permissions.some((permission) => {
      return (
        all_permission === permission || templatePermission.includes(permission)
      );
    });
    return hasPermissions;
  }
  function handleClick(tab) {
    console.log(tab.props.name)
    if (tab.props.name === "second") {
      oneself.value = true
      handleOneself()
    } else {
      oneself.value = false
      handleOneself()
    }
  }
  /** 新增按钮操作 */
  function handleAdd() {
    proxy.$refs.companyRef.open();
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    const id = row.id || ids.value;
    proxy.$refs.companyRef.open(id);
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal
      .confirm("是否确认删除选中的数据项？")
      .then(function () {
        return delCompany(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功！");
      })
      .catch(() => { });
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download(
      "business/company/export",
      {
        ...queryParams.value,
      },
      `company_${new Date().getTime()}.xlsx`
    );
  }

  getList();
</script>