<template>
  <div>
    <toolbar
      :dept-options="deptOptions"
      @onSearch="filterData"
    />
    <white-board>
      <el-row class="header" :gutter="20">
        <el-col class="toolbar-txt" :span="6">共有医生：{{ info.totalDoctorNum }}位</el-col>
        <el-col v-if="doctorType === 'ALL'" class="toolbar-txt" :span="6">关联内容：{{ info.doctorWithArticleNum }}位</el-col>
        <el-col v-else class="toolbar-txt" :span="6" />
        <el-col class="flex-end" :span="12">
          <el-button icon="el-icon-plus" type="primary" @click="handleAddItem">新建医生</el-button>
        </el-col>
      </el-row>
      <table-with-page
        :columns="columns"
        :items="items"
        :pagination="pagination"
        :page-change="pageChange"
      >
        <template #btn-box="scope">
          <el-button type="danger" @click="handleDeleteSelect(scope.selected)">删除</el-button>
        </template>
      </table-with-page>
    </white-board>
  </div>
</template>

<script>
import toolbar from './toolbar'
import EMITKEY from '@/const/emitNameKey'
import mxFilterList from '@/mixins/filterList'
import { getList, getDoctorWithArticleNum, changeShowStatus, deleteDoctors } from '@/model/doctor'
import { getTagTypeInArr, setStatus } from '@/utils'

export default {
  name: 'DoctorPage',

  components: { toolbar },

  mixins: [mxFilterList({
    fetchList: getList // 在下方data再声明一个 fetchList: getList 同等效果
  })],

  props: {
    doctorType: {
      type: String,
      default: ''
    },
    deptOptions: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {
      columns: [
        {
          key: 'selection'
        }, {
          key: 'hospital',
          name: '所属医院',
          minWidth: '80',
          'show-overflow-tooltip': true
        }, {
          key: 'dept',
          name: '所属科室',
          minWidth: '80'
        }, {
          key: 'doctorName',
          name: '医生名称',
          width: '120'
        }, {
          name: '头像',
          width: '100',
          render: (h, { row }) => {
            return h('img', {
              style: {
                width: '100%',
                height: 'auto'
              },
              attrs: {
                src: row.headUrl
              }
            })
          }
        }, {
          key: 'articleNum',
          name: '关联内容数',
          width: '100'
        }, {
          key: 'showStatus',
          name: '展示状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['展示中', '未展示'], ['success', 'danger']), val)
        }, {
          key: 'action',
          name: '操作',
          width: '180',
          render: (h, { row }) => {
            const isShow = row.isShow
            const action = []
            action.push(h('el-button', {
              props: {
                type: 'primary'
              },
              on: {
                click: () => this.view(row)
              }
            }, '详情'))
            action.push(h('el-button', {
              props: {
                type: isShow ? 'info' : 'primary',
                size: 'small'
              },
              on: {
                click: () => this.changeShowStatus(row)
              }
            }, isShow ? '禁用展示' : '开启展示'))
            return h('div', action)
          }
        }
      ],
      info: {
        totalDoctorNum: 0,
        doctorWithArticleNum: 0
      }
    }
  },

  watch: {
    doctorType: {
      handler(val) {
        this.internalFilterObj['status'] = val
      },
      immediate: true
    }
  },

  created() {
    this.bus.$on(EMITKEY.DOCTORRELOAD, this.reload)
  },

  beforedestroy() {
    this.bus.$off(EMITKEY.DOCTORRELOAD, this.reload)
  },
  methods: {
    loadCallBack(source) {
      this.info.totalDoctorNum = source.total
      this.loadNum()
    },

    async loadNum() {
      if (this.doctorType === 'WITHOUT_ARTICLE') {
        return false
      } else if (this.doctorType === 'ALL') {
        const { success, data, msg } = await getDoctorWithArticleNum({
          status: 'WITH_ARTICLE' || this.doctorType,
          data: {
            ...this.filter
          }
        })
        if (success) {
          this.info.doctorWithArticleNum = data.withArticle
        } else {
          this.$message.error(msg)
        }
      }
    },

    handleAddItem() {
      this.$doctorItemDrawer({
        modalType: 'add'
      })
    },

    async handleDeleteSelect(selected) {
      if (selected.length < 1) {
        this.$message.error(`请选择要删除的记录~`)
        return false
      }
      const { success, msg } = await deleteDoctors({
        data: {
          ids: selected.map(x => x.id)
        }
      })
      if (success) {
        this.$message.success('删除成功~')
        this.bus.$emit(EMITKEY.DOCTORRELOAD)
      } else {
        this.$message.error(msg)
      }
    },

    view(row) {
      this.$doctorItemDrawer({
        modalType: 'edit',
        id: row.id
      })
    },

    async changeShowStatus(row) {
      const { success, msg } = await changeShowStatus({
        id: row.id
      })
      if (success) {
        this.$message.success('切换成功~')
        this.bus.$emit(EMITKEY.DOCTORRELOAD)
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style scoped>
.header {
  padding: 4px 20px 6px 20px;
}
.toolbar-txt {
  font-size: 13px;
  padding: 5px;
  line-height: 1.7;
}
.flex-end {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
