<template>
  <basic-container>
    <div>
      <Card class="cardTitle">
        <p>
          <el-button type="primary" @click="backPage" style="width: 80px;"
            >返回</el-button
          >
        </p>
      </Card>
      <avue-crud
        style="width: 100%; margin-top: 15px"
        :data="tableData"
        :option="option1"
        :page.sync="page"
        :table-loading="tableLoading"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-change="searchChange"
      >
        <template v-if="typeVal === 'shoudong'" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="addData()"
            >新增</el-button
          >
        </template>
        <template slot="menu" slot-scope="{ row, index, size, type }">
          <el-button
            v-if="typeVal === 'shoudong' && row.id"
            class="del_clr_btn"
            @click="deleteRow(row)"
            type="text"
            icon="el-icon-circle-close"
            >删 除</el-button
          >
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="toView(row)"
            >查 看</el-button
          >
        </template>
      </avue-crud>
      <add-dialog
        v-if="showDialog"
        :showDialog="showDialog"
        :dialogType="dialogType"
        :docname="docname"
        :scorePackageId="scorePackageId"
        :rowData="rowData"
        @handleClose="handleClose"
      />
    </div>
  </basic-container>
</template>

<script>
import addDialog from "./components/addDialog";
import {
  tableOptions1,
  tableOptions2
} from "@/const/crud/specialTask/patientEdu/prescriptionUpload";
import {
  getPrescriptionList,
  deleteTaskPrescription
} from "@/api/specialTask/patientEdu/prescriptionUpload";
import { getPage } from "@/api/assignPoints/currency/manList";

export default {
  name: "prescriptionUpload",
  data() {
    return {
      tableData: [],
      option1: null,
      page: {
        pageSize: 10,
        pagerCount: 5
      },
      tableLoading: false,
      searchForm: {},
      typeVal: "",
      showDialog: false,
      docname: "",
      scorePackageId: "",
      scorePackageTaskNum: "",
      viewShow: false,
      dialogType: "add",
      rowData: null
    };
  },
  computed: {
    isShowDelete() {
      const tableData = this.tableData.filter(ele => {
        if (ele.id) return ele;
      });
      return tableData.length > 1;
    }
  },
  created() {
    this.scorePackageId = this.$route.query.id;
    this.typeVal = this.$route.query.type;
    if (this.typeVal === "shoudong") {
      this.option1 = tableOptions1;
    } else if (this.typeVal === "duijie") {
      this.option1 = tableOptions2;
    }
    // this.getPage()
  },
  methods: {
    // 新增弹窗
    addData() {
      if (this.tableData.length >= this.scorePackageTaskNum) {
        return this.$message.warning("不能新增更多！");
      }
      this.dialogType = "add";
      this.showDialog = true;
    },
    // 删除
    deleteRow(row) {
      this.$confirm("确认删除吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteTaskPrescription({ id: row.id }).then(res => {
            this.$message.success("删除成功");
            this.getList(this.page);
          });
        })
        .catch(() => {});
    },
    // 查看
    toView(row) {
      this.dialogType = "view";
      this.showDialog = true;
      this.rowData = row;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    // 初始化页面
    async getList(page, params) {
      if (this.typeVal === "shoudong") {
        // 获取医生名称和任务数
        await this.getPage();
      }
      // 获取列表数据
      this.tableLoading = true;
      const response = await getPrescriptionList(
        Object.assign(
          {
            scorePackageId: this.scorePackageId,
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm
        )
      );
      this.tableData = [];
      this.tableData = response.data.data.records;
      this.page.total = response.data.data.total;
      this.tableLoading = false;
    },
    // 获取医生名称和任务数
    async getPage() {
      const res = await getPage({ packageId: this.scorePackageId });
      const records = res.data.data.records;
      if (records.length > 0) {
        const { userId, taskNum } = records[0];
        this.docname = userId;
        this.scorePackageTaskNum = taskNum;
      } else {
        this.$message.error("没有医生名称和任务数");
      }
    },
    // 返回按钮
    backPage() {
      if (this.typeVal === "shoudong") {
        this.$store.commit("ADD_PATIENT_EDU_TYPE", "second");
      } else {
        this.$store.commit("DEL_PATIENT_EDU_TYPE");
      }
      this.$router.$avueRouter.closeTag();
      this.$router.push("/admin/specialTask/patientEdu/index");
    },
    handleClose(val) {
      if (val === "success") {
        this.getList(this.page);
      }
      this.showDialog = false;
    },
    // 分页查询
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    }
  },
  components: {
    addDialog
  }
};
</script>
