<template>
  <div style="margin-top: -10px;">
    <div>
      <el-tabs v-model="tabChoosed" style="margin-top: 0px;">
        <el-tab-pane label="待发申请检查项目" name="1">
          <el-table :data="tableDataReq0" border style="width: 100%" row-key="id" height="450" highlight-current-row
            size="small" @selection-change="handleSelectionChange" :row-style="{ height: '25px' }">
            <el-table-column type="selection" align="center" width="40" />
            <el-table-column type="index" label="序号" align="center" width="40" />
            <el-table-column label="检查ID" min-width="320" prop="registerCheckId"></el-table-column>
            <el-table-column label="组合项目" min-width="150" prop="asbitemName"></el-table-column>
            <el-table-column label="申请号" min-width="120" prop="checkRequestNo"></el-table-column>
            <el-table-column prop="execFlag" label="状态" min-width="70" align="center" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="已发申请检查项目" name="2">
          <el-table :data="tableDataReq1" border style="width: 100%" row-key="id" height="450" highlight-current-row
            size="small" @row-click="rowClick" :row-style="{ height: '25px' }">
            <el-table-column type="index" label="序号" align="center" width="40" />
            <el-table-column label="检查ID" min-width="320" prop="registerCheckId"></el-table-column>
            <el-table-column label="组合项目" min-width="150" prop="asbitemName"></el-table-column>
            <el-table-column label="申请号" min-width="120" prop="checkRequestNo"></el-table-column>
            <el-table-column prop="execFlag" label="状态" min-width="70" align="center" />
            <el-table-column label="操作" width="50" align="center">
              <template slot-scope="scope">
                <el-dropdown>
                  <span>
                    <i class="el-icon-s-operation" style="font-size: 24px;color: blue;cursor:pointer;"></i>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="cancelRequest(scope.row)"
                      :disabled="setBtnDisabled(scope.row, 'cancel')">撤消申请</el-dropdown-item>
                    <!--
                    <el-dropdown-item @click.native="refreshRequest(scope.row)">刷新状态</el-dropdown-item>
                    <el-dropdown-item @click.native="chargeBack(scope.row)"
                      :disabled="setBtnDisabled(scope.row, 'chargeBack')">退费申请</el-dropdown-item>
                      -->
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 按钮区域 -->
    <div style="display: flex; margin-top: 10px; justify-content: space-between;">
      <div></div>
      <div style="display: flex;">
        <div style="margin-left: 10px">
          <el-button class="commonbutton" @click="btnRefresh">刷新</el-button>
        </div>
        <div style="margin-left: 10px" v-show="tabChoosed == '1'">
          <el-button class="commonbutton" @click="btnSubmit">检查申请</el-button>
        </div>
        <div style="margin-left: 10px">
          <el-button class="commonbutton" @click="dialogWin.ThirdPacsRequest = false">关闭</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { dddw, arrayExistObj, deepCopy } from "../../utlis/proFunc";

