<template>
  <!-- 查询统建处方 -->
  <div class="prescription">
    <!-- 搜索条件 -->
    <div class="top-filter">
      <el-form label-position="right" label-width="70px" :model="form" inline>
        <el-form-item label="方名">
          <el-input v-model="form.name" size="small" placeholder="请输入"></el-input>
        </el-form-item>
        <!--<el-form-item label="共享状态" v-if="activeName === '2'">
          <el-select v-model="form.share" placeholder="请选择" size="small">
            &lt;!&ndash; <el-option
              :label="item.name"
              :value="item.pid"
              v-for="(item,index) in shareSelect"
              :key="index"
            ></el-option>&ndash;&gt;
            <el-option label="个人" :value="0"></el-option>
            <el-option label="二级科室" :value="1"></el-option>
            <el-option label="科室" :value="2"></el-option>
            <el-option label="本院" :value="3"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="医派">
          <el-select v-model="form.sectCategory" @change="getHospitalDepartment(form.sectCategory)"
                     placeholder="请选择" size="small" clearable>
            <el-option v-for="item in hospitalCategory" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室">
          <el-select v-model="form.sectDept"
                     placeholder="请选择" size="small" clearable>
            <el-option v-for="item in hospitalDepartment" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-button size="small" type="primary" @click="searchDis">搜索</el-button>
      <el-button size="small" type="warning" @click="clearDis">清空</el-button>
    </div>
    <!-- 表格数据展示 -->
    <div class="table">
      <!-- 统建处方(协定方)表格 -->
      <el-table :data="tableData1" border style="width: 100%" v-if="activeName === '2'">
        <el-table-column prop="sectCategory" label="医派" align="center" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="sectDept" label="科室" align="center" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" label="方名" align="center" show-overflow-tooltip></el-table-column>
        <!--<el-table-column prop="chinesesymptom" label="是否可修改" align="center" width="95">
          <template slot-scope="scope">
            <div>{{ scope.row.type==0?'是':'否' }}</div>
          </template>
        </el-table-column>-->
        <el-table-column prop="effect" label="功效与适用症" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="drugInformation" label="中药信息" align="center" show-overflow-tooltip></el-table-column>
        <!--<el-table-column prop="book" label="共享状态" align="center" width="100">
          <template slot-scope="scope">
            <div>{{ scope.row.showType |filterShowType}}</div>
          </template>
        </el-table-column>-->
        <el-table-column prop="provenance" label="处方出处" align="center" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column prop="address" label="操作" width="80" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleAgree(scope)">转方</el-button>
            <!-- <el-button size="mini" type="warning" @click="viewCaseDetail(scope)">合方</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页数据 -->
    <div class="paging">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="10"
        :total="total"
        background
        layout=" prev, pager, next, jumper"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { getAccordShareList } from "@/api/business.js";
import { getEffectQuery, getPrescriptionsList } from "@/api/knowledge.js";
import { getAgreeRecipe, getAccordDetail } from "@/api/diagnosis.js";
import { getHospitalCategory, getHospitalDepartment } from "@/api/city";

