<template>
  <div class="app-container">
    <!--        v-if="is_super"-->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      label-width="85px"
    >
      <el-form-item label="需求名称" prop="title">
        <el-input
          v-model="queryParams.title"
          clearable
          placeholder="请输入需求名称"
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求类别" prop="category">
        <el-select
          v-model="queryParams.category"
          clearable
          placeholder="用户状态"
          size="small"
          style="width: 240px"
        >
          <el-option :value="1" label="企业服务需求" />
          <el-option :value="2" label="专家服务需求" />
        </el-select>
      </el-form-item>
      <el-form-item label="时间查询" prop="time">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          size="small"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="YYYY-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="所属站点" prop="tenantId">
        <SiteOptions v-model="queryParams.tenantId" :site-list="[]" />
      </el-form-item>

      <el-form-item>
        <el-button
          icon="search"
          size="small"
          type="primary"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="refresh" size="small" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-radio-group
      v-model="queryParams.status"
      size="small"
      @change="handleQuery"
    >
      <el-radio-button label="1">待受理</el-radio-button>
      <el-radio-button label="3">已受理</el-radio-button>
      <el-radio-button label="2">已结束</el-radio-button>
    </el-radio-group>
    <el-table
      ref="tableRef"
      v-loading="loading"
      :data="dataList"
      style="margin-top: 12px"
    >
      <el-table-column align="center" label="数据编号" prop="id" />
      <el-table-column align="center" label="需求名称" prop="name" />
      <el-table-column align="center" label="需求类别" prop="research_name">
        <template #default="{ row }">
          <dict-tag :options="demandCategoryDict" :value="row.category" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="类型" prop="laboratory_name" />
      <el-table-column align="center" label="状态" prop="laboratory_name" />
      <el-table-column align="center" label="站点" prop="address" />
      <el-table-column align="center" label="申请时间" prop="createTime">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="{ row }">
          <el-button
            v-if="queryParams.status === '1'"
            size="small"
            type="text"
            @click="handleAdd"
          >
            受理
          </el-button>
          <el-button
            v-if="queryParams.status === '1'"
            size="small"
            type="text"
            @click=""
          >
            取消
          </el-button>
          <el-button size="small" type="text" @click="handleExamine(row.id)">
            查看
          </el-button>
          <el-button
            v-if="queryParams.status !== '1'"
            size="small"
            type="text"
            @click="handleDelete(row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
    <!-- 对话框 -->
    <el-dialog v-model="open" :title="title" width="500px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="受理单位:" prop="name">
          <el-input v-model="form.name" placeholder="请输入受理单位" />
        </el-form-item>
        <el-form-item label="联系人:" prop="key">
          <el-input v-model="form.key" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话:" prop="value">
          <el-input v-model="form.value" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import SiteOptions from "@/views/components/SiteOptions";
import {
  casDemandDelete,
  casDemandList,
} from "@/api/innovation-demand-service/demand";
import { reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import DictTag from "@/components/DictTag/index.vue";
import { ElMessage, ElMessageBox } from "element-plus";

const router = useRouter();
// 日期范围
const dateRange = ref([]);
// 遮罩层
const loading = ref(true);
// 总条数
const total = ref(0);
// 实验室表格数据
const dataList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

const queryFormRef = ref();
const formRef = ref();

const demandCategoryDict = [
  {
    label: "企业",
    value: "1",
    elTagType: "primary",
  },
  {
    label: "专家",
    value: "2",
    elTagType: "success",
  },
];
const data = reactive({
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: "1",
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {
    name: [{ required: true, message: "受理单位不能为空", trigger: "blur" }],
    key: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
    value: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
  },
});
const { form, rules, queryParams } = toRefs(data);

/** 查询机构列表 */
function getList() {
  loading.value = true;
  casDemandList(queryParams.value).then((resp) => {
    total.value = resp.total;
    dataList.value = resp.rows;
    loading.value = false;
  });
}

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

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

// handleClick() {
//   this.$nextTick(() => {
//     this.$refs["tableRef"].doLayout();
//   });
// },
function handleExamine(id) {
  this.$router.push({
    path: "/innovateDemand/demandDetail",
    query: { id },
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    name: undefined,
    key: undefined,
    value: undefined,
  };
  formRef.value?.resetFields();
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "受理";
}

/** 提交按钮 - 修改名称普通数据 */
function submitForm() {
  this.$refs["form"].validate((valid) => {
    if (valid) {
      // configAdd(this.form).then((response) => {
      //   this.$modal.msgSuccess("新增成功");
      //   open.value= false;
      //   getList();
      // });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(id) {
  ElMessageBox.confirm('是否确认删除产品编号为"' + id + '"的数据项？')
    .then(function () {
      return casDemandDelete(id);
    })
    .then(() => {
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
}

// is_super: this.$store.getters.is_super,

getList();
</script>
