<template>
  <div class="container">
    <div v-show="!isDetail">
      <direct-search
        ref="form"
        :label-position="'right'"
        :forms="searchList"
        :style="{ textAlign: 'left' }"
        @handleSearch="handleFormSearch"
      />
      <el-table-self
        ref="table"
        :table-data="tableData"
        :columns="columns"
        :list-loading="listLoading"
        :current-page="pageIndex"
        :total-count="total"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        @pageSizeChange="handleSizeChange"
        @currentPageChange="handleCurrentChange"
        :sortChange="sortChange"
      />
    </div>

    <div v-if="isDetail">
      <div>
        <el-button icon="el-icon-back" @click="isDetail = false"
          >返 回</el-button
        >
      </div>
      <ConfigForms
        form-type="1"
        :patient-id="patientId"
        :disabled="disabled"
      ></ConfigForms>
    </div>
  </div>
</template>

<script>
import paginationMixin from "@/components/TabComponents/mixin"
import { getPatientPage } from "@/api/patient.js"
import ConfigForms from "./components/ConfigForms.vue"
export default {
  name: "ScreeningIndex",
  mixins: [paginationMixin],
  components: { ConfigForms },
  data() {
    return {
      isDetail: false,
      listLoading: false,
      disabled: false,
      tableData: [],
      cacheForm: {},
      searchList: [
        {
          type: "input",
          label: "姓名",
          prop: "patientName",
        },

        {
          label: "性别",
          type: "select",
          prop: "gender",
          optsFormatter: () => {
            return this.dictMap && this.dictMap["d-sex"]
          },
        },
        {
          type: "daterange",
          dateType: "datetimerange",
          label: "创建时间",
          prop: "CreateTime",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
        },
        {
          type: "daterange",
          dateType: "datetimerange",
          label: "保存时间",
          prop: "Time",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
        },
        {
          type: "button",
          icon: "el-icon-search",
          value: "搜索",
        },
      ],
      columns: [
        {
          label: "姓名",
          minWidth: 120,
          value: "hzxx_xm",
        },
        {
          label: "出生日期",
          minWidth: 120,
          value: "hzxx_csrq",
        },

        {
          label: "性别",
          minWidth: 80,
          value: "hzxx_xb",
          formatter: (row) => {
            return this.$handle.formatDicList(
              this.dictMap["d-sex"],
              row.hzxx_xb
            )
          },
        },
        {
          label: "创建时间",
          minWidth: 180,
          sortable: "custom",
          value: "create_time",
        },
        {
          label: "保存时间",
          minWidth: 180,
          sortable: "custom",
          value: "update_time",
        },
        {
          label: "操作",
          width: 180,
          fixed: "right",
          operType: "button",
          operations: [
            {
              func: this.handleView,
              formatter(row) {
                return {
                  label: "查看",
                  type: "text",
                }
              },
            },
            {
              func: this.handleAdd,
              formatter(row) {
                return {
                  label: "修改",
                  type: "text",
                }
              },
            },
          ],
        },
      ],
    }
  },
  methods: {
    handleView(row) {
      this.handleAdd(row, null, true)
    },
    handleAdd({ patient_id, hzxx_xm }, index, disabled = false) {
      this.disabled = disabled
      this.isDetail = true
      this.patientId = patient_id || null
      this.name = hzxx_xm
    },

    sortChange({ prop, order }) {
      const asc = order ? (order === "ascending" ? true : false) : ""
      const column = order ? prop : ""
      this.handleSearch({ "orders[0].asc": asc, "orders[0].column": column })
    },
    // 查询
    handleFormSearch(form) {
      this.pageIndex = 1
      this.handleSearch(form)
    },
    handleSearch(form) {
      this.listLoading = true
      const params = Object.assign(this.cacheForm, form)
      const data = {}
      for (let key in params) {
        if (params[key] !== "" && params[key] !== null) {
          if (key.includes("Time") && params[key]) {
            data["start" + key] = params[key][0]
            data["end" + key] = params[key][1]
          } else {
            data[key] = params[key]
          }
        }
      }
      data.current = this.pageIndex
      data.size = this.pageSize
      getPatientPage(data).then((res) => {
        this.listLoading = false
        if (res.code === 1) {
          const d = res.data
          this.tableData = d.records || []
          this.total = Number(d.total)
        }
      })
    },
  },
  created() {
    this.handleFormSearch()
  },
}
</script>

<style lang="scss" scoped></style>
