<template>
  <div class="druglist">
    <!-- 顶部筛选 -->
    <div class="screening">
      <div class="screening-title flex-vertical-center-l">
        <img src="~@/assets/filters.png" alt />
      </div>
      <div class="screening-form flex-vertical-center-l flex-wrap">
        <div class="screening-item flex-vertical-center-l">
          <span>方名：</span>
          <div class="input">
            <el-input size="mini" v-model="form.name" placeholder="请输入"></el-input>
          </div>
        </div>

        <div class="screening-item flex-vertical-center-l">
          <span>功效和适应症：</span>
          <div class="input">
            <el-input size="mini" v-model="form.efficacy" placeholder="请输入"></el-input>
          </div>
        </div>
        <div class="screening-item flex-vertical-center-l">
          <span>是否可修改事项：</span>
          <div class="input">
            <el-select size="mini" placeholder="请选择" v-model="form.type">
              <el-option label="是" value="0"></el-option>
              <el-option label="否" value="1"></el-option>
            </el-select>
          </div>
        </div>
        <div class="screening-item flex-vertical-center-l">
          <span>是否统建处方：</span>
          <div class="input">
            <el-select size="mini" placeholder="请选择" v-model="form.purposeType" clearable>
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </div>
        </div>

        <el-button type="primary" size="mini" @click="search()">搜索</el-button>
        <el-button type="warning" size="mini" @click="clearFilter()">清空</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="$router.push({path:'/index/accordedit'})"
          v-if="showAdd"
        >新增</el-button>
      </div>
    </div>

    <!-- 底部表格数据 -->
    <div class="table">
      <!-- <div class="flex-vertical-center-l table-btns"> -->
      <!-- <div class="flex-center" @click="$router.push({path:'/index/accordedit'})" v-if="showAdd">新增</div> -->
      <!-- <div class="flex-center bg-yellow">导入</div>
      <div class="flex-center bg-red">自动映射</div>-->
      <!-- </div> -->

      <div class="today-table">
        <div class="table-container">
          <el-table :data="tableData" stripe style="width: 100%" border height="100%">
            <el-table-column prop="id" label="序号" width="50" align="center"></el-table-column>
            <el-table-column prop="effect" label="功效和适应症" align="center" width="180"></el-table-column>
            <el-table-column prop="name" label="方名" align="center" width="120"></el-table-column>
            <el-table-column prop="createName" label="创建者" align="center" width="120"></el-table-column>
            <el-table-column prop="drugName" label="药品信息" align="center"></el-table-column>
            <el-table-column prop="provenance" label="处方出处" align="center" width="180"></el-table-column>
            <el-table-column prop="state" label="共享状态" width="100" align="center">
              <template slot-scope="scope">
                <div>{{scope.row.rangState==0?'个人':scope.row.rangState==1?'二级科室':scope.row.rangState==2?'科室':'医疗机构'}}</div>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="是否可修改事项" width="120" align="center">
              <template slot-scope="scope">
                <div>{{scope.row.type==0?'是':'否'}}</div>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="统建处方" width="100" align="center">
              <template slot-scope="scope">
                <div>{{scope.row.purposeType === '1' ? '是' : '否'}}</div>
              </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="200">
              <template slot-scope="scope">
                <div class="flex-center operation">
                  <div class="flex-center" v-if="showEdit" @click="editData(scope)">编辑</div>
                  <div class="flex-center bg-yellow" v-if="showDel" @click="deleteData(scope)">删除</div>

                  <div
                    class="flex-center bg-red"
                    v-if="showFind"
                    @click="$router.push({path:`/index/accordedit?pid=${scope.row.pid}&type=find`})"
                  >查看</div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="flex-vertical-center-r today-page">
          <el-pagination
            background
            layout=" prev, pager, next, jumper, total"
            :total="total"
            :page-size="limit"
            @current-change="sizeC($event)"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getAccordList, deleteAccord, getRecipeBR } from "@/api/business.js";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      form: {
        efficacy: "", // 功效
        name: "",
        purposeType: "", // 用途类型 1 八病九方
        type: "" // 类别
      },
      page: 1,
      limit: 10,
      total: 0,

      tableData: [],
      showAdd: true,
      showEdit: true,
      showDel: true,
      showFind: true
    };
  },
  created() {
    this.getAccordList();
    this.getRecipeBR();

    // console.log(this.getuserinfo);
  },
  methods: {
    deleteData(scope) {
      this.$confirm("确定要删除当前数据吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteAccord(scope.row.pid);
        })
        .catch(() => {});
    },
    clearFilter() {
      this.form = {
        efficacy: "", // 功效
        name: "",
        type: "" // 类别
      };
      this.getAccordList();
    },
    search() {
      this.page = 1;
      this.getAccordList();
    },
    sizeC(e) {
      console.log(e, "1");
      this.page = e;
      this.getAccordList();
    },
    editData(scope) {
      if (scope.row.createUser != this.getuserinfo.pid) {
        this.$message({
          message: "当前登录账号不是该协定方创建者,无法编辑",
          showClose: true,
          type: "warning"
        });
        return;
      }

      this.$router.push({
        path: `/index/accordedit?pid=${scope.row.pid}&type=edit`
      });
    },
    // 获取协定方列表
    async getAccordList() {
      let params = {
        effect: this.form.efficacy,
        name: this.form.name,
        type: this.form.type,
        page: this.page,
        limit: this.limit
      };
      if (this.form.purposeType) params.purposeType = this.form.purposeType;
      let res = await getAccordList(params);
      if (res.ResultCode == 0) {
        this.tableData = res.Data.Items;
        this.total = res.Data.TotalRecordCount;
        this.tableData.filter((item, index) => {
          return (item.id = index + 1);
        });

        this.showEdit = res.Data.ButtonRight.indexOf("edit") != -1;
        this.showDel = res.Data.ButtonRight.indexOf("del") != -1;
        this.showFind = res.Data.ButtonRight.indexOf("view") != -1;
      }
    },
    //删除协定方
    async deleteAccord(id) {
      let arr = [];

      arr.push(id);

      let params = {
        ids: arr.length == 1 ? arr[0] + "," : arr.join(",")
      };
      const loading = this.$loading({
        lock: true,
        text: "正在删除...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await deleteAccord(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();

        this.$message({
          showClose: true,
          message: "删除成功",
          type: "success"
        });

        this.getAccordList();
        // this.showDialog = false
      }
    },
    // 获取协定方权限
    async getRecipeBR() {
      let res = await getRecipeBR();
      if (res.ResultCode == 0) {
        this.showAdd = res.Data.indexOf("add") != -1;
      }
    }
  },
  computed: {
    ...mapGetters(["getuserinfo"])
  }
};
</script>
<style lang="scss" scoped>
@import "../../style/common.scss";
@import "../../style/base.scss";

