<template>
  <div class="app-container">
    <el-tabs v-model="activeTab" @tab-change="handleTabChange">
      <el-tab-pane label="单位信息" name="company">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="单位名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入单位名称" 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-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['pms:company:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['pms:company:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['pms:company:remove']">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
        </el-row>

        <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="单位名称" align="center" prop="name" min-width="200" :show-overflow-tooltip="true" />
          <el-table-column label="单位类型" align="center" prop="type" width="120" />
          <el-table-column label="联系人" align="center" prop="contact" width="120" />
          <el-table-column label="联系电话" align="center" prop="tel" width="130" />
          <el-table-column label="操作" align="center" width="150">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['pms:company:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['pms:company:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-tab-pane>

      <el-tab-pane label="联系人" name="contact">
        <el-form :model="contactQueryParams" ref="contactQueryRef" :inline="true" v-show="showSearch">
          <el-form-item label="联系人" prop="name">
            <el-input v-model="contactQueryParams.name" placeholder="请输入联系人" clearable @keyup.enter="handleContactQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleContactQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetContactQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleContactAdd" v-hasPermi="['pms:contact:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="contactSingle" @click="handleContactUpdate" v-hasPermi="['pms:contact:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="contactMultiple" @click="handleContactDelete" v-hasPermi="['pms:contact:remove']">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getContactList" />
        </el-row>

        <el-table v-loading="contactLoading" :data="contactList" @selection-change="handleContactSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="联系人" align="center" prop="name" min-width="150" />
          <el-table-column label="手机号" align="center" prop="phone" width="130" />
          <el-table-column label="邮箱" align="center" prop="email" min-width="200" />
          <el-table-column label="操作" align="center" width="150">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleContactUpdate(scope.row)" v-hasPermi="['pms:contact:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleContactDelete(scope.row)" v-hasPermi="['pms:contact:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="contactTotal > 0" :total="contactTotal" v-model:page="contactQueryParams.pageNum" v-model:limit="contactQueryParams.pageSize" @pagination="getContactList" />
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位类型" prop="type">
              <el-input v-model="form.type" placeholder="请输入单位类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="form.tel" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="contactTitle" v-model="contactOpen" width="500px" append-to-body>
      <el-form ref="contactFormRef" :model="contactForm" :rules="contactRules" label-width="100px">
        <el-form-item label="联系人" prop="name">
          <el-input v-model="contactForm.name" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="contactForm.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="contactForm.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitContactForm">确 定</el-button>
          <el-button @click="contactCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PmsCompany">
import { listCompany, getCompany, addCompany, updateCompany, delCompany, listContact, getContact, addContact, updateContact, delContact } from '@/api/pms/company'

const { proxy } = getCurrentInstance()
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const open = ref(false)
const companyList = ref([])
const activeTab = ref('company')

const contactLoading = ref(false)
const contactIds = ref([])
const contactSingle = ref(true)
const contactMultiple = ref(true)
const contactTotal = ref(0)
const contactTitle = ref('')
const contactOpen = ref(false)
const contactList = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined
  },
  rules: {
    name: [{ required: true, message: '单位名称不能为空', trigger: 'blur' }]
  },
  contactForm: {},
  contactQueryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined
  },
  contactRules: {
    name: [{ required: true, message: '联系人不能为空', trigger: 'blur' }]
  }
})

const { form, queryParams, rules, contactForm, contactQueryParams, contactRules } = toRefs(data)

function getList() {
  loading.value = true
  listCompany(queryParams.value).then(response => {
    companyList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

function getContactList() {
  contactLoading.value = true
  listContact(contactQueryParams.value).then(response => {
    contactList.value = response.rows
    contactTotal.value = response.total
    contactLoading.value = false
  })
}

function handleTabChange(tab) {
  if (tab === 'company') {
    getList()
  } else if (tab === 'contact') {
    getContactList()
  }
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleContactQuery() {
  contactQueryParams.value.pageNum = 1
  getContactList()
}

function resetContactQuery() {
  proxy.resetForm('contactQueryRef')
  handleContactQuery()
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

function handleContactSelectionChange(selection) {
  contactIds.value = selection.map(item => item.id)
  contactSingle.value = selection.length !== 1
  contactMultiple.value = !selection.length
}

function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    type: undefined,
    contact: undefined,
    tel: undefined
  }
  proxy.resetForm('formRef')
}

function cancel() {
  open.value = false
  reset()
}

function handleAdd() {
  reset()
  open.value = true
  title.value = '新增单位'
}

function handleUpdate(row) {
  reset()
  const id = row.id || ids.value[0]
  getCompany(id).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改单位'
  })
}

function submitForm() {
  proxy.$refs['formRef'].validate(valid => {
    if (valid) {
      if (form.value.id) {
        updateCompany(form.value).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addCompany(form.value).then(() => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleDelete(row) {
  const idsVal = row.id || ids.value
  proxy.$modal.confirm('是否确认删除单位编号为"' + idsVal + '"的数据项？').then(() => {
    return delCompany(idsVal)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}

function resetContact() {
  contactForm.value = {
    id: undefined,
    name: undefined,
    phone: undefined,
    email: undefined
  }
  proxy.resetForm('contactFormRef')
}

function contactCancel() {
  contactOpen.value = false
  resetContact()
}

function handleContactAdd() {
  resetContact()
  contactOpen.value = true
  contactTitle.value = '新增联系人'
}

function handleContactUpdate(row) {
  resetContact()
  const id = row.id || contactIds.value[0]
  getContact(id).then(response => {
    contactForm.value = response.data
    contactOpen.value = true
    contactTitle.value = '修改联系人'
  })
}

function submitContactForm() {
  proxy.$refs['contactFormRef'].validate(valid => {
    if (valid) {
      if (contactForm.value.id) {
        updateContact(contactForm.value).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          contactOpen.value = false
          getContactList()
        })
      } else {
        addContact(contactForm.value).then(() => {
          proxy.$modal.msgSuccess('新增成功')
          contactOpen.value = false
          getContactList()
        })
      }
    }
  })
}

function handleContactDelete(row) {
  const idsVal = row.id || contactIds.value
  proxy.$modal.confirm('是否确认删除联系人编号为"' + idsVal + '"的数据项？').then(() => {
    return delContact(idsVal)
  }).then(() => {
    getContactList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}

onMounted(() => {
  getList()
})
</script>
