<template>
  <div style="display: flex; ">
    <div style="width:230px;">
      <div style="display: flex;">
        <span>项目类别</span>
        <el-cascader :options="dict.itemTypeTree" v-model="itemTypeIds" style="margin-left: 3px;width:160px;"
          :props="{ checkStrictly: true, expandTrigger: 'hover', ...customerOrg.treeprops, }" :show-all-levels="false"
          clearable filterable @change="parseAsbitem" size="small">
        </el-cascader>
      </div>
      <div class="mainareaBox">
        <el-table :data="asbItems" :height="400" @row-dblclick="dbClickChoosedAsb"
          @selection-change="handleSelectionChange" size="small">
          <el-table-column type="selection" align="center" width="60" />
          <el-table-column prop="displayName" label="未选组合项目" width="160" />
        </el-table>
      </div>
    </div>

    <div style="width:100px;margin-top: 70px;">
      <div class="btnList">
        <el-button type="primary" @click="addAbs(asbItemChoosed)" style="width:90px;">添加 <i class="el-icon-right"></i>
        </el-button>
      </div>
      <div class="btnList">
        <el-button type="primary" @click="delAbs(asbItemOCXChoosed)" style="width:90px;">移除 <i class="el-icon-back"></i>
        </el-button>
      </div>
    </div>

    <div style="display: block; width:230px;">

      <div class="disTotal">
        <el-select v-model="asbItemId" placeholder="快速选择组合项目" size="small" filterable clearable remote automatic-dropdown
          :remote-method="remoteMethod" @change="quickChoosedAsb" default-first-option ref="asbItemId"
          style="width:150px;text-align: left;padding-right: 15px;">
          <el-option v-for="item in quickAsb" :key="item.id" :value="item.id" :label="item.displayName" />
        </el-select>
      </div>

      <div class="mainareaBox">
        <el-table :data="report.dataAsbitemOCX" height="400" width="100%" @row-dblclick="removeAbs"
          @selection-change="selecteditems" size="small">
          <!-- temporaryselection   personnelUnit.nogroupselected-->
          <el-table-column type="selection" align="center" width="60" />
          <el-table-column label="已选组合项目" width="160" prop="displayName" />
        </el-table>
      </div>
    </div>

    <div style="width:100px;margin-top: 70px;">
      <div class="btnList">
        <el-button type="primary" @click="medicalPackageVisble = true" style="width:90px;">复制套餐</el-button>
      </div>
      <div class="btnList">
        <el-button type="primary" @click="btnOk('close')" style="width:90px;">确定 <i class="el-icon-check"></i>
        </el-button>
      </div>
      <div class="btnList"
        style="text-align: center;background-color: rgb(192, 192, 192);border-radius: 5%;padding: 5px 0;">
        已选组合项<br>
        目为空时即<br>
        表示选择所<br>
        有组合项自
      </div>
    </div>

    <!--复制套餐-->
    <el-dialog title="体检套餐" :visible.sync="medicalPackageVisble" width="600px" height="400"
      :show-close="false" :close-on-click-modal="false" :append-to-body="true">
      <el-table :data="dict.medicalPackage" border height="350" row-key="id" size="small"
        highlight-current-row @row-click="rowClick" @row-dblclick="rowDblclick" ref="patientList" >
        
        <!-- "displayName": "健康体检套餐",
        "price": 20.00,
        "forSexId": "0",
        "isActive": "Y",
        "remark": "2",
        "displayOrder": 1,
        "simpleCode": "JKTJTC",
        "creatorName": "admin",
        "lastModifierName": "users",
        "lastModificationTime": "2023-08-04T16:34:02.94408",
        "lastModifierId": "3a0ca83b-3116-da05-3916-dfd4c0c548cb",
        "creationTime": "2023-07-10T19:53:30.210553",
        "creatorId": "3a0c4180-107c-0c89-b25b-0bd34666dcec",
        "id": "3a0c51d3-2345-38df-ba0b-1862a3c3606f" -->
        <el-table-column type="index" width="50" />
        <el-table-column prop="displayName" label="套餐名称" width="180"/>
        <el-table-column prop="price" label="价格" width="80"/>
        <el-table-column prop="forSexId" label="适用性别" width="80">
          <template slot-scope="scope">
            <div>
              {{ dddw(dict.forSex, "id", scope.row.forSexId, "displayName") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="100"/>
        <el-table-column prop="creatorName" label="创建人员" width="80"/>
        <el-table-column prop="creationTime" label="创建日期" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.creationTime">
              {{ moment(scope.row.creationTime).format("yyyy-MM-DD") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="lastModifierName" label="修改人员" width="80"/>
        <el-table-column prop="lastModificationTime" label="修改日期" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.lastModificationTime">
              {{ moment(scope.row.lastModificationTime).format("yyyy-MM-DD") }}
            </div>
          </template>
        </el-table-column>        
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="medicalPackageVisble = false" style="width:90px;">取消</el-button>
        <el-button type="primary" @click="copyMedicalPackage" style="width:90px;">确定</el-button>
      </span>
    </el-dialog>
    
  </div>
</template>
<script>

import { mapState, mapActions } from "vuex";
import moment from "moment";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { dddw,arrayFilter, arrayReduce, arrayExistObj, deepCopy } from "../../utlis/proFunc";


export default {
  components: {
    
  },
  props: ["patientRegisterForm", "payTypeFlag"],
  data() {
    return {
      itemTypeIds: [], //项目类别

      asbItems: [],     //可被选的 asbItem
      asbItemChoosed: [], //勾选的  未选组合项目

      asbItemOCXChoosed: [], //勾选的  已选组合项目

      asbItemQuick: [],  //可供 快速选择的组合项目
      quickAsb: [],      //快速选择当前显的下拉数据
      asbItemId: '',     //快速选择的组合项目ID

      medicalPackageVisble:false,
      curPackageId:'',  //当前选中套餐ID
      packageAsbitems:[], //套餐中包含的组合项目

    };
  },
  computed: {
    ...mapState(["dict", "customerOrg", "patientRegister", "personnelUnit", "report"]),
  },

  created() {

  },

  mounted() {
    this.dictInit();
  },

  methods: {
    moment,dddw,

    dictInit() {

      //获取所有组合项目
      getapi("/api/app/asbitem/in-filter?Filter").then((res) => {
        if (res.code == 1) {
          this.dict.asbItemAll = res.data.items;
          this.parseAsbitem();
        }
      });

      //套餐
      getapi("/api/app/medical-package/in-filter").then((res) => {
        if (res.code == 1) {
          this.dict.medicalPackage = res.data.items;
        }
      });

    },

    //分析可选组合项目，及快速可选组合项目的数据
    parseAsbitem() {
      this.asbItemQuick = deepCopy(this.dict.asbItemAll);
      arrayReduce(this.asbItemQuick, this.report.dataAsbitemOCX, 'id');
      this.asbItems = deepCopy(this.asbItemQuick);
      if (this.itemTypeIds && this.itemTypeIds.length > 0) {
        this.asbItems = arrayFilter(this.asbItems, 'itemTypeId', this.itemTypeIds[this.itemTypeIds.length - 1])
      }
    },



    //未选组合项目 勾选情况
    handleSelectionChange(val) {
      // this.selecteddata = val;
      this.asbItemChoosed = val;
      //console.log(this.asbItemChoosed);
    },

    // 添加组合项目
    ///api/app/register-asbitem/many/3fa85f64-5717-4562-b3fc-2c963f66afa6'
    addAbs(asbItemChoosed) {

      if (asbItemChoosed.length < 1) {
        alert("请选择要添加的组合项目")
        return
      }
      arrayReduce(this.asbItems, asbItemChoosed, 'id')
      arrayReduce(this.asbItemQuick, asbItemChoosed, 'id')
      this.report.dataAsbitemOCX = this.report.dataAsbitemOCX.concat(asbItemChoosed)

      this.btnOk();
    },

    //人员已选组合项目 勾选情况
    selecteditems(val) {
      this.asbItemOCXChoosed = val
    },

    //删除 人员已选中的组合项目
    ///api/app/register-asbitem/many?RegisterAsbitemIds=3fa85f64-5717-4562-b3fc-2c963f66afa6
    delAbs(absForDel) {
      if (absForDel.length < 1) {
        alert("请选择要移除的组合项目")
        return
      }
      arrayReduce(this.report.dataAsbitemOCX, absForDel, 'id');
      this.asbItems = this.asbItems.concat(absForDel)
      this.asbItemQuick = this.asbItemQuick.concat(absForDel)
      this.btnOk();
    },

    //双击删除已选项目
    removeAbs(row) {
      this.delAbs([row]);
    },


    //双击选择组合项目
    dbClickChoosedAsb(row) {
      this.addAbs([row]);
    },

    //快速选择组合项目
    quickChoosedAsb(v) {
      //远程查询时，设置了 value-key 也不管用，只能取到value console.log('quickChoosedAsb',v)
      let lfind = -1
      if (v) {
        lfind = arrayExistObj(this.asbItemQuick, 'id', v)
        if (lfind > -1) {
          this.addAbs([this.asbItemQuick[lfind]])
        }
      }
    },

    //快速选择组合项目时，调整可按拼间简码及简称查找
    remoteMethod(keyWords) {
      //console.log('remoteMethod',this.dict.asbItemQuick)  
      if (keyWords) {
        this.quickAsb = [];
        this.asbItemQuick.forEach(item => {
          if (item.displayName.toLowerCase().indexOf(keyWords.toLowerCase()) > - 1
            || item.simpleCode.toLowerCase().indexOf(keyWords.toLowerCase()) > - 1
            || item.shortName.toLowerCase().indexOf(keyWords.toLowerCase()) > - 1) {
            this.quickAsb.push(item);
          }
        });
      } else {
        this.quickAsb = deepCopy(this.asbItemQuick);
      }
    },

    //选中套餐
    async rowClick(row){
      this.curPackageId = row.id
      this.packageAsbitems = []
      try {
        let res = await getapi(`/api/app/medical-package-detail/medical-package-in-asbitem?MedicalPackageId=${row.id}`)
        this.packageAsbitems = res.data
      } catch (error) {
        console.log(error)
      }         
    },

    //双击选套餐，并确认复制
    async rowDblclick(row){
      await this.rowClick(row)
      this.copyMedicalPackage()
    },

    //复制套餐(确认选中套餐)
    copyMedicalPackage(){
      if(!this.curPackageId){
        this.$message.warning("请先选择要复制的套餐！")
        return
      }
      console.log('this.packageAsbitems',this.packageAsbitems)
      this.addAbs(this.packageAsbitems)
      this.medicalPackageVisble = false
    },

    btnOk(closed){
      if(this.report.dataAsbitemOCX && this.report.dataAsbitemOCX.length > 0){
        switch (this.report.dataAsbitemOCX.length) {
          case 1:
            this.patientRegister.query.asbitemOCX = this.report.dataAsbitemOCX[0].displayName
            break;        
          default:
            this.patientRegister.query.asbitemOCX = this.report.dataAsbitemOCX[0].displayName + '，… + ' + this.report.dataAsbitemOCX.length
            break;
        }
      }else{
        this.patientRegister.query.asbitemOCX = ''
      }

      if(closed){
        this.report.dialogAsbitemOCX = false
      }
    }
    
  },

  //监听事件
  watch: {

  },
};
</script>
<style lang="scss" scoped>
@import "../../assets/css/global.css";

.mainareaBox {
  border: 1px solid #000;
  height: 400px;
  margin-top: 5px;
}

.btnList {
  margin-top: 5px;
  margin-left: 5px;
  margin-right: 5px;
}

.disTotal {
  margin-left: 10px;
}

::v-deep .el-input__inner {
  /*text-align: center;*/
  padding-left: 2px;
  padding-right: 15px;
}

::v-deep .el-input__icon {
  width: 15px;
  /* 输入框下拉箭头或清除图标 默认 25 */
}

/* 表格 表头 列间距控制 */
::v-deep .el-table th.el-table__cell>.cell {
  text-align: center;
  /* 表格 表头 对齐方式，默认 left */
  padding-right: 2px;
  /* 表格 表头 列间距控制，默认 10px */
  padding-left: 2px;
  /* 表格 表头 列间距控制，默认 10px */
}

/* 表格 表体 首列左间距控制 */
::v-deep .el-table--border .el-table__cell:first-child .cell {
  padding-left: 2px;
  /* 表格 表体 首列左间距控制，默认 10px */
}

/* 表格 行列间距控制 ，默认 10 px*/
::v-deep .el-table__cell {
  padding: 2px 2px 2px 2px;
  /* 上，右，下，左 */
}


::v-deep .cell {
  padding-left: 2px;
  padding-right: 2px;
}

::v-deep .el-icon-date:before {
  content: ""
}</style>