<template>
  <div class="app-container case-templates-statistics-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="名称" prop="searchKey">
        <el-input
          v-model="queryParams.searchKey"
          placeholder="请输入名称搜索"
          clearable
          style="width: 200px"
          @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">新增</el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statisticsList">
      <el-table-column label="属性名称" align="center" prop="name" />
      <el-table-column label="属性代码" align="center" prop="code" />
      <el-table-column label="属性类型" align="center" prop="typeEnum">
        <template #default="scope">
          {{ typeEnums(scope.row.typeEnum) }}
        </template>
      </el-table-column>
      <el-table-column label="统计值单位" align="center" prop="unit" />
      <el-table-column label="是否必填" align="center" prop="required">
        <template #default="scope">
          {{ scope.row.required === 1 ? '必填' : '不必填' }}
        </template>
      </el-table-column>
      <el-table-column label="是否统计" align="center" prop="isStatistics">
        <template #default="scope">
          {{ scope.row.isStatistics === 1 ? '统计' : '不统计' }}
        </template>
      </el-table-column>
      <el-table-column label="字典名称" align="center" prop="dictName"> </el-table-column>
      <el-table-column label="字典类型" align="center" prop="dictType"> </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"> </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="statisticsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="属性名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入属性名称" />
        </el-form-item>
        <el-form-item label="属性代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入属性代码" />
        </el-form-item>
         <el-form-item label="体温单类型编码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入体温单属性编码" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="displayOrder">
          <el-input-number v-model="form.displayOrder" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="属性类型" prop="typeEnum">
          <el-radio-group v-model="form.typeEnum">
            <el-radio v-for="dict in typeEnumsOptions" :key="dict.value" :label="dict.value">{{
              dict.info
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否必填" prop="required">
          <el-switch
            v-model="form.required"
            :active-value="1"
            :inactive-value="0"
            active-text="是"
            inactive-text="否"
            inline-prompt
          />
        </el-form-item>
        <el-form-item label="是否统计" prop="isStatistics">
          <el-switch
            v-model="form.isStatistics"
            :active-value="1"
            :inactive-value="0"
            active-text="是"
            inactive-text="否"
            inline-prompt
          />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入统计值单位" />
        </el-form-item>
        <el-form-item label="字典名称" prop="dictType">
          <el-select v-model="form.dictType" style="width: 100%" filterable>
            <el-option
              v-for="item in typeOptions"
              :key="item.dictId"
              :label="item.dictName"
              :value="item.dictType"
            >
              <span style="float: left">{{ item.dictName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.dictType }}</span>
            </el-option>
          </el-select>
        </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="Post">
import {add, deleteStatistics, getPageList, init, update} from './api';
import {optionselect as getDictOptionselect} from '@/api/system/dict/type';

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict('sys_normal_disable');

const statisticsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);

const total = ref(0);
const title = ref('');

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined,
  },
  rules: {
    name: [{ required: true, message: '请输入属性名称', trigger: 'blur' }],
    code: [{ required: true, message: '请输入属性代码', trigger: 'blur' }],
  },
});

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

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  getPageList(queryParams.value).then((response) => {
    statisticsList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    id: undefined, // ID
    name: '', // 名称
    typeEnum: 1, // 类型枚举
    code: '', // 编码
    required: 0, // 是否必填(0-否 1-是)
    remark: '', // 备注
    unit: '', // 单位
    isStatistics: 1, // 是否统计(0-否 1-是)
    displayOrder: 0, // 显示顺序
    dictName: '', //字典名称
    dictType: '', //字典类型
    typeCode:'',//体温单类型编码
  };
  proxy.resetForm('statisticsRef');
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef');
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = '添加统计';
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = '修改统计';
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs['statisticsRef'].validate((valid) => {
    if (form.value.dictType ) {
      form.value.dictName=typeOptions.value.find((item) => item.dictType == form.value.dictType)?.dictName || form.value.dictName;
    }
    if (valid) {
      if (form.value.id != undefined) {
        update(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功');
          open.value = false;
          getList();
        });
      } else {
        add(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功');
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除编号为"' + row.id + '"的数据项？')
    .then(function () {
      return deleteStatistics(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess('删除成功');
    })
    .catch(() => {});
}
const typeEnumsOptions = ref([]);
const typeEnums = (value) => {
  if (typeEnumsOptions.value.length) {
    return typeEnumsOptions.value.find((item) => item.value == value)?.info || '未知';
  }
};
const initPage = async () => {
  try {
    const response = await init();
    typeEnumsOptions.value = response.data.doc_statistics_definition_type_enums || {};
  } catch (error) {}
};
const typeOptions = ref([]);
/** 查询字典类型列表 */
function getTypeList() {
  getDictOptionselect().then((response) => {
    typeOptions.value = response.data;
  });
}
initPage();
getTypeList();
getList();
</script>
<style scoped lang="scss">
.case-templates-statistics-container {
  // height: calc(100vh - 84px);
  .toolbar {
    height: 48px;
    display: flex;
    padding: 0 8px;
    align-items: center;
  }
  .statistics-data-container {
    height: calc(100% - 48px);
  }
}
</style>
