<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>
        <el-form-item label="公司名称" prop="company_name">
          <el-input
            class="w-[280px]"
            v-model="queryParams.company_name"
            clearable
            placeholder="请输入公司名称"
          />
        </el-form-item>
        <el-form-item label="公司类型" prop="company_type">
          <el-select
            v-model="queryParams.company_type"
            placeholder="公司类型"
            clearable
            @change="changeType"
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
          <!-- <el-input
            class="w-[280px]"
            v-model="queryParams.company_type"
            clearable
            placeholder="请输入公司类型"
          /> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <div class="mt-4">
        <el-table :data="pager.lists" @cell-click="handleCurrentChange">
          <el-table-column label="公司名称" property="company_name" />
          <el-table-column label="公司类型" property="company_type">
            <template #default="{ row }">
              <span
                >{{typeList.find((item: any)=>item.id==row.company_type).name}}</span
              >
            </template>
          </el-table-column>
          <el-table-column label="区县" property="area_name" />
          <el-table-column label="乡镇" property="street_name" />
          <el-table-column label="主联系人" property="master_name" />
          <el-table-column label="联系方式" property="master_phone" />
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup name="companyLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import {
  apiCompanyLists,
  companyListTwo,
  apiCompanyDelete,
} from "@/api/company";
import { defineEmits } from "vue";

// 当前类型
const props = defineProps({
  type: {
    type: Number,
    default: 0,
  },
  companyTypeList: {
    type: Array,
    default: () => {
      return [];
    },
  },
});

const typeList = props.companyTypeList.filter((item: any) => {
  return item.id == 30 || item.id == 16;
});

// 查询条件
const queryParams = reactive({
  level_two: "",
  level_one: "",
  company_name: "",
  organization_code: "",
  city: "",
  area: "",
  street: "",
  company_type: "",
  master_name: "",
  master_position: "",
  master_phone: "",
  master_email: "",
  other_contacts: "",
  area_manager: "",
  is_contract: "",
  account: "",
  password: "",
  deposit: "",
  deposit_time: "",
  qualification: "",
  status: "",
});

if (props.type) queryParams.company_type = props.type;

// 选中数据
const emits = defineEmits(["customEvent"]);

// 选中数据子父传递
const handleCurrentChange = (value: any) => {
  emits("customEvent", value);
};

// 选择公司类型
const changeType = (e: any) => {
  getLists();
};

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: companyListTwo,
  params: queryParams,
});

getLists();
</script>
