<template>
  <lay-container fluid="true" class="organization-box">
    <div style="display: flex">
      <div :style="{ width: isFold ? `0px` : `230px` }" class="left-tree">
        <lay-tree v-show="!isFold" style="margin-top: 10px" :data="data" v-model:selectedKey="group_id"
                  :showLine="false" @node-click="handleClick">
          <template #title="{ data }">
            <span :class="group_id == 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>
            <lay-row>
              <lay-col :md="5">
                <lay-form-item label="年" label-width="80px">
                  <lay-date-picker type="year" v-model="year" placeholder="年"
                                   allow-clear></lay-date-picker>
                </lay-form-item>
              </lay-col>
              <lay-col :md="5">
                <lay-form-item label="季度" label-width="80px">
                  <lay-select v-model="quarter" 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-option :value="4" label="第四季度"></lay-select-option>
                  </lay-select>
                </lay-form-item>
              </lay-col>
              <lay-col :md="5">
                <lay-form-item label-width="20">
                  <lay-button style="margin-left: 20px" type="normal" size="sm" @click="toSearch">
                    查询
                  </lay-button>
                  <lay-button size="sm" @click="toReset"> 重置</lay-button>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-card>
        <lay-tab v-model="tab_id" type="brief"  @change="openTab">
          <lay-tab-item v-for="item in tabList" :title="item.title" :id="item.id">
            <lay-table :page="page" height="600px" :columns="columns" :loading="loading"
                       :data-source="dataSource" v-model:selected-keys="selectedKeys" @change="change">
              <template v-slot:scoringrecord_status="{row}">
                <lay-tag v-if="row.scoringrecord_status==1" type="primary">已自评</lay-tag>
                <lay-tag v-if="row.scoringrecord_status==2">未自评</lay-tag>
              </template>
              <template v-slot:nickname="{ row }">
                <div style="color: #00A394;cursor: pointer" @click="openUserListShow(row)">{{ row.nickname }}</div>
              </template>
              <template v-slot:operator="{ row }">
                <span style="color: #00A394;cursor: pointer" v-if="row.department_score_scoringrecord==0 && row.scoringrecord_status==1"
                      @click="getInfo(row,1)">科室评分</span>
                <span style="color: #00A394;cursor: pointer" v-if="row.department_score_scoringrecord!=0 && row.scoringrecord_status==1"
                      @click="getInfo(row,2)">考评详情</span>
                <span style="color: #999999;cursor: not-allowed" v-if="row.department_score_scoringrecord==0 && row.scoringrecord_status==2">未自评</span>
              </template>
            </lay-table>
          </lay-tab-item>
        </lay-tab>

      </div>
    </div>
    <lay-layer v-model="infoShow" title="自评详情" :type="4" :shade="true" :area="['950px','100%']"
               :btn="addIsInfo==1?addButton:''">
      <lay-container fluid="true" style="padding: 20px">
        <lay-table :default-expand-all="true" ref="tableRef6" children-column-name="children" :columns="columns6"
                   :data-source="dataSource6">
          <template v-slot:input="{ data,rowIndex}">
            <lay-input-number v-if="data.pid==0" :disabled="true" :min="0" :max="data.base_score"
                              v-model="data.base_score" position="right"></lay-input-number>
            <lay-input-number v-if="data.pid!=0" :disabled="addIsInfo==2" :min="0" :max="data.base_score"
                              v-model="data.content_score"
                              position="right"></lay-input-number>
          </template>
          <template v-slot:ksinput="{ data,rowIndex}">
            <lay-input-number v-if="data.pid==0" :disabled="true" :min="0" :max="data.base_score"
                              v-model="data.base_score" position="right"></lay-input-number>
            <lay-input-number v-if="data.pid!=0" :disabled="addIsInfo==2" :min="0" :max="data.base_score"
                              v-model="data.department_score"
                              position="right"></lay-input-number>
          </template>
        </lay-table>
      </lay-container>
    </lay-layer>
    <lay-layer v-model="userShow" title="季度自评列表" :shade="true" :area="['90%', '90%']">
      <lay-container fluid="true" style="padding: 20px">
        <lay-row :space="10">
          <lay-col :md="24">
            <lay-card>
              <lay-form style="margin-top: 20px">
                <lay-row>
                  <lay-col :md="5">
                    <lay-form-item label="年度：" label-width="50">
                      <lay-date-picker type="year" v-model="searchAccountUser" placeholder="请选择"
                        allow-clear></lay-date-picker>
                    </lay-form-item>
                  </lay-col>
                  <lay-col :md="4">
                    <lay-form-item label-width="0">
                      <lay-button type="primary" @click="toUserSearch">查询</lay-button>
                      <lay-button @click="toReset">重置</lay-button>
                    </lay-form-item>
                  </lay-col>
                </lay-row>
              </lay-form>
            </lay-card>
          </lay-col>
          <lay-col :md="24">
            <lay-tab v-model="userTabId" type="brief" @change="openUserTab">
              <lay-tab-item v-for="item in tabList" :title="item.title" :id="item.id">
                <lay-table height="400px" :default-expand-all="true" :columns="userColumns"
                  :data-source="userDataSource">
                  <template v-slot:operator="{ row }">
                    <span  v-if="row.if ==2" style="color: #00A394;cursor: pointer"
                      @click="getMonthlyInfo(row)">考评详情</span>
                      <span style="color: #999999;cursor: not-allowed"
                      v-if="row.if ==1">未自评</span>
                  </template>
                </lay-table>
              </lay-tab-item>
            </lay-tab>

          </lay-col>
        </lay-row>
      </lay-container>
    </lay-layer>
  </lay-container>
