<!-- 调配管理 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <div class="query-box">
        <div class="query-box__left">
          <el-form-item label="" prop="yljgId">
            <el-select
              v-model="queryParams.yljgId"
              placeholder="请选择医疗机构"
              class="defwidth"
            >
              <el-option
                v-for="item in listMedicalMechanismArr"
                :key="item.id"
                :value="item.id"
                :label="item.name"
                :disabled="item.state == 1 ? false : true"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="checkTime">
            <el-date-picker
              style="width:260px"
              clearable
              class="defwidth"
              size="small"
              v-model="queryParams.checkTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="审核开始时间"
              end-placeholder="审核结束时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="" prop="isAllocation">
            <el-select v-model="queryParams.isAllocation" placeholder="调配状态"
                       style="width:160px"
            >
              <el-option
                v-for="item in deployStateArr"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="allocateNickName">
            <el-input
              v-model="queryParams.allocateNickName"
              style="width: 190px;"
              placeholder="请输入调配师姓名 / 工号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="" prop="reqStr">
            <el-input
              v-model="queryParams.reqStr"
              style="width: 190px;"
              placeholder="请输入患者姓名 / 处方号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
            </el-button
            >
          </el-form-item>
        </div>
      </div>
    </el-form>
    <el-table
      v-loading="loading"
      :data="allocateAllocate"
      @selection-change="handleSelectionChange"
      border
      size="mini"
      class="prescriptionCore"
    >
      <el-table-column type="index" width="55" align="center" label="序号"/>
      <el-table-column label="医疗机构" align="center" prop="yijgName" show-overflow-tooltip/>
      <el-table-column label="处方号" align="center" prop="preNo"/>
      <el-table-column label="门诊/住院" align="center" prop="preMzZy">
        <template slot-scope="scope">{{ scope.row.preMzZy == 1 ? '门诊' : '住院' }}</template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="name"/>
      <el-table-column label="性别" align="center" prop="sex"/>
      <el-table-column label="临床诊断" align="center" prop="symName">
        <template slot-scope="scope">{{ scope.row.disName }}-{{ scope.row.symName }}</template>
      </el-table-column>
      <el-table-column label="处方名称" align="center" prop="preName"/>
      <el-table-column label="剂数" align="center" prop="number"/>
      <el-table-column label="审核人" align="center" prop="checkBy"/>
      <el-table-column label="审核时间" align="center" prop="checkTime"/>
      <el-table-column label="调配状态" align="center" width="78" prop="isAllocation">
        <template slot-scope="scope">
          {{ isAllocationRefData[ scope.row.isAllocation ] || '' }}
        </template>
      </el-table-column>
      <el-table-column label="调配师" align="center" prop="allocateNickName"/>
      <el-table-column label="调配时间" align="center" prop="allocateTime"/>
      <el-table-column label="置顶时间" align="center" prop="topTime"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            style="width: 40px"
            @click="handleUpdate(1, scope.row)"
            v-hasPermi="['rescription:prescriptionCore:query']"
          >详情
          </el-button
          >
          <el-button
            size="mini"
            type="primary"
            style="width: 40px;background-color: #1ab394;"
            v-if="scope.row.isAllocation!=1"
            @click="handleTop(scope.row)"
            v-hasPermi="['rescription:prescriptionCore:top']"
          >置顶
          </el-button>
          <el-button
            size="mini"
            type="primary"
            style="width: 80px;background: #409eff;"
            v-if="scope.row.isAllocation!=1"
            @click="handleAssign(scope.row)"
            v-hasPermi="['rescription:prescriptionCore:allocate']"
          >指定调配师
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 详情对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1280px"
      append-to-body
    >
      <div>
        <div style="display: flex; align-items: center;margin-bottom:20px;">
          <h3>处方信息</h3>
          <div style="margin: 0 0 0 30px">处方号：{{ detailData.preNo }}</div>
        </div>
        <div style="display: flex; align-items: center;">
          <h3>调配状态：</h3>
          <span style="color:#fff"
                :class="{'yellow-btn':detailData.isAllocation==0,'green-btn':detailData.isAllocation==1}">
            {{ isAllocationRefData[ detailData.isAllocation ] || '' }}
          </span>
          <template v-if="detailData.isAllocation==1">
            <h3 style="margin-left:50px">调配师：</h3>
            <span>{{ detailData.allocateNickName }}</span>
            <h3 style="margin-left:50px">调配时间：</h3>
            <span>{{ detailData.allocateTime }}</span>
          </template>
          <template v-else-if="detailData.isTop==1">
            <h3 style="margin-left:50px">置顶时间：</h3>
            <span>{{ detailData.topTime }}</span>
          </template>
        </div>
        <el-divider></el-divider>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-descriptions title="" :column="5">
              <el-descriptions-item label="患者姓名">{{
                  detailData.name
                }}
              </el-descriptions-item>
              <el-descriptions-item label="性别">{{
                  detailData.sex
                }}
              </el-descriptions-item>
              <el-descriptions-item label="年龄">{{
                  detailData.age
                }}
              </el-descriptions-item>
              <el-descriptions-item label="科室">{{
                  detailData.department
                }}
              </el-descriptions-item>
              <el-descriptions-item label="临床诊断">
                {{ detailData.disName }}-{{ detailData.symName }}
              </el-descriptions-item>
              <el-descriptions-item label="开方医生">{{
                  detailData.doctor
                }}
              </el-descriptions-item>
              <el-descriptions-item label="处方类型">{{
                  detailData.type == 1 ? '中药处方' : '中药制剂'
                }}
              </el-descriptions-item>
              <el-descriptions-item label="发药状态">{{
                  detailData.fyState == 0 ? '未发药' : detailData.fyState == 1
                    ? '已发药'
                    : '取消发药'
                }}
              </el-descriptions-item>
              <el-descriptions-item label="煎药方式">{{
                  detailData.oralPre && detailData.oralPre.decoctingMethod
                }} <span v-if="detailData.oralPre && detailData.oralPre.decoctingMethod=='浓煎'">{{
                    detailData.oralPre && detailData.oralPre.concentration
                  }}</span></el-descriptions-item>
              <el-descriptions-item label="处方金额">{{
                  detailData.type == 1
                    ? detailData.oralPre &&
                    detailData.oralPre.prescriptionSum
                    : detailData.solidPre &&
                    detailData.solidPre.prescriptionSum
                }}
              </el-descriptions-item>
              <el-descriptions-item label="剂型">{{
                  detailData.oralPre && detailData.oralPre.dosageForm
                }}
              </el-descriptions-item>
              <el-descriptions-item label="剂数">{{
                  detailData.oralPre && detailData.oralPre.number
                }}
              </el-descriptions-item>
              <el-descriptions-item label="代煎剂数">{{
                  detailData.oralPre && detailData.oralPre.daijianNumber
                }}
              </el-descriptions-item>
              <el-descriptions-item label="代煎费用">{{
                  detailData.oralPre && detailData.oralPre.daijianCost
                }}
              </el-descriptions-item>
              <el-descriptions-item label="处方用法">{{
                  detailData.oralPre && detailData.oralPre.prescriptionusage
                }}
              </el-descriptions-item>
              <el-descriptions-item label="服药时间">{{
                  detailData.oralPre && detailData.oralPre.medicationTime
                }}
              </el-descriptions-item>
              <el-descriptions-item label="频次">{{
                  detailData.oralPre && detailData.oralPre.frequency
                }}
              </el-descriptions-item>
              <el-descriptions-item label="快递单号">{{
                  detailData.expressCode
                }}
              </el-descriptions-item>
              <el-descriptions-item label="收件人">{{
                  detailData.consignee
                }}
              </el-descriptions-item>
              <el-descriptions-item label="收货电话">{{
                  detailData.contactNumber
                }}
              </el-descriptions-item>
              <el-descriptions-item label="配送费用">{{
                  detailData.oralPre && detailData.oralPre.distributionCost
                }}
              </el-descriptions-item>
              <el-descriptions-item label="总金额">{{
                  detailData.oralPre && detailData.oralPre.prescriptionTotleSum
                }}
              </el-descriptions-item>
            </el-descriptions>
            <el-descriptions>
              <el-descriptions-item label="嘱托">{{
                  detailData.oralPre && detailData.oralPre.entrust
                }}
              </el-descriptions-item>
            </el-descriptions>
            <el-descriptions>
              <el-descriptions-item label="收货地址">{{
                  detailData.address
                }}
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
          <el-col :span="24">
            <el-table
              v-loading="loading"
              :data="detailData && detailData.oralPreItemList"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="index" width="55" align="center"/>
              <el-table-column label="药品名称" align="center" prop="matName"/>
              <el-table-column
                label="药品规格"
                align="center"
                prop="ypggCenter"
              />
              <el-table-column label="单位" align="center" prop="matUnitName"/>
              <el-table-column label="用法" align="center" prop="matUsageName"/>
              <el-table-column label="剂量" align="center" prop="matDose"/>
              <el-table-column label="零售价" align="center" prop="matXsj"/>
              <el-table-column label="产地" align="center" prop="matOrigin"/>
              <el-table-column
                label="小计"
                align="center"
                prop="subtotalMoney"
              />
            </el-table>
          </el-col>
        </el-row>
        <div style="margin-top: 20px" v-if="btnState === 2">
          <span>备注</span>
          <el-input
            placeholder="请输入拒绝原因"
            style="margin-top: 10px"
            v-model="remarks"
          ></el-input>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog title="在线调配师"
               :title="title"
               :visible.sync="openDispatcher"
               width="500px"
               append-to-body
    >
      <el-table
        class="dispatcher-table-wrapper"
        ref="dispatcherTable"
        :data="listDispatcherArr"
        :max-height="maxHeight"
        highlight-selection-row
        @row-click="handleDispatcherChange"
        @select="handleDispatcherChange"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="50">
        </el-table-column>
        <el-table-column
          property="name"
          label="姓名"
          width="120">
        </el-table-column>
        <el-table-column
          property="jobNumber"
          label="工号"
          width="120">
        </el-table-column>
        <el-table-column
          property="status"
          label="接单状态">
          <template slot-scope="scope">
            {{
              scope.row.onlineStatus == 1
                ? '不在线'
                : scope.row.workStatus == 1
                  ? '暂停接单'
                  : '接单中'
            }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm(1)">指定</el-button>
        <el-button type="primary" @click="confirm(2)">指定并置顶</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getPrescriptionCore } from '@/api/prescription/prescriptionCore';
