<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="deptName">
        <el-input
          v-model="queryParams.deptName"
          clearable
          placeholder="请输入科室名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室类型" prop="deptType">
        <el-select
            v-model="queryParams.deptType"
            clearable
            placeholder=""
            size="small"
            @change="handleQuery"
        >
          <el-option
            :key="''"
            :label="'全部'"
            :value="''"
          />
          <el-option
            v-for="dict in deptTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查类型" prop="visitType">
        <el-select
          v-model="queryParams.visitType"
          clearable
          placeholder=""
          size="small"
          @change="handleQuery"
        >
          <el-option v-for="item in Examtypes " :key="item.typeCode" :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 icon="el-icon-plus" size="mini" type="primary" @click="addDept">新增</el-button>
        <el-button
          :disabled="single"
          icon="el-icon-edit"
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
       <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-refresh-right"
          size="mini"
          @click="handleSync"
        >同步</el-button>
      </el-col>
    </el-row> -->

    <el-table ref="multipleTable" v-loading="loading"
              :data="deptList" border
              stripe tooltip-effect="light"  @row-click="handleRowClick"
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>

      <el-table-column align="left" fixed="left" label="科室名称" min-width="150" prop="deptName" show-overflow-tooltip/>
      <el-table-column align="left" fixed="left" label="科室编码" min-width="110" prop="deptId" show-overflow-tooltip/>
      <el-table-column :formatter="deptTypeFormat" align="center" label="科室类型" prop="deptType" width="150"/>
      <el-table-column align="center" label="检查类型" prop="visitType" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.visitType" allow-create clearable filterable placeholder="请选择"
                     @change="inputBlur(scope.row)"
          >
            <el-option v-for="item in Examtypes " :key="item.typeCode" :label="item.typeName" :value="item.typeCode">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
<!--      <el-table-column align="left" label="拼音码" prop="pyCode" show-overflow-tooltip width="120"/>-->
      <el-table-column label="具体位置" min-width="400" prop="address" show-overflow-tooltip/>
<!--      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="editDept(scope.row)">修改</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 :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="orgId">
          <el-select
            v-model="form.orgId"
            clearable
            placeholder="请选择"
            size="small"
          >
            <el-option
              v-for="dict in orgList"
                :key="dict.orgCode"
                :label="dict.orgName"
                :value="dict.orgCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="院区" prop="yardId">
          <el-select
              v-model="form.yardId"
              clearable
              placeholder="请选择"
              size="small"
          >
            <el-option
                v-for="dict in yardList"
                :key="dict.yardCode"
                :label="dict.yardName"
                :value="dict.yardCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="科室编码" prop="deptId">
          <el-input v-model="form.deptId" :disabled="form.id"/>
        </el-form-item>
        <el-form-item label="科室名称" prop="deptName">
          <el-input v-model="form.deptName"/>
        </el-form-item>
        <el-form-item label="科室类型" prop="deptType">
          <el-select
              v-model="form.deptType"
              clearable
              placeholder="请选择"
              size="small"
          >
            <el-option
                v-for="dict in deptTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检查类型" prop="visitType">
          <el-select v-model="form.visitType"
                     allow-create
                     placeholder=""
          >
            <el-option v-for="item in Examtypes "
                       :key="item.typeCode"
                       :label="item.typeName"
                       :value="item.typeCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="具体位置" prop="address">
          <el-input v-model="form.address" autosize 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 { listDept, getDept, delDept, addDept, updateDept, exportDept, syncDept } from '@/api/basis/dept'
import { listExamtype } from '@/api/basis/examtype'
import { listOrg } from '@/api/system/org'
import { listYard } from '@/api/basis/yard'
import vPinyin from '@/utils/vue-py'

export default {
  name: 'deptModel',
  props: {
    /* 编辑器的内容 */
    value: Number
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科室信息表格数据
      deptList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 科室类型字典
      deptTypeOptions: [],
      Examtypes: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        deptId: null,
        deptName: null,
        deptType: '',
        pyCode: null
      },
      // 表单参数
      form: {
        id: null,
        deptId: '',
        deptName: '',
        deptType: '',
        visitType: '',
        address: '',
        orgId: '',
        yardId: '',
        pyCode: ''
      },
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: '组织机构编码不能为空', trigger: 'blur' }
        ],
        yardId: [
          { required: true, message: '院区不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '科室编码不能为空', trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: '科室名称不能为空', trigger: 'blur' }
        ],
        deptType: [
          { required: true, message: '科室类型不能为空', trigger: 'change' }
        ]
      },
      orgList: [],
      yardList: []
    }
  },
  created() {
    this.getList()
    this.getDicts('dept_type').then(response => {
      this.deptTypeOptions = response.data
    })
    listExamtype().then(response => {
      this.Examtypes = response.rows
    })
    listOrg({ isValid: 1 }).then(response => {
      this.orgList = response.rows
    })
    listYard({ isValid: 1 }).then(response => {
      this.yardList = response.rows
    })
  },
  methods: {
    handleRowClick(	row, column, event){
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    editDept(row) {
      this.title = '编辑'
      this.form = {
        id: row.id,
        deptId: row.deptId,
        deptName: row.deptName,
        deptType: row.deptType,
        visitType: row.visitType,
        address: row.address,
        orgId: row.orgId,
        yardId: row.yardId,
        pyCode: row.pyCode
      }
      this.$nextTick(() => {
        this.open = true
      })
    },
    addDept() {
      this.title = '新增'
      this.form = {
        id: null,
        deptId: '',
        deptName: '',
        deptType: '',
        visitType: '',
        address: '',
        orgId: '',
        yardId: '',
        pyCode: ''
      }
      this.$nextTick(() => {
        this.open = true
      })
    },
    /** 查询科室信息列表 */
    getList() {
      this.loading = true
      listDept(this.queryParams).then(response => {
        this.deptList = response.rows
        this.total = response.total
        this.loading = false
        console.log('打印院区' + this.deptList[0].yardName)
      })
    },
    // 科室类型字典翻译
    deptTypeFormat(row, column) {
      return this.selectDictLabel(this.deptTypeOptions, row.deptType)
    },
    inputBlur(val) {
      let obj = { 'id': val.id, 'visitType': val.visitType }
      updateDept(obj).then(res => {
        console.log(res)
      })
    },
    /*同步 */
    handleSync() {
      this.$confirm(
          '确认同步用户信息?',
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
      )
          .then(function() {
            return syncDept()
          })
          .then(() => {
            this.getList()
            this.msgSuccess('同步成功')
          })
          .catch(function() {
          })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgId: null,
        yardId: null,
        deptId: null,
        deptName: null,
        pyCode: null,
        deptType: this.queryParams.deptType,
        locationBuildingId: null,
        locationFloorId: null,
        address: null,
        updatedByName: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    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
      getDept(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改科室信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title === '修改科室信息') {
            updateDept({
              ...this.form,
              pyCode: this.form.pyCode || vPinyin.chineseToPinYin(this.form.deptName)
            }).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDept({
              ...this.form,
              pyCode: vPinyin.chineseToPinYin(this.form.deptName)
            }).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 delDept(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有科室信息数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportDept(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
