<template>
  <lay-container fluid="true" class="organization-box">
    <div style="display: flex">
      <div :style="{ width: isFold ? `0px` : `300px` }" class="left-tree">
        <!-- tree -->
        <div v-show="!isFold">
          科室列表
        </div>
        <lay-tree v-show="!isFold" style="margin-top: 10px" :data="data" v-model:selectedKey="leftId" :showLine="false"
          @node-click="handleClick">
          <template #title="{ data }">
            <span :class="leftId == data.id ? 'isChecked' : ''">
              {{ data.name }}
            </span>
          </template>
        </lay-tree>
        <div class="isFold" @click="isFold = !isFold">
          &nbsp;<lay-icon v-if="!isFold" class="layui-icon-left"></lay-icon>
          <lay-icon v-else class="layui-icon-right"></lay-icon>
        </div>
      </div>
      <div style="flex: 1; padding: 10px; over-flow: auto">
        <!-- table -->
        <lay-card>
          <lay-form style="margin-top: 20px">
                                <lay-row style="min-width: 1200px">
                                    <lay-col :md="5">
                                        <lay-form-item label="发生时间：" :label-width="100">
                                            <lay-date-picker style="width: 100%" v-model="searchForm.fsdate"
                                                placeholder="发生时间" allow-clear></lay-date-picker>
                                        </lay-form-item>
                                    </lay-col>
                                    <lay-col :md="6" >
                                        <lay-form-item label="考评对象：" :label-width="100">
                                            <lay-select v-model="searchForm.user_id" placeholder="请选择考评对象"
                                                :show-search="true">
                                                <lay-select-option v-for="user in userList" :key="user.id"
                                                    :value="user.id" :label="user.nickname">
                                                </lay-select-option>
                                            </lay-select>
                                        </lay-form-item>
                                    </lay-col>
                                    <lay-col :md="4">
                                        <lay-form-item label="审核状态：" :label-width="100">
                                            <lay-select style="width: 100%" v-model="searchForm.status"
                                                placeholder="审核状态">
                                                <lay-select-option :value="1" label="待审核"></lay-select-option>
                                                <lay-select-option :value="2" label="已通过"></lay-select-option>
                                                <lay-select-option :value="3" label="已驳回"></lay-select-option>
                                            </lay-select>
                                        </lay-form-item>
                                    </lay-col>
                                    <lay-col :md="4">
                                        <lay-form-item :label-width="0">
                                            <lay-button style="margin-left: 20px" type="primary"
                                                @click="toSearch">查询</lay-button>
                                            <lay-button @click="toReset">重置</lay-button>
                                        </lay-form-item>
                                    </lay-col>
                                </lay-row>
                            </lay-form>
        </lay-card>
        <lay-table :page="page" @change="handlePageChange" height="600px" :columns="columns" :loading="loading" :data-source="dataSource">
          <template v-slot:status="{ data }">
            <span v-if="data.status == 1" style="color: #1E9FFF">待审核</span>
            <span v-if="data.status == 2" style="color: #009688">审核通过</span>
            <span v-if="data.status == 3" style="color: #009688">已驳回</span>
          </template>
          <template v-slot:assessment_type="{ data }">
            <lay-tag :type="data.assessment_type == 1 ? 'primary' : 'danger'">
              {{ data.assessment_type == 1 ? '加分' : '减分' }}
            </lay-tag>
          </template>
          <template v-slot:operator="{ data }">
            <lay-space size="lg">
              <span style="color: #00A394;cursor: pointer" @click="showDetail(data)">详情</span>
              <lay-popconfirm v-if="data.status == 1" trigger="click" content="请确认你的操作，流程将结束" cancelText="审核拒绝" confirmText="审核通过" @confirm="confirm(data, 2)"
              @cancel="confirm(data, 3)">
                <span style="color: #00A394;cursor: pointer">审核</span>
              </lay-popconfirm>
            </lay-space>
          </template>
        </lay-table>
      </div>
    </div>
    <lay-layer v-model="detailVisible" :type="4" title="详情查看" :area="['950px', '100%']" :shade="true">
      <div style="padding: 20px">
        <lay-descriptions title="考评信息" :column="2" border>
          <lay-descriptions-item label="考评人">
            {{ detailForm.nickname }}
          </lay-descriptions-item>
          <lay-descriptions-item label="科室">
            {{ detailForm.group_name }}
          </lay-descriptions-item>
          <lay-descriptions-item label="发生日期">
            {{ detailForm.fsdate }}
          </lay-descriptions-item>
          <lay-descriptions-item label="证明人">
            {{ detailForm.zm_nickname }}
          </lay-descriptions-item>
          <lay-descriptions-item label="考评类型">
            {{ detailForm.assessment_type }}
          </lay-descriptions-item>
          <lay-descriptions-item label="考评项目">
            {{ detailForm.project_name }}
          </lay-descriptions-item>
          <lay-descriptions-item label="分值">
            {{ detailForm.score_value }}
          </lay-descriptions-item>
          <lay-descriptions-item label="状态">
            {{ detailForm.status == 1 ? '待审核' : detailForm.status == 2 ? '审核通过' : '已驳回' }}
          </lay-descriptions-item>
          <lay-descriptions-item label="备注" :span="2">
            {{ detailForm.notes || '暂无备注' }}
          </lay-descriptions-item>
        </lay-descriptions>
        <lay-descriptions v-if="detailForm.fj_url" title="附件信息" :column="1" border style="margin-top: 20px">
          <lay-descriptions-item label="附件">
            <a style="color: #009688" :href="detailForm.fj_url" target="_blank">附件下载</a>
          </lay-descriptions-item>
        </lay-descriptions>
        <div style="text-align: center; margin-top: 20px;">
          <lay-button @click="detailVisible = false">关闭</lay-button>
        </div>
      </div>
    </lay-layer>
  </lay-container>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { layer } from '@layui/layui-vue'
