<template>
  <!-- 随访查询 -->
  <div class="screeningSearch">
    <div class="top">
      <form-components
        ref="form"
        :forms="formList"
        :form-edit="formEdit"
        @handleSearch="onSearch"
      ></form-components>
    </div>
    <div class="bot">
      <el-table-self
        ref="table"
        :table-data="tableData"
        :columns="columns"
        :header-class="'newHeader'"
        :list-loading="listLoading"
        :current-page="pageIndex"
        :total-count="total"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        @pageSizeChange="handleSizeChange"
        @currentPageChange="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import FormComponents from "@/components/FormComponents"
import CustomsTable from "@/components/CustomsTable"
import paginationMixin from "@/components/TabComponents/mixin"
import searchMixin from "../searchMixin"
import { getCurrentFormByType } from "@/api/coop-group.js"
import { getFollowSearch } from "@/api/followup"
import { getDictDetail } from "@/api/dict.js"
export default {
  components: {
    FormComponents,
    // CustomsTable,
  },
  mixins: [paginationMixin, searchMixin],
  data() {
    return {
      followId: "",
      listLoading: false,
      keyword: "",
      modifiedFlag: false,
      columns: [
        {
          label: "医联体",
          minWidth: 120,
          value: "unionName",
          formatter: (row) => {
            return row.unionName ? row.unionName : "--"
          },
        },
        {
          label: "姓名",
          minWidth: 120,
          formatter: (row) => {
            return row.name ? row.name : "--"
          },
          value: "name",
        },
        {
          label: "性别",
          minWidth: 80,
          value: "sex",
          formatter: (row) => {
            return this.$handle.formatDicList(
              this.dictMap["d-sex"],
              row.sex + ""
            )
          },
        },
        {
          label: "身份证",
          minWidth: 120,
          value: "idCard",
          formatter: (row) => {
            return row.idCard ? row.idCard : "--"
          },
        },
        {
          label: "年龄",
          minWidth: 120,
          value: "age",
          formatter: (row) => {
            return row.age ? row.age : "--"
          },
        },
        {
          label: "筛查时间",
          minWidth: 180,
          value: "screeningTime",
          formatter: (row) => {
            return row.screeningTime ? row.screeningTime : "--"
          },
        },
        {
          label: "风险评估结果",
          minWidth: 120,
          value: "riskRank",
          formatter: (row) => {
            let riskRank = {
              low: "低危",
              medium: "中危",
              high: "高危",
            }
            return row.riskRank ? riskRank[row.riskRank] : "--"
          },
        },
        {
          label: "上次随访时间",
          value: "followTime",
          minWidth: 120,
          formatter: (row) => {
            return row.followTime ? row.followTime : "--"
          },
        },
        {
          label: "随访进度",
          value: "followBatch",
          minWidth: 120,
          formatter: (row) => {
            return this.$handle.formatDicList(
              this.dictMap["follow_type"],
              String(row.followBatch)
            )
          },
        },
        {
          label: "操作",
          width: 220,
          fixed: "right",
          operType: "button",
          operations: [
            {
              isIndex: true,
              func: this.rowOpration,
              formatter(row) {
                return {
                  label: "查询",
                  type: "text",
                }
              },
            },
            // {
            //   isIndex: true,
            //   func: this.handleEdit,
            //   formatter(row) {
            //     if (row.checkStatus != 3 && row.checkStatus != 4) {
            //       return {
            //         label: "修改",
            //         type: "text",
            //       }
            //     } else {
            //       return {
            //         label: "",
            //         type: "text",
            //         style: {
            //           display: "none",
            //         },
            //       }
            //     }
            //   },
            // },
          ],
        },
      ],
      tableData: [],
      formList: [
        {
          type: "daterange",
          label: "筛查时间",
          prop: "ScreeningTime",
          placeholder: "请选择时间",
          valueFormat: "yyyy-MM-dd",
        },
        {
          type: "select",
          label: "随访审核状态",
          prop: "checkStatus",
          trans: "checkStatus",
          placeholder: "请选择随访状态",
          rules: [],
          opts: [],
        },
        {
          type: "select",
          label: "随访进度",
          prop: "followBatch",
          trans: "follow_type",
          placeholder: "请选择随访进度",
          rules: [],
          opts: [],
        },
        {
          type: "select",
          label: "计划随访时间",
          prop: "nextFollowTime",
          trans: "next_follow_time",
          placeholder: "请选择计划随访时间",
          rules: [],
          opts: [],
        },
        {
          type: "input",
          label: "姓名",
          prop: "name",
          placeholder: "请输入姓名",
          rules: [],
        },
        {
          type: "input",
          label: "身份证",
          prop: "idCard",
          placeholder: "请输入身份证",
          rules: [],
        },
        {
          type: "input",
          label: "医联体",
          prop: "unionName",
          placeholder: "请输入医联体",
          rules: [],
        },
        {
          type: "btn",
          list: [
            {
              btnType: "button",
              type: "",
              style: {
                width: "80px",
                height: "32px",
                borderRadius: "4px",
                fontSize: "14px",
                marginLeft: "40px",
              },
              btnText: "查询",
              func: () => {
                this.onClickSearch()
              },
            },
            // {
            //   btnType: "tobeModified",
            //   tobeModified: 20,
            // },
          ],
        },
      ],
      formEdit: {},
    }
  },
  watch: {
    pageSize(val) {
      sessionStorage.setItem("followQuery-pageSize", val)
    },
    //当前页
    pageIndex(val) {
      sessionStorage.setItem("followQuery-pageIndex", val)
    },
    followId(v) {
      if (v) {
        this.listLoading = true
        this.$nextTick(() => {
          setTimeout(() => {
            this.listLoading = false
            let query = { ...this.$route.query }
            this.$router.replace({ query: {} })
            this.$router.push({
              path: "/followupentry",
              query: query,
            })
          }, 500)
        })
      }
    },
  },
  created() {
    this.initSearchForm()
  },
  mounted() {
    this.followId = this.$route.query.followId
    // this.formEdit = sessionStorage.getItem('followQuery-form') ? JSON.parse(sessionStorage.getItem('followQuery-form')) : {}
    this.pageSize = Number(sessionStorage.getItem("followQuery-pageSize")) || 10
    this.pageIndex =
      Number(sessionStorage.getItem("followQuery-pageIndex")) || 1
    this.getCurrentFormByType(2)
  },
  methods: {
    changeModified() {
      this.modifiedFlag = !this.modifiedFlag
    },
    setSelectedIndex(i) {
      console.log(this.selectedIndex)
      this.selectedIndex = i
      sessionStorage.setItem("homeSelectedIndex", this.selectedIndex)
    },
    handleSizeChange(v) {
      console.log(v)
    },
    handleEdit(data, i, index) {
      this.$router.push({
        path: "/followupentry",
        query: {
          patientId: data.patientId,
          followId: data.id,
          model: "edit",
          getData: 1,
        },
      })
    },
    rowOpration(data, i, index) {
      console.log(data)
      if (data.followBatch == 0) {
        // 筛查
        this.$router.push({
          path: "/followupentry",
          query: {
            patientId: data.patientId,
            followId: data.id,
            model: "view",
            getData: 1,
            formType: 1, // 去筛查的表单还是随访的表单
          },
        })
      } else {
        this.$router.push({
          path: "/followupentry",
          query: {
            patientId: data.patientId,
            followId: data.id,
            model: "view",
            getData: 1,
            formType: 2,
          },
        })
      }
    },
    onSearch(form) {
      console.log(form)
      sessionStorage.setItem("followQuery-form", JSON.stringify(form))
      this.formEdit = form
      // this.handleSearch()
    },
    onClickSearch() {
      this.pageIndex = 1
      this.handleSearch()
    },
    async handleSearch() {
      this.listLoading = true
      console.log(this.$refs.form.form)

      let params = {
        pageSize: this.pageSize,
        pageNum: this.pageIndex,
        formId: this.formId,
        ...this.$refs.form.form,
        patientFrom: sessionStorage.getItem("selectedIndex"), //筛查场景
      }
      if (this.formEdit) {
        params = { ...Object.assign(this.formEdit, params) }
      }
      for (let key in params) {
        if (params[key] !== "" && params[key] !== null) {
          if (key.includes("Time") && params[key]) {
            params["start" + key] = params[key][0]
            params["end" + key] = params[key][1]
            delete params.ScreeningTime
          } else {
            params[key] = params[key]
          }
        }
      }
      let res = await getFollowSearch(params)
      if (res.code == 1) {
        //分页内容
        const d = res.data
        this.total = d.total
        this.tableData = d["records"]
      }
      this.listLoading = false
    },
    //查询表单枚举值字典
    initSearchForm() {
      this.formList.forEach((item, index) => {
        if (item.trans) {
          if (!item["opts"]) {
            item["opts"] = []
          } else {
            item["opts"] = this.dictMap[item.trans]
          }
        }
      })
      this.$forceUpdate()
    },
    //获取随访表单类型
    getCurrentFormByType(type) {
      getCurrentFormByType({
        type,
        groupId: this.$store.state.user.group.groupId || "",
      }).then((res) => {
        if (res.code === 1) {
          this.formId = res.data[0].id
          this.handleSearch()
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.screeningSearch {
  .top {
    width: 100%;
    // height: 72px;
    padding: 0 116px 0 44px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .bot {
    padding: 0 24px;
  }
}
</style>
