<template>
  <!-- 随访调查 -->
  <div class="screeningSearch">
    <template v-if="!show">
      <div class="top">
        <form-components
          :forms="formList"
          :formEdit="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"
          :sort-change="sortChange"
          @pageSizeChange="handleSizeChange"
          @currentPageChange="handleCurrentChange"
        />
      </div>
    </template>
    <followupentry v-if="show" @changeShow="changeShow"></followupentry>
  </div>
</template>
<script>
import FormComponents from "@/components/FormComponents"
import followupentry from "../followupentry"
import paginationMixin from "@/components/TabComponents/mixin"
import searchMixin from "../searchMixin"
import { getCurrentFormByType } from "@/api/coop-group.js"
import { getFollowSurvey } from "@/api/followup"
import { mapGetters } from "vuex"
export default {
  components: {
    FormComponents,
    followupentry,
  },
  mixins: [paginationMixin, searchMixin],
  data() {
    return {
      formId: "", //随访表单类型
      listLoading: false,
      show: false,
      keyword: "",
      modifiedFlag: false,
      columns: [
        {
          label: "医联体",
          minWidth: 120,
          value: "unionName",
          formatter: (row) => {
            return row.unionName ? row.unionName : "--"
          },
          sortable: "custom",
        },
        {
          label: "姓名",
          minWidth: 120,
          formatter: (row) => {
            return row.name ? row.name : "--"
          },
          value: "name",
          sortable: "custom",
        },
        {
          label: "性别",
          minWidth: 80,
          value: "sex",
          formatter: (row) => {
            return this.$handle.formatDicList(
              this.dictMap["d-sex"],
              row.sex + ""
            )
          },
          sortable: "custom",
        },
        {
          label: "身份证",
          minWidth: 120,
          value: "idCard",
          formatter: (row) => {
            return row.idCard ? row.idCard : "--"
          },
          sortable: "custom",
        },
        {
          label: "年龄",
          minWidth: 120,
          value: "age",
          formatter: (row) => {
            return row.age ? row.age : "--"
          },
          sortable: "custom",
        },
        {
          label: "筛查时间",
          minWidth: 180,
          value: "screeningTime",
          formatter: (row) => {
            return row.screeningTime ? row.screeningTime : "--"
          },
          sortable: "custom",
        },

        {
          label: "风险评估结果",
          minWidth: 130,
          value: "riskRank",
          formatter: (row) => {
            let riskRank = {
              low: "低危",
              medium: "中危",
              high: "高危",
            }
            return row.riskRank ? riskRank[row.riskRank] : "--"
          },
          sortable: "custom",
        },
        {
          label: "上次随访时间",
          value: "followTime",
          minWidth: 130,
          formatter: (row) => {
            return row.followTime ? row.followTime : "--"
          },
          sortable: "custom",
        },
        {
          label: "随访进度",
          value: "followBatch",
          minWidth: 120,
          formatter: ({ followBatch }) => {
            return followBatch
              ? `第${this.$handle.toChineseNumber(followBatch)}次随访`
              : "筛查"
          },
          sortable: "custom",
        },
        {
          label: "计划随访时间",
          value: "nextFollowTime",
          // formatter: (row) => {
          //   return this.$handle.formatDicList(
          //     this.dictMap["next_follow_time"],
          //     String(row.nextFollowTime)
          //   )
          // },
          sortable: "custom",
          minWidth: 130,
        },
        {
          label: "操作",
          width: 100,
          fixed: "right",
          operType: "button",
          operations: [
            {
              func: this.rowOpration,
              formatter(row) {
                return {
                  label: " 录入",
                  type: "text",
                }
              },
              // isHidden({ isContinueFollow, checkStatus, followBatch }) {
              //   return (
              //     isContinueFollow !== 1 ||
              //     (followBatch > 0 && checkStatus !== 3)
              //   )
              // },
            },
          ],
        },
      ],
      tableData: [],
      formList: [
        {
          xs: 24,
          sm: 12,
          md: 12,
          lg: 7,
          xl: 7,
          type: "select",
          label: "计划随访时间",
          prop: "nextFollowTime",
          trans: "next_follow_time",
          placeholder: "请选择计划随访时间",
          rules: [],
          opts: [{ label: "123", value: "1" }],
        },
        {
          xs: 24,
          sm: 12,
          md: 12,
          lg: 7,
          xl: 7,
          type: "input",
          label: "姓名",
          prop: "name",
          placeholder: "请输入姓名",
          rules: [],
        },
        {
          xs: 24,
          sm: 12,
          md: 12,
          lg: 7,
          xl: 7,
          type: "input",
          label: "身份证",
          prop: "idCard",
          placeholder: "请输入身份证",
          rules: [],
        },
        {
          xs: 24,
          sm: 12,
          md: 12,
          lg: 7,
          xl: 7,
          type: "input",
          label: "医联体",
          prop: "unionName",
          placeholder: "请输入医联体",
          rules: [],
        },
        {
          xs: 24,
          sm: 12,
          md: 12,
          lg: 7,
          xl: 7,
          type: "select",
          label: "随访进度",
          prop: "followBatch",
          trans: "follow_type",
          placeholder: "请选择随访进度",
          rules: [],
          opts: [
            { label: "筛查", value: "0" },
            { label: "随访", value: "1" },
          ],
        },
        {
          label: "筛查场景",
          type: "select",
          prop: "patientFrom",
          opts: [
            { label: "社区筛查", value: "1" },
            { label: "医院筛查", value: "2" },
            { label: "体检筛查", value: "3" },
          ],
        },
        {
          xs: 1,
          sm: 2,
          md: 2,
          lg: 2,
          xl: 2,
          type: "btn",
          list: [
            {
              btnType: "button",
              type: "",
              style: {
                width: "80px",
                height: "32px",
                borderRadius: "4px",
                fontSize: "14px",
                marginLeft: "40px",
              },
              btnText: "查询",
              func: this.onSearch,
            },
            // {
            //   btnType: "tobeModified",
            //   tobeModified: 20,
            // },
          ],
        },
      ],
      formEdit: {},
      cacheForm: {},
    }
  },
  computed: {
    ...mapGetters({
      // selectedIndex: "table/selectedIndex",
      refreshFlag: "table/refreshFlag",
    }),
  },
  watch: {},
  created() {
    this.initSearchForm()
  },
  mounted() {
    this.handleSearch() //调试注释
  },
  methods: {
    changeShow() {
      this.$router.push({ query: {} })
      this.show = false
    },
    changeModified() {
      this.modifiedFlag = !this.modifiedFlag
    },
    changePage(v) {
      this.page[v.type] = v.value
      this.$refs.customTable.loading = false
    },
    onSearch(form) {
      sessionStorage.setItem("followResearch-form", JSON.stringify(form))
      this.formEdit = form
      this.pageIndex = 1
      this.handleSearch()
    },
    sortChange({ prop, order }) {
      const sort = order ? (order === "ascending" ? "asc" : "desc") : ""
      const sortField = order ? prop : ""
      this.handleSearch({ sortField, sort })
    },
    rowOpration(data, i) {
      sessionStorage.setItem("formEdit", JSON.stringify(data))
      this.$router.push({
        // path: `/followupentry`,
        query: {
          patientId: data.patientId,
          getData: 0,
          formType: "2",
          model: "add",
        },
      })
      this.show = true
    },
    async handleSearch(form = {}) {
      Object.assign(this.cacheForm, form)
      this.listLoading = true
      let params = {
        pageSize: this.pageSize,
        pageNum: this.pageIndex,
        ...this.cacheForm,
      }
      if (this.formEdit) {
        params = Object.assign(this.formEdit, params)
      }
      let res = await getFollowSurvey(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()
    },
    // async initDict(type,index){
    //   let params={
    //     type:type
    //   }
    //   const res = await getDictDetail(params)
    //   let item = this.formList.find(_ => _.trans == type)
    //   res.data.forEach((itemD,inx) => {
    //     // if(!this.columns[index]['transList']){
    //     //    this.columns[index]['transList']={}
    //     // }
    //     // else{
    //     //   this.columns[index]['transList'][itemD.code]=itemD.name
    //     // }
    //     if(item){
    //       if(!item['opts']){
    //        item['opts']=[]
    //     }
    //     else {
    //         item['opts']=res.data.map((_) => {
    //           return {
    //             label: _.name,
    //             value: _.code,
    //           }
    //         })
    //       }
    //     }
    //   })
    // },
    //获取随访表单类型
    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>
