<template>
  <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="searchAccount" 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="toSearch">查询</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-card>
          <div style="padding: 10px">
            <span style="font-size: 18px;vertical-align: center;margin-right: 20px">季度自评列表</span>
            <lay-button type="primary" size="sm">导出Excel</lay-button>
          </div>
          <lay-tab v-model="current1" @change="openTab">
            <lay-tab-item v-for="item in tabList" :title="item.title" :id="item.id"></lay-tab-item>
          </lay-tab>
          <lay-table :columns="columns" :dataSource="dataSource" :even="true" height="500px" size="lg">
            <template v-slot:id="{ data }">
              {{ data.id }}
            </template>
            <template v-slot:department_score="{ data }">
              {{ data.department_score == 0 ? '-' : data.department_score }}
            </template>
            <template v-slot:username="{ data }">
              {{ data.username }}
            </template>
            <template v-slot:password="{ data }">
              {{ data.password }}
            </template>
            <template v-slot:operator="{ data }">
              <span style="color: #00A394;cursor: pointer" v-if="data.if == 2" @click="getMonthlyInfo(data)">详情</span>
              <span style="color: #00A394;cursor: pointer" v-if="data.if == 1" @click="insZp(data)">开始自评</span>
            </template>
          </lay-table>
        </lay-card>
      </lay-col>
    </lay-row>
  </lay-container>
  <lay-layer v-model="visible11" 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:base_score="{ data }">
          <div v-if="data.pid == 0">题目总分：{{ data.base_score }}</div>
          <div v-if="data.pid != 0">{{ data.base_score }}</div>
        </template>
        <template v-slot:content_score="{ row, column, rowIndex }">
          <div v-if="row.pid == 0">已评总分：{{ row.avg_score ? row.avg_score : 0 }}</div>
          <lay-input-number @change="inputChange(row, rowIndex, column)" v-if="row.pid != 0" :disabled="addIsInfo == 2"
            :min="0" :max="row.base_score" v-model="row.content_score" position="right"></lay-input-number>
        </template>
      </lay-table>
    </lay-container>
  </lay-layer>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { layer } from '@layui/layer-vue';
import {
  QuarterList, QuarterContent, QuarterCreate, QuarterInfo, userQuarterData
} from '../../api/module/home';
import { useUserStore } from "@/store/user";
import dayjs from "dayjs";
const current1 = ref(1);
const tabList = ref([]);
const userStore = useUserStore()
const active = ref(-1);
const visible11 = ref(false);

const page = ref({ total: 100, limit: 10, current: 2 });

const dataSource = ref([]);

const addIsInfo = ref(1);

const quarterName = ref('');

onMounted(() => {
  //getQuarterList();
  //getQuarterContent();
  getUserEvaluationData();
})
const inputChange = (row) => {
  //计算出dataSource6里分数在children下所有的已评总分 
  let total = 0;
  const index = dataSource6.value.findIndex(item => item.id === row.pid);
  dataSource6.value[index].children.forEach(item => {
    total += parseInt(item.content_score);
  });
  //找到row的index
  dataSource6.value[index].avg_score = total;
  dataSource6.value[index].content_score = total;
}
//自评列表总分
const inputListZpShow = () => {
  //计算出dataSource6里分数在children下所有的已评总分 

  dataSource6.value.forEach((item, index) => {
    let total = 0;
    item.children.forEach(item2 => {
      total += parseInt(item2.content_score);
    });
    dataSource6.value[index].avg_score = total;
    dataSource6.value[index].content_score = total;
  });
}
const openTab = (d) => {
  console.log(d)
  current1.value = d;
  dataSource.value = [];
  getQuarterList();
}
const getUserEvaluationData = () => {
  userQuarterData({}).then((res) => {
    console.log(res)
    if (res.code == 1) {
      tabList.value = res.data;
      current1.value = res.data[0].id;
      getQuarterList();
    } else {
      layer.msg(res.msg, { icon: 2 })
    }
  })
}
const insZp = (item) => {
  visible11.value = true;
  addIsInfo.value = 1;
  quarterName.value = item.quarter;
  getQuarterContent();
}
const getMonthlyInfo = (item) => {
  visible11.value = true;
  addIsInfo.value = 2;
  QuarterInfo({ 'user_id': userStore.userInfo.id, 'quarter': item.quarter, 'evaluation_schedule_id': current1.value }).then((res) => {
    console.log(res)
    if (res.code == 1) {
      dataSource6.value = res.data;
      inputListZpShow();
    } else {
      layer.msg(res.msg, { icon: 2 })
    }
  })
}
const getQuarterList = () => {
  QuarterList({ 'id': current1.value, time: searchAccount.value }).then((res) => {
    console.log(res)
    if (res.code == 1) {
      dataSource.value = res.data;
    } else {
      layer.msg(res.msg, { icon: 2 })
    }
  })
}
const dataSource6 = ref([]);
const getQuarterContent = () => {
  QuarterContent({ 'id': current1.value }).then((res) => {
    console.log(res)
    if (res.code == 1) {
      dataSource6.value = res.data;
    } else {
      layer.msg(res.msg, { icon: 2 })
    }
  })
}
const columns6 = [
  {
    title: "考评项目",
    width: "300px",
    key: "project_name"
  },
  {
    title: "分值",
    width: "100px",
    align: 'center',
    key: "base_score",
    customSlot: 'base_score',
    totalRow: true
  },
  {
    title: "自评分",
    width: "100px",
    align: 'center',
    customSlot: 'content_score',
    key: "content_score",
    totalRow: true
  }
]
const columns = [
  {
    title: '季度',
    width: '120px',
    align: 'center',
    key: 'quarter'
  },
  {
    title: '考评对象',
    width: '150px',
    align: 'center',
    key: 'user'
  },
  {
    title: '参评时间',
    width: '250px',
    align: 'center',
    key: 'createtime'
  },
  {
    title: '自评总分',
    key: 'self_score',
    align: 'center',
  }, {
    title: '科室评分',
    key: 'department_score',
    customSlot: 'department_score',
    align: 'center',
  },
  // {
  //   title: '医院评分',
  //   key: 'hospital_score',
  //   align: 'center',
  // },
  {
    title: '操作',
    width: '180px',
    customSlot: 'operator',
    key: 'operator',
    align: 'center',
    fixed: 'right'
  }
]

function toSearch() {
  console.log(searchAccount.value)
  //layer.load(2, {time: 3000})
  getQuarterList();
}

const searchAccount = ref(dayjs().year())

function toReset() {
  searchAccount.value = dayjs().year()
}
const addButton = ref([
  {
    text: "确认",
    callback: async () => {
      var json = JSON.stringify(dataSource6.value);
      var res = await QuarterCreate({ 'evaluation_schedule_id': current1.value, 'term': quarterName.value, 'json': json, 'user_id': userStore.userInfo.id });
      console.log(res)
      if (res.code == 1) {
        layer.msg('提交成功！', { icon: 1 })
        visible11.value = false;
        getQuarterList();
        //getQuarterContent();
      } else {
        layer.msg(res.msg, { icon: 2 })
      }
    }
  },
  {
    text: "取消",
    callback: () => {
      visible11.value = false;
    }
  }
])
</script>
<style>
.layui-table-header .layui-table-cell {
  background-color: #ECF8FA !important;
}
</style>