import { listMedicalMechanism } from '@/api/medical/mechanism';
import { assignAndPinOrder, assignOrder, listAllocateCore, listDispatcher, pinOrder } from '@/api/pharmacy/blend';
import dayjs from 'dayjs'
export default {
  name: 'BlendCore',
  components: {},
  data() {
    const now = dayjs().format('YYYY-MM-DD');
    return {
      maxHeight: Math.floor(window.innerHeight * 0.6),
      $baseUrl: this.$baseUrl,
      deployStateArr: [
        {name: '待调配', id: 0},
        {name: '已调配', id: 1},
      ],
      isAllocationRefData: {
        0: '待调配',
        1: '已调配',
      },
      remarks: '',
      btnState: 1,
      rowId: null,
      detailData: {
        oralPreItemList: [],
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方调配表格数据
      allocateAllocate: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openDispatcher: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isAllocation: null,
        appId: null,
        yljgId: null,
        timeStamp: null,
        token: null,
        // 患者姓名/处方号
        reqStr: null,
        preNo: null,
        preName: null,
        name: null,
        sex: null,
        age: null,
        department: null,
        doctor: null,
        // 调配师 Id
        pharmacistUserId: null,
        allocateNickName: null,
        checkTime: [now, now],
        disCode: null,
        disName: null,
        symCode: null,
        symName: null,
        westernDisease: null,
        westernCode: null,
        patientContent: null,
        nowDesc: null,
        pastDesc: null,
        fourDiagnosis: null,
        physical: null,
        auxiliaryExam: null,
        premzzy: null,
        visitNo: null,
        checkState: null,
        remarks: null,
        extendedTxt: null,
        address: null,
        contactNumber: null,
        consignee: null,
        takingMethod: null,
        yfId: window.localStorage.getItem('pharmacyId'),
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      listMedicalMechanismArr: [],
      // 调配师
      listDispatcherArr: [],
    };
  },
  created() {
    this.getList();
    this.getListMedicalMechanism();
    this.getListDispatcherArr();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        prescriptionId: null,
        pharmacistId: null,
      };
      this.remarks = null;
      this.resetForm('form');
    },
    /** 置顶 */
    handleTop(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确定置顶处方编号为"' + row.preNo + '"的数据项?',
        '温馨提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        },
      )
      .then(function () {
        return pinOrder({prescriptionId: ids});
      })
      .then(() => {
        this.getList();
        this.msgSuccess(res.msg || '置顶成功');
      })
      .catch(() => {
      });
    },
    handleAssign(row) {
      this.reset();
      this.form.prescriptionId = row.id || this.ids;
      this.getListDispatcherArr().then(() => { this.openDispatcher = true; });
    },
    /** 查询调配处方列表 */
    getList() {
      this.loading = true;
      const form = Object.assign({}, this.queryParams);
      if ( this.queryParams.checkTime && this.queryParams.checkTime.length ) {
        form.startTime = this.queryParams.checkTime[ 0 ];
        form.endTime = this.queryParams.checkTime[ 1 ];
      } else {
        form.startTime = '';
        form.endTime = '';
      }
      delete form.checkTime;
      listAllocateCore(form).then((response) => {
        this.allocateAllocate = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListMedicalMechanism() {
      listMedicalMechanism().then((res) => {
        this.listMedicalMechanismArr = res.data;
      });
    },
    /** 查询调配师列表 */
    getListDispatcherArr() {
      return listDispatcher().then((res) => {
        this.listDispatcherArr = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.openDispatcher = false;
      this.open = false;
      this.reset();
    },
    confirm(num) {
      if ( !this.form.pharmacistId ) {
        this.msgError('请选择调配师');
        return;
      }
      let request;
      if ( num == 1 ) {
        request = assignOrder(this.form);
      } else if ( num == 2 ) {
        request = assignAndPinOrder(this.form);
      }
      if ( request ) {
        request.then(res => {
          this.cancel();
          this.getList();
          this.msgSuccess(res.msg || '操作成功');
        }).catch();
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      const now = dayjs().format('YYYY-MM-DD');
      this.queryParams.checkTime = [now, now];
      this.handleQuery();
    },
    handleDispatcherChange(selection, a) {
      this.$refs.dispatcherTable.clearSelection();
      if ( Array.isArray(selection) ) {
        const item = selection.pop();
        this.form.pharmacistId = item ? item.id : null;
        this.$refs.dispatcherTable.toggleRowSelection(item);
      } else {
        this.form.pharmacistId = selection ? selection.id : null;
        this.$refs.dispatcherTable.toggleRowSelection(selection);
      }
    },
    // 多选框选中数据
    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(num, row) {
      this.reset();
      const id = row.id || this.ids;
      this.btnState = num;
      this.rowId = id;
      getPrescriptionCore(id).then((response) => {
        /* 合并数据 */
        for ( const key in response.data ) {
          if ( response.data[ key ] == null ) {
            response.data[ key ] = row[ key ];
          }
        }

        this.detailData = response.data;

        this.open = true;
        this.title = '查看处方调配';
      });
    },
  },
};
</script>
<style scoped>
h3 {
  margin: 0;
}

.defwidth {
  width: 160px;
}

.prescriptionCore >>> .warning-row, .prescriptionCore >>> .el-table__body tr.warning-row.current-row > td, .prescriptionCore >>> .el-table__body tr.warning-row.hover-row > td {
  background: #e6a23c;
  color: #fff
}
</style>
<style>
.dispatcher-table-wrapper th.el-table-column--selection .cell {
  display: none;
}
</style>
