<template>
  <div class="app-container pageSediment">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="项目名称" prop="outExamItemName">
        <el-input
          v-model="queryParams.outExamItemName"
          clearable
          placeholder="项目名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          clearable
          placeholder="项目编码"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查类型" prop="examTypeId">
        <el-select
          v-model="queryParams.examTypeId"
          clearable
          placeholder="请输入检查类型"
        >
          <el-option
            :key="0"
            :label="'全部'"
            :value="''"
          />
          <el-option
            v-for="item in examtypeList"
            :key="item.id"
            :label="item.typeName"
            :value="item.typeCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="cyan" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          v-hasPermi="['basis:outexamitem:add']"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
        <el-button
          v-hasPermi="['basis:outexamitem:edit']"
          :disabled="single"
          icon="el-icon-edit"
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
        <el-button
          v-hasPermi="['basis:outexamitem:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table ref="multipleTable" v-loading="loading"
              :data="outexamitemList" border stripe tooltip-effect="light" @row-click="handleRowClick"
              @selection-change="handleSelectionChange"
              @sort-change="sortChange"
    >
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="left" label="项目编码" min-width="160" prop="itemCode"
                       show-overflow-tooltip
      />
      <el-table-column align="left" label="HIS系统查项目名" min-width="160" prop="outExamItemName"
                       show-overflow-tooltip
      />
      <el-table-column v-if="false" align="left" label="预约系统检查项目名" min-width="300"
                       prop="inExamItemName"
                       show-overflow-tooltip
      />
      <el-table-column :formatter="selectdeptType" align="center" label="检查类型" prop="EXAM_TYPE_ID" show-overflow-tooltip
                       sortable="custom" width="120"
      />
      <el-table-column :formatter="isEmptyStomachFormat" align="center" label="空腹属性" prop="isEmptyStomach"
                       show-overflow-tooltip width="120"
      />
      <el-table-column align="center" label="部位数" prop="positionCount" show-overflow-tooltip width="90"/>
      <el-table-column :formatter="bodyTypeFormat" align="center" label="肢体类型" prop="bodyType" show-overflow-tooltip
                       width="90"
      />
      <el-table-column :formatter="delayTimeFormat" align="center" label="延迟时间" prop="delayTime" show-overflow-tooltip
                       width="90"
      />
      <el-table-column :formatter="ageLimitFormat" align="center" label="年龄限制" prop="ageLimit" show-overflow-tooltip
                       width="90"
      />
      <el-table-column :formatter="examTimeFormat" align="left" label="检查时长" prop="examTime" show-overflow-tooltip
                       width="150"
      />
      <el-table-column align="left" label="项目注意事项" min-width="300" prop="individualRemarks"
                       show-overflow-tooltip
      />
      <el-table-column align="left" label="预约特殊提示" min-width="300" prop="specialTip"
                       show-overflow-tooltip
      />
      <el-table-column v-if="false" :formatter="selectyardId" align="left" label="院区名称" prop="yardId" width="200"/>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['basis:outexamitem:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['basis:outexamitem:remove']"
                >删除</el-button>
              </template>
            </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改外部检查项目对话框 -->
    <el-dialog v-dialogDrag :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body
               width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="院区" prop="yardId">
          <el-select v-model="form.yardId" placeholder="请选择院区">
            <el-option
              v-for="item in yardList"
              :key="item.id"
              :label="item.yardName"
              :value="item.yardCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检查类型" prop="examTypeId">
          <el-select v-model="form.examTypeId"
                     placeholder="请输入检查类型"
                     @change="selectInExamItem"
          >
            <el-option
              v-for="item in examtypeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.typeCode"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="内部检查项目" prop="inExamItemName">
                  <el-select v-model="form.inExamItemName" placeholder="请输入内部检查项目名">
                    <el-option
                    v-for="item in inExamItemList"
                    :key="item.itemCode"
                    :label="item.inExamItemName"
                    :value="item.inExamItemName"
                    />
                  </el-select>
                </el-form-item>-->
        <el-form-item label="项目编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入项目编码"/>
        </el-form-item>
        <el-form-item label="项目名称" prop="outExamItemName">
          <el-input v-model="form.outExamItemName" placeholder="请输入外部检查项目名"/>
        </el-form-item>
        <el-form-item label="部位数" prop="positionCount">
          <el-input v-model="form.positionCount" placeholder="请输入部位数"/>
        </el-form-item>
        <el-form-item label="空腹属性" prop="isEmptyStomach">
          <el-select v-model="form.isEmptyStomach" placeholder="请选择空腹属性">
            <el-option
              v-for="dict in isEmptyStomachOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="肢体类型" prop="bodyType">
          <el-select v-model="form.bodyType" clearable placeholder="请选择肢体类型">
            <el-option
              v-for="dict in bodyTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="延迟时间" prop="delayTime">
          <el-select v-model="form.delayTime" placeholder="请选择项目延迟时间">
            <el-option
              v-for="dict in itemDelayTime"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄限制" prop="ageLimit">
          <el-input v-model="form.ageLimit" :precision="0" placeholder="年龄超过禁止预约此项目" style="width: 350px"/>
        </el-form-item>
        <el-form-item label="尿液属性" prop="isEmptyBladder">
          <el-select v-model="form.isEmptyBladder" placeholder="请选择尿液属性">
            <el-option
              v-for="dict in isEmptyBladderOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue?parseInt(dict.dictValue):dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查时长" prop="examTime">
          <el-input v-model="form.examTime" placeholder="请输入检查时长">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="注意事项" prop="individualRemarks">
          <el-input v-model="form.individualRemarks" placeholder="请输入项目注意事项" type="textarea"/>
        </el-form-item>
        <el-form-item label="特殊提示" prop="specialTip">
          <el-input v-model="form.specialTip" placeholder="请输入特殊提示" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOutexamitem,
  getOutexamitem,
  delOutexamitem,
  addOutexamitem,
  updateOutexamitem,
  exportOutexamitem,
  syncOutexamitem
} from '@/api/basis/outexamitem'
import { listInExamItem } from '@/api/basis/inExamItem'
import { listYard } from '@/api/basis/yard'
import { listExamtype } from '@/api/basis/examtype'