</template>
<script setup lang="ts">
import {ref, reactive, onMounted} from 'vue'
import {layer} from '@layui/layui-vue'
import {
  getUserGroupNew,
  getGroupQuarter,
  getQuarterUserList,
  getQuarterListFind,
  getQuarterListUpdate,
  getQuarterlyFindData
} from "@/api/module/home";
import dayjs from "dayjs";

onMounted(() => {
  getLeftList();
})
const userShow = ref(false);
const userColumns = ref([
  { title: '季度', width: '80px', key: 'quarter', align: 'center' },
  { title: '考评对象', width: '100px', key: 'user', align: 'center' },
  { title: '参评时间', width: '100px', key: 'createtime', align: 'center' },
  { title: '自评总分', width: '100px', key: 'self_score', align: 'center' },
  { title: '科室评分', width: '100px', key: 'department_score', align: 'center' },
  { title: '医院评分', width: '100px', key: 'hospital_score', align: 'center' },
  { title: '操作', width: '100px', key: 'operator', customSlot: 'operator', align: 'center' },
])
const userDataSource = ref([]);
const userTabId = ref(0);
const userInfo = ref({});
const searchAccountUser = ref(dayjs().year());
const openUserListShow = (row) => {
  userInfo.value = row;
  userShow.value = true;
  getUserListInfo();
}
const getUserListInfo = () => {
  getQuarterlyFindData({
    'user_id': userInfo.value.id,
    'evaluation_id': userTabId.value,
    'time': searchAccountUser.value
  }).then((res) => {
    console.log(res)
    if(res.code==1){
      userDataSource.value = res.data;
    }else{
      userDataSource.value = [];
    }
  })
}
const openUserTab = (d) => {
  console.log(d)
  userTabId.value = d;
  userDataSource.value = [];
  getUserListInfo();
}
const getMonthlyInfo = (item) => {
  addIsInfo.value=2;
  infoShow.value = true;
  console.log(item);
  item.time=item.quarter;
  getInfoMode(item);
}
const toUserSearch = () => {
  getUserListInfo();
}
const openTab = (d) => {
  console.log(d)
  tab_id.value = d;
  dataSource.value = [];
  page.current = 1;
  getList();
}
const page = reactive({current: 1, limit: 10, total: 1})
const change = (d) => {
  console.log(d);
  page.current = d.current;
  page.limit = d.limit;
  getList();
}
const addIsInfo = ref(1);
const infoShow = ref(false);
const year = ref(dayjs().year());
const quarter = ref(1);