// import
export default {
  props: ["refParams"],
  data() {
    return {
      tabChoosed: '1',
      tableDataReq0: [],        //组合项目数据 过滤后显示的数据  
      selectedData: [],    //选中的组合项目
      tableDataReq1: [],   //检查申请单
      tableDataReq1Details: [],   //检查申请单明细
    };
  },

  created() {

  },

  mounted() {
    this.initData(this.refParams.patientRegisterId);
  },
  computed: {
    ...mapState(["dict", "dataTransOpts", "dialogWin"]),
  },
  methods: {

    initData(patientRegisterId) {
      this.tableDataReq0 = []
      this.tableDataReq1 = []
      this.tableDataReq1Details = []
      if (!patientRegisterId) return

      //待发申请 检查项目
      this.asbList(patientRegisterId)

      // 检查申请单
      this.requestList(patientRegisterId)
    },

    // 待检查项目
    asbList(patientRegisterId) {
      this.tableDataReq0 = []
      postapi('/api/app/RegisterCheck/GetPacsRequestListByPatientRegisterId', { patientRegisterId, execFlag: '0' })
        .then(res => {
          if (res.code > -1) {
            this.tableDataReq0 = res.data
          }
        })
    },

    // 检查申请单列表查询
    requestList(patientRegisterId) {
      this.tableDataReq1 = []
      postapi('/api/app/RegisterCheck/GetPacsRequestListByPatientRegisterId', { patientRegisterId, execFlag: '1' })
        .then(res => {
          if (res.code > -1) {
            this.tableDataReq1 = res.data
          }
        })
    },


    setChargeRequestFlag(chargeRequestFlag) {
      let ret = '检查申请'
      switch (chargeRequestFlag) {
        case '1':
          ret = '已检查'
          break;
        case '2':
          ret = '作废申请'
          break;
        case '3':
          ret = '已作废'
          break;
        case '4':
          ret = '退费申请'
          break;
        case '5':
          ret = '已退费'
          break;
        default:
          break;
      }
      return ret
    },

    handleSelectionChange(v) {
      this.selectedData = v;
    },

    setBtnDisabled(row, oprType) {
      let ret = true
      if (oprType == 'chargeBack') {
        if (row.chargeRequestFlag == '1') ret = false
      } else {
        if (row.chargeRequestFlag == '0') ret = false
      }
      return ret
    },

    // 获取申请单明细
    rowClick(row) {

    },

    // 刷新状态
    refreshRequest(row) {
      console.log('refreshRequest', row)
      postapi('/api/app/ChargeRequest/SyncChargeRequestFlagFromInterface', { chargeRequestId: row.id })
        .then(res => {
          if (res.code > -1) {
            this.btnRefresh()
          }
        })
    },


    // 撤消申请（作废申请）
    cancelRequest(row) {
      postapi('/api/app/RegisterCheck/CancelThirdPacsRequest', { registerCheckId: row.registerCheckId  })
        .then(res => {
          if (res.code > -1) {
            this.requestList(this.refParams.patientRegisterId)
          }
        })
    },

    // 退费申请
    chargeBack(row) {
      postapi('/api/app/ChargeRequest/RefundChargeRequest', { chargeRequestId: row.id })
        .then(res => {
          if (res.code > -1) {
            row.chargeRequestFlag = "4"
          }
        })
    },

    // 刷新
    btnRefresh() {
      if (this.tabChoosed == '1') {
        this.asbList(this.refParams.patientRegisterId)
      } else {
        this.requestList(this.refParams.patientRegisterId)
      }
    },

    // 发送申请
    async btnSubmit() {
      if (this.selectedData.length == 0) {
        this.$message.warning({ showClose: true, message: `请勾选要发送检查申请的检查项目！` });
        return;
      }

      for (let index = 0; index < this.selectedData.length; index++) {
        let e = this.selectedData[index];
        await postapi("/api/app/RegisterCheck/SendThirdPacsRequest", { registerCheckId: e.registerCheckId })
      }

      // 刷新
      this.asbList(this.refParams.patientRegisterId)

    },

  },

  //监听事件
  watch: {
    // 
    "refParams.brushTimes": {
      // immediate: true, // 立即执行
      // // deep: true, // 深度监听复杂类型内变化
      handler(newVal, oldVal) {
        console.log(`watch: 检查申请 newVal: ${newVal}, oldVal: ${oldVal}, 人员条码号: `, this.refParams.patientRegisterId)
        if (newVal != oldVal) this.initData(this.refParams.patientRegisterId)
      }
    },

    "tabChoosed": {
      // immediate: true, // 立即执行
      // // deep: true, // 深度监听复杂类型内变化
      handler(newVal, oldVal) {
        console.log(`watch: 检查申请 newVal: ${newVal}, oldVal: ${oldVal}, 人员条码号: `, this.refParams.patientRegisterId)
        if (newVal != oldVal) {
          if (newVal == '1') {
            this.asbList(this.refParams.patientRegisterId)
          } else {
            this.requestList(this.refParams.patientRegisterId)
          }
        }
      }
    },
  },
};
</script>
<style scoped>
@import "../../assets/css/global_input.css";
@import "../../assets/css/global_table.css";
@import "../../assets/css/global.css";
</style>
