<template>
  <!-- 随访查询 -->
  <div class="screeningSearch">
    <div v-show="!isDetail && !show">
      <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>
    <FollowUpDetail
      :currentRow="currentRow"
      v-if="isDetail"
      @back="back"
    ></FollowUpDetail>
    <followupentry v-if="show" @changeShow="changeShow"></followupentry>
  </div>
</template>
<script>
import FormComponents from "@/components/FormComponents"
import paginationMixin from "@/components/TabComponents/mixin"
import searchMixin from "../searchMixin"
import { getCurrentFormByType } from "@/api/coop-group.js"
import { getFollowSearch } from "@/api/followup"
import FollowUpDetail from "./FollowUpDetail.vue"
import followupentry from "../followupentry"
export default {
  components: {
    FormComponents,
    FollowUpDetail,
    followupentry,
  },
  mixins: [paginationMixin, searchMixin],
  data() {
    return {
      isDetail: false,
      show: false,
      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: "审核状态",
          value: "checkStatus",
          minWidth: 120,
          formatter: (row) => {
            return this.$handle.formatDicList(
              this.dictMap["checkStatus"],
              String(row.checkStatus)
            )
          },
        },
        {
          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.handleDetail,
              formatter(row) {
                return {
                  label: "查看",
                  type: "text",
                }
              },
            },
            {
              isIndex: true,
              func: this.handleEdit,
              formatter(row) {
                return {
                  label: "修改",
                  type: "text",
                }
              },
              isHidden({ checkStatus, followBatch }) {
                return (
                  checkStatus === 3 || checkStatus === 4 || followBatch === 0
                )
              },
            },
          ],
        },
      ],
      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: {},
      currentRow: {},
    }
  },
  watch: {
    pageSize(val) {
      sessionStorage.setItem("followQuery-pageSize", val)
    },
    //当前页
    pageIndex(val) {
      sessionStorage.setItem("followQuery-pageIndex", val)
    },
  },
  created() {
    this.initSearchForm()
  },
  mounted() {
    const followId = this.$route.query.followId
    if (followId) {
      this.handleDetail({ ...this.$route.query })
    }
    // 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: {
    changeShow() {
      this.$router.push({ query: {} })
      this.show = false
    },
    back() {
      this.isDetail = false
      const followId = this.$route.query.followId
      if (followId) {
        this.$router.replace({ path: "/followupquery", query: {} })
      }
    },
    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.show = true
      this.$router.push({
        // path: "/followupentry",
        query: {
          patientId: data.patientId,
          followId: data.id,
          model: "edit",
          getData: 1,
          formType: 2,
        },
      })
    },
    handleDetail(row) {
      this.isDetail = true
      this.currentRow = row
    },
    onSearch(form) {
      console.log(form)
      this.pageIndex = 1
      sessionStorage.setItem("followQuery-form", JSON.stringify(form))
      this.formEdit = form
      this.handleSearch()
    },
    onClickSearch() {
      this.pageIndex = 1
      this.handleSearch()
    },
    async handleSearch() {
      this.listLoading = true

      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>
