<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="申请单位" prop="unitName">
        <el-input v-model="queryParams.unitName" placeholder="请输入申请单位" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="审批状态" prop="applyState">
        <el-select v-model="queryParams.applyState" placeholder="全部" clearable style="width: 130px">
          <el-option label="待审批" value="0" />
          <el-option label="已通过" value="1" />
          <el-option label="已驳回" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </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="['gx:apply:add']">新增申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['gx:apply:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请单位" prop="unitName" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="业务域" prop="bizName" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="申请时间" prop="applyTime" width="170" align="center" />
      <el-table-column label="到期时间" prop="applyExpire" width="170" align="center" />
      <el-table-column label="审批状态" prop="applyState" width="100" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.applyState === '0' ? 'warning' : scope.row.applyState === '1' ? 'success' : 'danger'" size="small">
            {{ scope.row.applyState === '0' ? '待审批' : scope.row.applyState === '1' ? '已通过' : '已驳回' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审批人" prop="approveUser" width="120" />
      <el-table-column label="审批时间" prop="approveTime" width="170" align="center" />
      <el-table-column label="操作" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.applyState === '0'" link type="primary" icon="Check" @click="handleAudit(scope.row, '1')" v-hasPermi="['gx:apply:edit']">通过</el-button>
          <el-button v-if="scope.row.applyState === '0'" link type="danger" icon="Close" @click="handleAudit(scope.row, '2')" v-hasPermi="['gx:apply:edit']">驳回</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gx:apply: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-dialog title="新增数据共享申请" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="申请单位" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入申请单位" />
        </el-form-item>
        <el-form-item label="业务域" prop="bizCode">
          <el-select v-model="form.bizCode" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in bizOptions" :key="item.bizCode" :label="item.bizName" :value="item.bizCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="到期时间" prop="applyExpire">
          <el-date-picker v-model="form.applyExpire" type="datetime" placeholder="选择到期时间" style="width: 100%" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </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>
  </div>
</template>

<script setup name="GxApply">
import { listApply, addApply, auditApply, delApply } from "@/api/gx/apply";
import { listBiz } from "@/api/gx/biz";

const { proxy } = getCurrentInstance();

const applyList = ref([]);
const bizOptions = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const open = ref(false);
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    unitName: undefined,
    applyState: undefined,
  },
  rules: {
    unitName: [{ required: true, message: "申请单位不能为空", trigger: "blur" }],
    bizCode: [{ required: true, message: "业务域不能为空", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

function getList() {
  loading.value = true;
  const params = { ...queryParams.value };
  if (dateRange.value && dateRange.value.length === 2) {
    params.params = { beginTime: dateRange.value[0], endTime: dateRange.value[1] };
  }
  listApply(params).then((response) => {
    applyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getBizOptions() {
  listBiz({ pageNum: 1, pageSize: 999 }).then((response) => {
    bizOptions.value = response.rows;
  });
}

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

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

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

function reset() {
  form.value = { unitName: undefined, bizCode: undefined, applyExpire: undefined, remark: undefined };
  proxy.resetForm("formRef");
}

function handleAdd() {
  reset();
  open.value = true;
}

function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      addApply(form.value).then(() => {
        proxy.$modal.msgSuccess("申请提交成功");
        open.value = false;
        getList();
      });
    }
  });
}

function handleAudit(row, state) {
  const action = state === "1" ? "通过" : "驳回";
  proxy.$modal.confirm(`是否确认${action}该申请？`).then(() => {
    return auditApply({ id: row.id, applyState: state, approveRemark: "" });
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess(`${action}成功`);
  }).catch(() => {});
}

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

getBizOptions();
getList();
</script>