.table {
  padding: 5px 5px;
  background: #ffffff;
  border-radius: 5px;
  margin-top: 10px;
  height: 72vh;

  .table-btns {
    margin-bottom: 13px;

    div {
      width: 74px;
      height: 36px;
      background: #5386f6;
      border-radius: 4px;
      cursor: pointer;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #ffffff;
      margin-right: 7px;
    }

    .bg-yellow {
      background: #ffae45;
    }

    .bg-red {
      background: #ff6245;
    }
  }

  .table-container {
    height: 90%;
  }

  .today-table {
    height: 100%;

    .operation {
      div {
        width: 60px;
        height: 24px;
        background: #5386f6;
        border-radius: 2px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
        cursor: pointer;
      }

      .bg-yellow {
        // width: 80px;
        height: 24px;
        background: #ffae45;
        margin-left: 21px;
        margin-right: 21px;
      }

      .bg-red {
        background: #ff6245;
      }
    }
  }
}

.table-body::v-deep .el-table .cell {
  text-align: center;
}
</style>

<style lang="scss" scoped>
@media screen and (min-width: 1681px) and (max-width: 1920px) {
  .table {
    height: 82vh;

    .table-container {
      height: 94%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 18px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 18px 0;
  }
}

@media screen and (min-width: 1601px) and (max-width: 1680px) {
  .table {
    height: 81vh;

    .table-container {
      height: 94%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 18px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 18px 0;
  }

  .today-table::v-deep .el-table td {
    padding: 18px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 18px 0;
  }
}

@media screen and (min-width: 1361px) and (max-width: 1600px) {
  .table {
    height: 73vh;

    .table-container {
      height: 90%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 5px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 5px 0;
  }
}

@media screen and(min-width:1281px) and (max-width: 1360px) {
  .table {
    height: 74vh;

    .table-container {
      height: 90%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 5px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 5px 0;
  }
}
</style>