export default {
  data() {
    return {
      hospitalCategory: [],
      hospitalDepartment: [],

      activeName: "2",
      form: {
        name: "", // 方名
        type: "", // 类型
        share: "", // 共享状态
        purposeType: "1", // 用途类型 1 八病九方

        sectCategory: '', // 医派
        sectDept: '', // 科室
      },
      shareSelect: [],
      types: [],
      tableData: [],
      tableData1: [],
      currentPage: 1,
      total: 0
    };
  },
  created() {
    this.getAccordShareList();
    this.getEffectQuery();
    // this.getPrescriptionsList();
    // this.getAgreeRecipe();
    this.clearDis();
  },
  filters: {
    filterShowType(val) {
      if (val == 0) return "个人";
      if (val == 1) return "二级科室";
      if (val == 2) return "科室";
      if (val == 3) return "本院";
    }
  },
  methods: {
    async getHospitalCategory() {
      this.hospitalCategory = await getHospitalCategory();
    },
    async getHospitalDepartment() {
      this.hospitalDepartment = await getHospitalDepartment({hospital: this.form.sectCategory || ''});

      if (!this.hospitalDepartment.find(item => item.id === this.form.sectDept)) this.form.sectDept = '';
    },
    clearDis() {
      this.currentPage = 1;

      this.form = {
        sectCategory: '', // 医派
        sectDept: '', // 科室
        purposeType: "1", // 用途类型 1 八 病九方
        name: "", // 方名
        type: "", // 类型
        share: "" // 共享状态
      };

      this.getHospitalCategory().then(() => this.getHospitalDepartment());
      if (this.activeName === '2') {
        this.getAgreeRecipe();
      }
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      if (this.activeName === '2') {
        this.getAgreeRecipe();
      }
    },
    searchDis() {
      this.currentPage = 1;
      if (this.activeName === '2') {
        this.getAgreeRecipe();
      }
    },
    handleClick() {
      this.currentPage = 1;

      this.form = {
        name: "", // 方名
        type: "", // 类型
        share: "", // 共享状态
        purposeType: "1", // 用途类型 1 八病九方
      };
      if (this.activeName === '2') {
        this.getAgreeRecipe();
      }
    },
    //   方剂换方/合方
    handleFj(scope, type) {
      let item = scope.row;
      if (type == 1) {
        // 换
        this.$parent.$parent.$parent.inferChange(item);
      } else {
        // 合
        this.$parent.$parent.$parent.inferChange1(item);
      }
    },
    // 协定方转方
    handleAgree(scope) {
      this.getAccordDetail(scope.row.preId);
    },
    // 获取共享至信息
    async getAccordShareList() {
      let res = await getAccordShareList();
      if (res.ResultCode == 0) {
        this.shareSelect = res.Data;
      }
    },
    // 获取方剂分类
    async getEffectQuery() {
      let res = await getEffectQuery({
        effecttype: "1",
        effictId: ""
      });

      if (res.code == 0) {
        this.types = res.data.effects;
      }
    },
    // 获取方剂列表
    async getPrescriptionsList() {
      let params = {
        pageid: this.currentPage,
        pagesize: 10,
        searchtype: "",
        keyword: this.form.name,
        effected: this.form.type
      };
      let res = await getPrescriptionsList(params);
      if (res.code == 0) {
        this.total = res.data.TotalRecordCount;
        this.tableData = res.data.pres;
      }
    },
    //获取右侧协定方列表
    async getAgreeRecipe() {
      let params = {
        type: this.form.share,
        purposeType: this.form.purposeType,
        sectCategory: this.form.sectCategory,
        sectDept: this.form.sectDept,

        name: this.form.name,
        pageId: this.currentPage,
        pageSize: 10
      };
      let res = await getAgreeRecipe(params);
      if (res.ResultCode == 0) {
        this.total = res.Data.TotalRecordCount;
        this.tableData1 = res.Data.Items;
      }
    },
    //   获取协定方详情
    async getAccordDetail(id) {
      let res = await getAccordDetail({
        pid: id
      });
      if (res.ResultCode == 0) {
        // this.$parent.$parent.$parent.agreeInfo = res.Data;
        // this.$parent.$parent.$parent.turnRecipe2(res.Data);
        const recipe = { ...res.Data, medicine: res.Data.preStiDetails, __RecipeFrom__: '2' }
        this.$parent.$parent.$parent.transposition(recipe);
        if (res.Data.diseaseid) {
          this.$parent.$parent.$parent.$refs.TCM.setParams({
            namemedicine: res.Data.diseasename,
            disid: res.Data.diseaseid,
            symptomid: res.Data.symptomid,
            syndrometypes: res.Data.symptomname,
            treatment: res.Data.treatment
          });

          setTimeout(() => {
            this.$parent.$parent.$parent.tcmClick(false);
          }, 200);
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.top-filter {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-bottom: 10px;
}

.top-filter {
  ::v-deep {
    .el-input__inner {
      width: 150px;
    }

    .el-form--inline .el-form-item {
      margin-bottom: 0;
    }
  }
}

.paging {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}
</style>
    