<!--
 * @Descripttion: 
 * @version: 
 * @Author: JC9527
 * @Date: 2023-09-04 10:20:06
 * @LastEditors: laozt 2721205210@qq.com
 * @LastEditTime: 2023-12-26 10:34:38
-->
<template>
  <div class="plan-management">
    <div class="search-box">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="年份" prop="plannedYear">
          <el-date-picker
            v-model="queryParams.plannedYear"
            type="year"
            size="mini"
            value-format="yyyy"
            placeholder="请选择年份"
          />
        </el-form-item>
        <el-form-item label="月份" prop="plannedMonth">
          <el-select
            v-model="plannedMonth"
            multiple
            collapse-tags
            style="margin-left: 20px"
            placeholder="请选择月份"
            @change="multipleChange"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input
            v-model="queryParams.enterpriseName"
            placeholder="请输入企业名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="行政区划" prop="district">
          <executive
            @change="onChange"
            @quhuadizhi="quhuadizhi"
            ref="executive"
          />
        </el-form-item>
        <el-form-item label="执法层级" prop="lawHierarchy">
          <!-- <el-select
            v-model="queryParams.lawHierarchy"
            placeholder="请选择执法层级"
            filterable
            :disabled="islawHierarchy"
          >
            <el-option
              v-for="item in dict.type.dict_law_hierarchy"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select> -->
          <ClCascader ref="clCascader" @change="onChangeLawHierarchy" />
        </el-form-item>
        <!-- <el-form-item label="是否省重点" prop="isPoint">
          <el-radio-group v-model="queryParams.isPoint">
            <el-radio label="">全部</el-radio>
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="queryParams.status">
            <el-radio :label="1">正式</el-radio>
            <el-radio :label="0">草稿</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button class="btn-search" size="mini" @click="handleQuery"
            >搜索</el-button
          >
          <el-button class="btn-reset" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            size="mini"
            class="btn-del"
            icon="el-icon-delete"
            :disabled="multiple"
            :style="multiple ? 'background: #eb7a9a;' : ''"
            @click="handleDelete"
            >批量删除</el-button
          >
        </el-form-item>
      </el-form>

      <div class="btn-group">
        <div class="export" @click="handleExport">
          <img src="@/assets/images/export.png" alt="" />
          <span>导出excel</span>
        </div>
        <div class="newProject" @click="handleAdd">
          <img src="@/assets/images/newProject.png" alt="" />
          <span>新增计划</span>
        </div>
      </div>
    </div>
    <div class="plan-tables">
      <el-table
        v-loading="loading"
        :data="enterpriseList"
        :border="false"
        tooltip-effect="light"
        row-key="id"
        :row-class-name="tableRowClassName"
        :height="tableHeight"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="计划年月" align="center" prop="plannedYear">
          <template slot-scope="scope">
            <span>
              {{ scope.row.plannedYear }}-{{ scope.row.plannedMonth }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="行政区划" align="center" prop="district">
          <template slot-scope="scope">
            <span>{{ componendDistrict(scope.row.district) || "/" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="企业名称"
          align="center"
          prop="enterpriseName"
        />
        <!-- <el-table-column label="是否省重点" align="center" prop="isPoint">
          <template slot-scope="scope">
            <span>{{
              scope.row.isPoint == 1 ? "是" : scope.row.isPoint == 2 ? "否" : ""
            }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="执法地区" align="center" prop="lawAreas" />
        <el-table-column label="执法级别" align="center" prop="lawLevel" />
        <el-table-column label="执法层级" align="center" prop="lawHierarchy" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <span style="color: #28b384" v-if="scope.row.status == 1"
              >正式</span
            >
            <span style="color: #fdab5b" v-else-if="scope.row.status == 0"
              >草稿</span
            >
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250" header-align="center">
          <template slot-scope="scope">
            <div class="tabs-btns">
              <div class="look" @click="handleInfo(scope.row)">
                <img src="@/assets/images/look.png" alt="" />
                <span>查看</span>
              </div>
              <div class="change" @click="handleUpdate(scope.row)">
                <img src="@/assets/images/change.png" alt="" />
                <span>修改</span>
              </div>
              <div class="delete" @click="handleDelete(scope.row)">
                <img src="@/assets/images/delete.png" alt="" />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 发布计划 -->
    <add-dialog ref="addDialog" @closeDialog="closeDialog" />

    <!-- 详情对话框 -->
    <info-dialog ref="infoDialog" />

    <!-- 编辑对话框 -->
    <edit-dialog ref="editDialog" />

    <!-- 重置密码 -->
    <el-dialog
      title="需要修改原始密码"
      :visible.sync="isResetPwdDialog"
      :show-close="false"
      width="30%"
      center
    >
      <resetPwd />
    </el-dialog>
  </div>
</template>
<script>
import {
  listEnterprise,
  getEnterprise,
  delEnterprise,
  addEnterprise,
  updateEnterprise,
} from "@/api/yingji/enterprise";
import AddDialog from "@/views/components/AddDialog/index.vue";
import ClCascader from "@/views/components/ClCascader/index.vue";
import InfoDialog from "./planmanage/components/infoDialog.vue";
import EditDialog from "./planmanage/components/editDialog.vue";
import executive from "./echarts/executive.vue";
import resetPwd from "../system/userc/profile/resetPwd.vue";
import detectZoom from "@/utils/detectZoom";

export default {
  components: {
    AddDialog,
    ClCascader,
    InfoDialog,
    executive,
    EditDialog,
    resetPwd,
  },
  dicts: ["dict_law_hierarchy"],
  data() {
    return {
      isResetPwdDialog: false,
      islawHierarchy: false,
      lawHierarchys: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
      ],
      options: [
        {
          value: "1",
          label: "1月",
        },
        {
          value: "2",
          label: "2月",
        },
        {
          value: "3",
          label: "3月",
        },
        {
          value: "4",
          label: "4月",
        },
        {
          value: "5",
          label: "5月",
        },
        {
          value: "6",
          label: "6月",
        },
        {
          value: "7",
          label: "7月",
        },
        {
          value: "8",
          label: "8月",
        },
        {
          value: "9",
          label: "9月",
        },
        {
          value: "10",
          label: "10月",
        },
        {
          value: "11",
          label: "11月",
        },
        {
          value: "12",
          label: "12月",
        },
      ],
      plannedMonth: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计划企业表格数据
      enterpriseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        enterpriseId: null,
        createId: null,
        updateId: null,
        userId: null,
        deptId: null,
        district: null,
        plannedYear: null,
        isPoint: "",
        enterpriseName: null,
        lawSort: null,
        lawAreas: null,
        lawLevel: null,
        lawHierarchy: null,
        plannedMonth: null,
        status: 1,
        checkStatus: null,
        checkId: null,
        checkAgeing: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tableHeight: 400,
    };
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    resetPwd() {
      this.isResetPwdDialog = true;
    },
    // 行政区划
    onChange(e) {
      // console.log(1, e);
      this.queryParams.district = e;
    },
    // 执法层级
    onChangeLawHierarchy(e) {
      this.queryParams.lawHierarchy = e;
    },
    multipleChange(e) {
      // console.log(e.join(","));
      this.queryParams.plannedMonth = e.join(",");
    },
    // 关闭对话框刷新页面
    closeDialog() {
      this.getList();
    },
    /** 查询计划企业列表 */
    getList() {
      this.loading = true;
      listEnterprise(this.queryParams).then((response) => {
        this.enterpriseList = response.data;
        // console.log(this.enterpriseList);
        this.total = response.data.length;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        createId: null,
        createBy: null,
        createTime: null,
        updateId: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null,
        district: null,
        plannedYear: null,
        isPoint: null,
        enterpriseName: null,
        lawSort: null,
        lawAreas: null,
        lawLevel: null,
        lawHierarchy: null,
        plannedMonth: null,
        status: null,
        checkStatus: null,
        checkId: null,
        checkAgeing: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.plannedMonth = "";
      this.$refs.executive.onClear();
      this.$refs.clCascader.onClear();
      if (this.$route.query.lawHierarchy) {
        this.queryParams.lawHierarchy = null;
      }
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addDialog.openDialog();
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.$refs.infoDialog.openDialog(row);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.editDialog.openDialog(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除计划企业的数据项？")
        .then(function () {
          return delEnterprise(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    quhua(e) {
      this.form.district = e;
    },
    // 获取行政区划树
    quhuadizhi(district) {
      this.option = district;
    },
    // 匹配行政区划
    componendDistrict(district) {
      let fullName;
      let treeData = JSON.parse(localStorage.getItem("TREE_DATA"));
      treeData.map((item) => {
        if (item.districtCode == district) {
          fullName = item.district;
        } else {
          item.children.map((itemTwo) => {
            if (itemTwo.countyCode == district) {
              fullName = item.district + "-" + itemTwo.county;
            }
          });
        }
      });
      return fullName;
    },
    // 导出
    handleExport() {
      this.$confirm(`确认根据您的搜索条件导出搜索之后的数据吗?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.download(
            "/pharmaceuticals/bPlanEnterprise/exportplan",
            {
              ...this.queryParams,
            },
            `计划管理.xlsx`
          );
        })
        .catch(() => {});
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "success-row";
      }
      return "";
    },
  },
  created() {
    let treeData = JSON.parse(localStorage.getItem("TREE_DATA"));
    this.myDistrict = treeData;
    // 查询条件
    let userInfo = JSON.parse(sessionStorage.getItem("USER_INFO"));
    if (userInfo.lawLevel == "县级" || userInfo.lawLevel == "镇级") {
      this.queryParams.district = userInfo.permissionCode;
      this.queryParams.lawHierarchy = userInfo.nickName;
    }

    if (userInfo.lawLevel == "镇级") {
      this.islawHierarchy = true;
    }
    this.queryParams.plannedYear = this.$moment(new Date()).format("yyyy");
    if (this.$route.query.lawHierarchy) {
      // console.log(this.$route.query.lawHierarchy);
      this.queryParams.lawHierarchy = this.$route.query.lawHierarchy;
    }
    if (JSON.parse(sessionStorage.getItem("USER_P_STATUS")) == 1) {
      // 原始密码修改
      this.resetPwd();
    }

    // 初始化
    this.getList();
    // 设置表格高度 100% 125%
    this.tableHeight = detectZoom == 100 ? 650 : 450;
  },
};
</script>
<style lang="scss" scoped>
.plan-management {
  padding: 0 20px;
  background-color: #fff;
  height: 100%;

  .search-box {
    display: flex;
    justify-content: space-between;
    padding: 31px 0 12px 0;
    position: relative;
  }

  .plan-tables {
    margin-top: 10px;

    ::v-deep .el-table {
      .success-row {
        background-color: rgba(234, 242, 253, 0.25);
      }
      td {
        height: 42px;
        box-sizing: border-box;
        text-align: center;
        font-size: 14px;
        font-family: "Alibaba PuHuiTi";
        font-weight: 400;
        color: #525966;
      }
      th {
        height: 45px;
        background-color: #eaf2fd;
        font-size: 15px;
        font-family: "Alibaba PuHuiTi";
        font-weight: bold;
        color: #525966;
      }
    }
    ::v-deep .el-table::before {
      height: 0;
    }
    .tabs-btns {
      display: flex;
      align-items: center;
      justify-content: center;
      .look,
      .change,
      .delete {
        display: flex;
        align-items: center;
        margin-right: 30px;
        cursor: pointer;
        img {
          width: 20px;
          margin-right: 5px;
        }
        span {
          font-size: 15px;
          font-family: Alibaba PuHuiTi;
          font-weight: 400;
          color: #045ffd;
        }
      }
      .delete {
        margin-right: 0;
        span {
          color: #f71052;
        }
      }
    }
  }

  .btn-group {
    display: flex;
    align-items: center;
    position: absolute;
    bottom: 30px;
    right: 0;
    .export,
    .newProject {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 6px 19px;
      cursor: pointer;
      img {
        width: 22px;
        margin-right: 10px;
      }
      span {
        color: #ffffff;
        font-size: 14px;
        font-family: "Alibaba PuHuiTi";
        font-weight: 400;
      }
    }
    .export {
      background-color: #2378ec;
      margin-right: 30px;
    }
    .newProject {
      background-color: #28b384;
    }
  }

  .btn-search {
    background: #2378ec;
    color: #ffffff;
    font-size: 15px;
    font-family: Alibaba PuHuiTi;
    border-radius: 2px;
    border: none;
  }

  .btn-reset {
    background: #fdab5b;
    color: #ffffff;
    font-size: 15px;
    font-family: Alibaba PuHuiTi;
    border-radius: 2px;
    border: none;
  }

  .btn-del {
    background: #f71052;
    color: #ffffff;
    font-size: 15px;
    font-family: Alibaba PuHuiTi;
    border-radius: 2px;
    border: none;
  }
}
</style>
