<template>
  <div class="item-shield">
    <bread-crumb :curmbFirst="curmbFirst" :curmbSecond="curmbSecond"></bread-crumb>
    <div class="component-content screenSet" id="screenSet">
      <el-radio-group v-model="shieldType" @change="changeType">
        <el-radio-button label="0">屏蔽机构</el-radio-button>
        <el-radio-button label="1">屏蔽人员</el-radio-button>
      </el-radio-group>
      <!-- <el-button class="complete" type="primary" size="small" @click="complete">完成</el-button> -->
      <el-row :gutter="30" class="row" type="flex" style="margin-top: 10px;">
        <el-form :model="formInline" ref="formInline" label-width="75px" class="form-inline">
          <el-col :span="6" v-if=" shieldType == 0">
            <el-form-item label="地区：">
              <el-cascader
                size="small"
                ref="cascaderRegion"
                :options="optionsRegion"
                :props="props"
                v-model="formInline.region"
                @change="handleChange"
                filterable
                change-on-select
                placeholder="请选择地区"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="机构：">
              <el-input size="small" v-model="formInline.hospitalName" placeholder="请输入机构名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" v-if="shieldType == 1">
            <el-form-item label="名称：">
              <el-input size="small" v-model="formInline.doctorName" placeholder="请输入人员名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态类型：">
              <el-select size="small" v-model="formInline.blackStatus" placeholder="请选择状态">
                <el-option
                  v-for="(item, index) in statusList"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="padding:0;text-align:right;padding-right:10px;">
            <el-button type="primary" size="small" @click="searchList()">搜索</el-button>
          </el-col>
        </el-form>
      </el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column v-if="shieldType == 0" prop="hospitalName" label="医院名称" align="center"></el-table-column>
        <el-table-column
          v-if="shieldType == 0"
          prop="hospitalLevelGradeName"
          label="医院级别"
          align="center"
        ></el-table-column>
        <el-table-column v-if="shieldType == 1" prop="doctorName" label="人员名称" align="center"></el-table-column>
        <el-table-column v-if="shieldType == 1" prop="hospitalName" label="所属医院" align="center"></el-table-column>
        <el-table-column v-if="shieldType == 1" prop="departmentName" label="所属科室" align="center"></el-table-column>
        <el-table-column prop="provinceName" label="所属省份" align="center"></el-table-column>
        <el-table-column prop="cityName" label="所属城市" align="center"></el-table-column>
        <el-table-column prop="countyName" label="所属区县" align="center"></el-table-column>
        <el-table-column prop="townName" label="所属街道" align="center"></el-table-column>
        <el-table-column fixed="right" label="状态" align="center">
          <template slot-scope="scope"><p v-bind:class="{ 'text-black': scope.row.blackStatus === 2 }">{{ scope.row.blackStatus | shieldStatus }}</p></template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="openDialog(scope.row)"
            >{{ scope.row.blackStatus | shieldButton }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formInline.pageNo"
          :page-sizes="[10, 20, 30, 100]"
          :page-size="formInline.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalRows"
        ></el-pagination>
      </div>
      <el-dialog title="提醒" :visible.sync="dialogShow" width="30%" center>
        <p v-if="shieldType == 0 && shieldBlock == 1">屏蔽后该机构将从圈选机构范围内去除，确定继续屏蔽吗？</p>
        <p v-if="shieldType == 0 && shieldBlock == 2">取消屏蔽后该机构将恢复到圈选范围，确定要取消屏蔽吗？</p>
        <p v-if="shieldType == 1 && shieldBlock == 1">屏蔽后该人员将从圈选人员范围内去除，确定要继续屏蔽吗？</p>
        <p v-if="shieldType == 1 && shieldBlock == 2">取消屏蔽后该人员将恢复到圈选范围，确定要取消屏蔽吗？</p>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="changeStatus()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import BreadCrumb from "../../components/breadcrumb.vue";
import { mapGetters } from "vuex";
import { openLoading, closeLoading } from "../../utils/utils";
import * as commonUtil from "../../utils/utils";
import * as operationData from "../../utils/operation";
let vm = null;
export default {
  components: {
    BreadCrumb
  },
  data() {
    return {
      curmbFirst: "教培项目",
      curmbSecond: "屏蔽",
      projectId: "",
      shieldType: 0,
      formInline: {
        region: ["0"],
        administrativeId: "",
        hospitalName: "",
        doctorName: "",
        blackStatus: 0,
        pageNo: 1,
        pageSize: 10
      },
      statusList: [
        {
          label: "全部",
          value: 0
        },
        {
          label: "未屏蔽",
          value: 1
        },
        {
          label: "已屏蔽",
          value: 2
        }
      ],
      optionsRegion: [],
      areaList: [
        {
          label: "全部地区",
          value: 0
        }
      ],
      tableData: [],
      totalRows: 0,
      dialogShow: false,
      shieldBlock: "",
      scopeRow: {},
      administrativeValue: "",
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          console.log("node", node);
          if (node.level == 1) {
            let req = {
              provinceId: node.data.value
            };
            vm.GET("basic-data/position/cities", req).then(res => {
              let newData = vm.setMoreOption(res.data.cityList, "cities");
              console.log("newData", newData);
              resolve(newData);
            });
          } else if (node.level == 2) {
            let req = {
              cityId: node.data.value
            };
            vm.GET("basic-data/position/counties", req).then(res => {
              let newData = vm.setMoreOption(res.data.countyList, "counties");
              console.log("newData", newData);
              resolve(newData);
            });
          } else if (node.level == 3) {
            let req = {
              countyId: node.data.value
            };
            vm.GET("basic-data/position/towns", req).then(res => {
              let newData = vm.setMoreOption(res.data.townList, "towns");
              console.log("newData", newData);
              resolve(newData);
            });
          }
        }
      }
    };
  },
  computed: {
    ...mapGetters(["_token"])
  },
  created() {
    vm = this;
    vm.projectId = vm.getUrlSearch(window.location.href, "projectId");
    vm.getRegionOption();
    vm.search();
  },
  // 挂载到Dom完成时
  mounted: function() {
    // window.onbeforeunload = function(e) {
    //   let pathname = location.href.split("/");
    //   pathname = pathname[pathname.length - 1];
    //   console.log(pathname);
    //   if (pathname == "item-shield") {
    //     e = e || window.event;
    //     if (e) {
    //       e.returnValue = "数据不会保存";
    //     }
    //     return "数据不会保存";
    //   }
    // };

    commonUtil.resizeHeight();
  },
  // beforeRouteLeave(to, from, next) {
  //   const answer = window.confirm(
  //     "Do you really want to leave? you have unsaved changes!"
  //   );
  //   if (answer) {
  //     next();
  //   } else {
  //     next(false);
  //   }
  // },
  methods: {
    handleChange(value) {
      let areaId = "000";
      for (let i = 0; i < value.length; i++) {
        areaId += "_" + value[i];
      }
      vm.administrativeValue = areaId;
      if (areaId == "000_0") {
        vm.administrativeValue = "";
      }
      console.log(value, vm.administrativeValue);
    },
    //获取地区
    getRegionOption() {
      let req = {};
      openLoading(vm);
      vm.GET("basic-data/position/provinces", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          vm.optionsRegion = operationData.setRegionOption2(
            res.data.provinceList
          );
        }
      });
    },
    setMoreOption(data, type) {
      let option = [];
      for (let i = 0; i < data.length; i++) {
        let obj = data[i];
        if (type == "cities") {
          obj.label = data[i].cityName;
          obj.value = data[i].cityId;
        } else if (type == "counties") {
          obj.label = data[i].countyName;
          obj.value = data[i].countyId;
          // obj.leaf = true;
        } else if (type == "towns") {
          obj.label = data[i].townName;
          obj.value = data[i].townId;
          obj.leaf = true;
        }
        option.push(obj);
      }
      return option;
    },
    searchList() {
      this.formInline.pageNo = 1;
      this.search();
    },
    search() {
      if (vm.shieldType == 0) {
        let req = {
          administrativeId: vm.administrativeValue,
          blackStatus: vm.formInline.blackStatus,
          hospitalName: vm.formInline.hospitalName,
          pageNo: vm.formInline.pageNo,
          pageSize: vm.formInline.pageSize,
          projectId: vm.projectId
        };
        openLoading(vm);
        vm.GET("portal/black/getScopeHospitalWithBlack", req).then(res => {
          closeLoading(vm);
          vm.setTable(res);
          // vm.tableData = res.data.list;
          // vm.totalRows = res.data.count;
        });
      } else if (vm.shieldType == 1) {
        let req = {
          blackStatus: vm.formInline.blackStatus,
          hospitalName: vm.formInline.hospitalName,
          doctorName: vm.formInline.doctorName,
          pageNo: vm.formInline.pageNo,
          pageSize: vm.formInline.pageSize,
          projectId: vm.projectId
        };
        openLoading(vm);
        vm.GET("portal/black/getScopePeopleWithBlack", req).then(res => {
          closeLoading(vm);
          vm.setTable(res);
          // vm.tableData = res.data.list;
          // vm.totalRows = res.data.count;
        });
      }
    },
    setTable(res) {
      vm.tableData = [];
      vm.totalRows = 0;
      if (res.code == "000000") {
        vm.tableData = res.data.list;
        vm.totalRows = res.data.count;
      } else {
        vm.$message(res.message);
      }
    },
    openDialog(row) {
      vm.scopeRow = row;
      vm.shieldBlock = row.blackStatus;
      vm.dialogShow = true;
    },
    // 屏蔽/取消屏蔽
    changeStatus() {
      let row = vm.scopeRow;
      let option = "";
      if (row.blackStatus == 2) {
        option = 1;
      } else if (row.blackStatus == 1) {
        option = 2;
      }
      if (vm.shieldType == 0) {
        //屏蔽机构
        let req = {
          blackStatus: option,
          hospitalId: row.hospitalId,
          projectId: vm.projectId
        };
        openLoading(vm);
        vm.POST("portal/black/setHospitalBlackStatus", req).then(res => {
          closeLoading(vm);
          vm.dialogShow = false;
          vm.$message(res.message);
          vm.search();
        });
      } else if (vm.shieldType == 1) {
        //屏蔽人员
        let req = {
          blackStatus: option,
          doctorId: row.doctorId,
          projectId: vm.projectId
        };
        openLoading(vm);
        vm.POST("portal/black/setPeopleBlackStatus", req).then(res => {
          closeLoading(vm);
          vm.dialogShow = false;
          vm.$message(res.message);
          vm.search();
        });
      }
    },
    //改变
    changeType(value) {
      console.log(value);
      vm.resetForm();
      // vm.searchList();
    },
    complete() {
      console.log("完成");
    },
    //重置表单
    resetForm() {
      vm.formInline = Object.assign(
        {},
        {
          region: ["0"],
          administrativeId: "",
          hospitalName: "",
          doctorName: "",
          blackStatus: 0,
          pageNo: 1,
          pageSize: 10
        }
      );
      vm.searchList();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.formInline.pageSize = val;
      vm.searchList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formInline.pageNo = val;
      this.search();
    }
  }
};
</script>
<style lang="scss">
.item-shield {
  .component-content {
    padding: 10px;
    background: #fff;
    .complete {
      float: right;
    }
    .text-black {
      color: #D51F35;
    }
  }
}
</style>