import { getAdditionIndexManage, getAdditionIndexManageExamine, getDoctorName, getUserGroupNew } from '@/api/module/home';
import { useUserStore } from '@/store/user';
const userStore = useUserStore()
onMounted(() => {
  getLeftList();
  getUserAllList();
})
const userList = ref([]);
const getUserAllList = () => {
    getDoctorName({}).then((res) => {
        if (res.code === 1) {
            userList.value = res.data;
        }
    })
}
const leftId = ref(0);
const data = ref()
const getLeftList = () => {
  getUserGroupNew({}).then((res) => {
    console.log(res)
    if (res.code == 1) {
      data.value = res.data;
      leftId.value = res.data[0].id;
      getUserList();

    } else {
      layer.msg(res.msg, { icon: 2 })
    }
  })
}
const getUserList = () => {
  getAdditionIndexManage({ page: page.current, size: page.limit, group_id: leftId.value, ...searchForm.value }).then((res) => {
    if (res.code == 1) {
      dataSource.value = res.data.data;
      page.total = res.data.count;
    } else {
      layer.msg(res.msg, { icon: 2 })
    }
  })
}
// 搜索方法
const toSearch = () => {
    page.current = 1
    dataSource.value = []
    getUserList()
}
// 添加分页变化处理方法
const handlePageChange = (d) => {
    console.log(d);
    getUserList()
}
// 重置方法
const toReset = () => {
    searchForm.value = {
        fsdate: '',
        user_id: '',
        status: ''
    }
    page.current = 1
    dataSource.value = []
    getUserList()
}
const loading = ref(false)
const selectedKeys = ref()
const page = reactive({ current: 1, limit: 10, total: 1 })
const columns = ref([
  {
    title: "序号",
    width: "80px",
    key: "id",
    align: 'center'
  },
  {
    title: "发生时间",
    width: "160px",
    key: "fsdate",
    align: 'center'
  },
  {
    title: "考评对象",
    width: "120px",
    key: "nickname",
    align: 'center'
  },
  {
    title: "考评类型",
    width: "100px",
    key: "assessment_type",
    customSlot: 'assessment_type',
    align: 'center'
  },
  {
    title: "考评项",
    width: "150px",
    key: "project_name",
    align: 'center'
  },
  {
    title: "分值",
    width: "80px",
    key: "score_value",
    align: 'center'
  },
  {
    title: "记录状态",
    width: "100px",
    customSlot: 'status',
    key: "status",
    align: 'center'
  },
  {
    title: '操作',
    width: '180px',
    customSlot: 'operator',
    key: 'operator',
    align: 'center',
    fixed: 'right'
  }
])
const dataSource = ref()
const detailVisible = ref(false)
const detailForm = ref({
  nickname: '',
  group_name: '',
  fsdate: '',
  zm_nickname: '',
  assessment_type: '',
  project_name: '',
  score_value: '',
  notes: '',
  status: 1,
  fj_url: ''
})
// 搜索表单
const searchForm = ref({
    fsdate: '',
    user_id: '',
    status: ''
})

// 显示详情方法
const showDetail = (row: any) => {
  detailForm.value = row
  detailVisible.value = true
}
const confirm = (data: any, status: number) => {
  console.log(data, status)
  getAdditionIndexManageExamine({ id: data.id, status: status }).then((res) => {
    if (res.code == 1) {
      layer.msg(res.msg, { icon: 1 })
    } else {
      layer.msg(res.msg, { icon: 2 })
    }
    getUserList();
  })
}
// 修改点击科室树节点的处理方法
const handleClick = (node: any) => {
  leftId.value = node.id
  page.current = 1
  dataSource.value = []
  getUserList();
}
</script>

<style scoped>
.organization-box {
  width: calc(100vw - 240px);
  height: calc(100vh - 110px);
  margin-top: 10px;
  box-sizing: border-box;
  background-color: #fff;
  overflow: hidden;
}

.left-tree {
  display: inline-block;
  padding: 20px 15px 0 5px;
  height: 1200px;
  border-right: 1px solid #e6e6e6;
  box-sizing: border-box;
  position: relative;
}

/* todo layui-tree-entry 设置无效 */
.layui-tree-entry {
  position: relative;
  padding: 10px 0;
  height: 20px;
  line-height: 20px;
  white-space: nowrap;
}

.isFold {
  position: absolute;
  top: 36%;
  right: -10px;
  width: 26px;
  height: 26px;
  line-height: 26px;
  border-radius: 13px;
  background-color: #fff;
  border: 1px solid #e6e6e6;
  cursor: pointer;
}

.search-input {
  display: inline-block;
  width: 98%;
  margin-right: 10px;
}

.isChecked {
  display: inline-block;
  background-color: #e8f1ff;
  color: red;
}
</style>