export default {
  name: 'Outexamitem',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外部检查项目表格数据
      outexamitemList: [],
      inExamItemList: [],
      // 检查类型表格数据
      examtypeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      //院区
      yardList: [],
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orgId: null,
        yardId: null,
        examTypeId: null,
        pyCode: null,
        outExamItemName: null,
        itemCode: null,
        inExamItemName: null,
        isValid: null,
        sort: []
      },
      // 表单参数
      form: {},
      // 项目是否空腹，0为不空腹，1为空腹字典
      isEmptyStomachOptions: [],
      // 是否排尿，0为不排尿，1为排尿字典
      isEmptyBladderOptions: [],
      itemDelayTime: [], // 项目延时时间
      bodyTypeOptions: [], // 肢体类型字典
      // 表单校验
      // 表单校验
      rules: {
        yardId: [
          { required: true, message: '检查类型编码不能为空', trigger: 'change' }
        ],
        examTypeId: [
          { required: true, message: '项目编码不能为空', trigger: 'blur' }
        ],
        itemCode: [
          { required: true, message: '部位数不能为空', trigger: 'blur' }
        ],
        outExamItemName: [
          { required: true, message: '空腹属性不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('empty_stomach').then(response => {
      this.isEmptyStomachOptions = response.data
    })
    this.getDicts('empty_bladder').then(response => {
      this.isEmptyBladderOptions = response.data
    })
    this.getDicts('item_delay_time').then(response => {
      this.itemDelayTime = response.data
    })
    this.getDicts('body_type').then(response => {
      this.bodyTypeOptions = response.data
    })
  },
  methods: {
    // 项目是否空腹，0为不空腹，1为空腹字典翻译
    isEmptyStomachFormat(row, column) {
      return this.selectDictLabel(this.isEmptyStomachOptions, row.isEmptyStomach)
    },
    bodyTypeFormat(row) {
      return this.selectDictLabel(this.bodyTypeOptions, row.bodyType)
    },
    delayTimeFormat(row, column) {
      return this.selectDictLabel(this.itemDelayTime, row.delayTime)
    },
    ageLimitFormat(row, column) {
      if (row.ageLimit) {
        if (row.ageLimit + '' === '0') {
          return '无限制'
        }
        return row.ageLimit + '岁'
      } else {
        return '无限制'
      }
    },
    examTimeFormat(row, column) {
      if (row.examTime) {
        return row.examTime + ' 分钟'
      }
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    // 排序信息修改
    sortChange({ prop, order }) {
      if (order === 'ascending') {
        order = 'asc'
      } else if (order === 'descending') {
        order = 'desc'
      }
      let index = this.queryParams.sort.findIndex(item => item.field === prop)
      if (index === -1) {
        this.queryParams.sort.push({ field: prop, order })
      } else {
        this.queryParams.sort.splice(index, 1, { field: prop, order })
      }
      this.$nextTick(() => {
        this.getList()
      })
    },
    /** 查询外部检查项目列表 */
    getList() {
      this.loading = true
      listOutexamitem({
        ...this.queryParams,
        sort: null,
        inExamItemName: (this.queryParams.sort && this.queryParams.sort.length > 0) ? JSON.stringify(this.queryParams.sort) : ''
      }).then(response => {
        this.outexamitemList = response.rows
        this.total = response.total
        this.loading = false
      })
      listYard({ isValid: 1 }).then(response => {
        this.yardList = response.rows
      })
      listExamtype({ isValid: 1 }).then(response => {
        this.examtypeList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgId: null,
        yardId: null,
        examTypeId: null,
        pyCode: null,
        outExamItemName: null,
        itemCode: null,
        inExamItemName: null,
        createdById: null,
        createdByName: null,
        createdAt: null,
        updatedById: null,
        updatedByName: null,
        updatedAt: null
      }
      this.resetForm('form')
      this.inExamItemList = []
    },
    /**检查类型*/
    selectdeptType(row, column) {
      let name = ''
      this.examtypeList.forEach(i => {
        if (row.examTypeId == i.typeCode) {
          name = i.typeName
        }
      })
      return name
    },
    /**院区名称*/
    selectyardId(row, column) {
      let yname = ''
      this.yardList.forEach(i => {
        if (row.yardId == i.yardCode) {
          yname = i.yardName
        }
      })
      return yname
    },
    /*同步 */
    handleSync() {
      this.$confirm(
        '确认同步用户信息?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return syncOutexamitem()
        })
        .then(() => {
          this.getList()
          this.msgSuccess('同步成功')
        })
        .catch(function() {
        })
    },
    selectInExamItem(val, ff) {
      this.inExamItemList = []
      if (ff == undefined) {
        this.form.inExamItemName = null
      }

      listInExamItem({ examTypeId: val }).then(response => {
        this.inExamItemList = response.rows
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOutexamitem(id).then(response => {
        this.form = response.data
        this.selectInExamItem(this.form.examTypeId, 'true')
        this.open = true
        this.title = '修改'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.form.ageLimit) {
            this.form.ageLimit = ''
          }
          if (this.form.id != null) {
            updateOutexamitem(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addOutexamitem(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除外部检查项目编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delOutexamitem(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有外部检查项目数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportOutexamitem(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