const yearmonth = ref();
const getList = () => {
  getQuarterUserList({
    'group_id': group_id.value,
    'time': yearmonth.value,
    'evaluation_schedule_id': tab_id.value,
    page: page.current,
    size: page.limit
  }).then((res) => {
    console.log(res)
    if (res.code == 1) {
      dataSource.value = res.data.result;
      page.total = res.data.count;
    } else {
      layer.msg(res.msg, {icon: 2})
    }
  })
}
const thisInfo = ref();
const getInfo = (row,type) => {
  infoShow.value = true;
  addIsInfo.value = type;
  console.log(row);
  thisInfo.value = row;
  getInfoMode(row);
}
const getInfoMode = (row) => {
  getQuarterListFind({
    evaluation_schedule_id: tab_id.value,
    month: row.time,
    user_id: row.id
  }).then((res) => {
    console.log(res)
    if (res.code == 1) {
      dataSource6.value = res.data;
    } else {
      layer.msg(res.msg, {icon: 2})
    }
  })
}
const tabList = ref([]);
const tab_id = ref(0);
const getTabList = () => {
  getGroupQuarter({'group_id': group_id.value}).then((res) => {
    console.log(res)
    if (res.code == 1) {
      tabList.value = res.data;
      tab_id.value = res.data[0].id;
      userTabId.value = res.data[0].id;
      getList();
    } else {
      layer.msg(res.msg, {icon: 2})
    }
  })
}
const group_id = ref(1);
const getLeftList = () => {
  getUserGroupNew({}).then((res) => {
    console.log(res)
    if (res.code == 1) {
      data.value = res.data;
      group_id.value = res.data[0].id;
      getTabList();
    } else {
      layer.msg(res.msg, {icon: 2})
    }
  })
}
const data = ref([])
const isFold = ref(false)
const searchQuery = ref({
  year: '',
  evaluateTarget: '',
  status: ''
})

function toReset() {
  searchQuery.value = {
    year: '',
    evaluateTarget: '',
    status: ''
  }
}

function handleClick(node: any) {
  // selectedNode.value = JSON.parse(JSON.stringify(node))
  page.current = 1;
  getList();
  // change(page)
}

function toSearch() {
  page.current = 1;
  yearmonth.value=year.value+'-'+quarter.value;
  getList();
}

const loading = ref(false)
const selectedKeys = ref()
const columns = ref([
  {title: '选项', width: '55px', type: 'checkbox', align: 'center'},
  {title: '序号', width: '60px', key: 'id', align: 'center'},
  {title: '季度', width: '80px', key: 'time', align: 'center'},
  {title: '考评对象', width: '100px', key: 'nickname',customSlot:'nickname', align: 'center'},
  {title: '工号', width: '100px', key: 'work_number', align: 'center'},
  {title: '部门', width: '120px', key: 'group_name', align: 'center'},
  {title: '医院评分', width: '100px', key: 'hospital_score', align: 'center'},
  {title: '科室评分', width: '100px', key: 'department_score_scoringrecord', align: 'center'},
  {title: '自评总分', width: '100px', key: 'user_scoringrecord', align: 'center'},
  {title: '自评状态', width: '100px', key: 'scoringrecord_status',   customSlot: 'scoringrecord_status',align: 'center'},
  {
    title: '操作',
    width: '180px',
    customSlot: 'operator',
    key: 'operator',
    fixed: 'right', align: 'center'
  }
])
const dataSource = ref()
const addButton = ref([
  {
    text: "确认",
    callback: async () => {
      var json = JSON.stringify(dataSource6.value);
      var res = await getQuarterListUpdate({
        'term': thisInfo.value.time,
        'json': json,
        'user_id': thisInfo.value.id,
        evaluation_schedule_id: tab_id.value
      });
      console.log(res)
      if (res.code == 1) {
        layer.msg('提交成功！', {icon: 1})
        thisInfo.value = {};
        infoShow.value = false;
        getList();
      } else {
        layer.msg(res.msg, {icon: 2})
      }
    }
  },
  {
    text: "取消",
    callback: () => {
      infoShow.value = false;
    }
  }
])
const columns6 = [
  {
    title: "考评项目",
    width: "300px",
    key: "project_name"
  },
  {
    title: "分值",
    width: "100px",
    align: 'center',
    key: "base_score"
  },
  {
    title: "自评分",
    width: "100px",
    align: 'center',
    customSlot: 'input',
    key: "input"
  },
  {
    title: "科室评分",
    width: "100px",
    align: 'center',
    customSlot: 'ksinput',
    key: "ksinput"
  }
]
const dataSource6 = ref([])
</script>

<style>
.layui-table-header .layui-table-cell {
  background-color: #ECF8FA !important;
}

.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>
