<!--
 * @Descripttion: 
 * @version: 
 * @Author: JC9527
 * @Date: 2023-09-04 10:20:06
 * @LastEditors: laozt 2721205210@qq.com
 * @LastEditTime: 2023-12-19 10:06:03
-->
<template>
  <div class="container-main" ref="main">
    <div class="enforce-the-law-title">
      <div class="filtrate-condition">
        <div class="select-input">
          <div class="select-span">年份:</div>
          <div>
            <el-date-picker
              v-model="queryParms.year"
              type="year"
              placeholder="请选择年份"
              value-format="yyyy"
              size="small"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="select-input">
          <div class="select-span" style="width: 85px">企业名称:</div>
          <el-input
            v-model="queryParms.enterpriseName"
            size="small"
            placeholder="请输入企业名称"
          ></el-input>
        </div>
        <div class="select-input">
          <el-input
            v-model="queryParms.entCode"
            size="small"
            placeholder="请输入统一社会信用代码"
          ></el-input>
        </div>
        <div style="display: flex">
          <div class="export" @click="searchList">
            <span>搜索</span>
          </div>
          <div
            class="export"
            @click="chongzhiList"
            style="background-color: #fdab5b"
          >
            <span>重置</span>
          </div>
        </div>
      </div>
      <div style="display: flex">
        <div class="export" @click="addkeys" style="background-color: #28b384">
          <span>新增</span>
        </div>

        <div class="export" style="background: #f71052" @click="deleteItem">
          <span>删除</span>
        </div>

        <div
          class="export"
          @click="handleImport"
          style="background-color: #28b384"
        >
          <span>导入</span>
        </div>
      </div>
    </div>
    <div class="tables">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        :height="484"
        :border="false"
        :row-class-name="tableRowClassName"
        :cell-style="columnStyle"
        tooltip-effect="light"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="year" label="年份" header-align="center">
          <template slot-scope="scope">
            {{ $moment(new Date(scope.row.year)).format("yyyy") }}
          </template>
        </el-table-column>
        <el-table-column
          prop="enterpriseName"
          label="企业名称"
          show-overflow-tooltip
          header-align="center"
        >
        </el-table-column>
        <el-table-column
          prop="entCode"
          label="统一社会信用代码"
          header-align="center"
        >
        </el-table-column>

        <el-table-column label="操作" header-align="center">
          <template slot-scope="scope">
            <div class="tabs-btns">
              <div class="look" @click="look(scope.row)">
                <img src="@/assets/images/look.png" alt="" />
                <span>查看</span>
              </div>
              <div class="change" @click="change(scope.row)">
                <img src="@/assets/images/change.png" alt="" />
                <span>修改</span>
              </div>
              <div class="delete" @click="deleteItem(scope.row)">
                <img src="@/assets/images/delete.png" alt="" />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="searchBefore.pageNum"
      :limit.sync="searchBefore.pageSize"
      @pagination="getList"
    />
    <!-- 新增修改 -->
    <el-dialog
      :visible.sync="dialogVisible"
      custom-class="dialog-boxed"
      append-to-body
      width="50%"
    >
      <div class="dialog-slot">
        <div class="closeClick">
          <div class="newplan">
            <div class="line"></div>
            <div class="span">{{ headerText }}省重点企业</div>
          </div>
          <img src="@/assets/images/close.png" alt="" @click="closeDetail" />
        </div>
        <div class="content">
          <div class="filtrate">
            <el-form
              :model="form"
              label-width="100px"
              class="demo-ruleForm"
              ref="form1"
              :rules="rules"
            >
              <el-row>
                <el-col :span="24">
                  <el-form-item label="企业名称:" prop="enterpriseName">
                    <el-input
                      placeholder="请输入企业名称"
                      v-model="form.enterpriseName"
                      size="small"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="信用代码:" prop="entCode">
                    <el-input
                      v-model="form.entCode"
                      size="small"
                      placeholder="请输入企业信用代码"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="年份:" prop="year">
                    <el-date-picker
                      v-model="form.year"
                      type="year"
                      placeholder="请选择年份"
                      value-format="yyyy"
                      size="small"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label="备注:" prop="remark">
                    <el-input
                      type="textarea"
                      v-model="form.remark"
                      size="small"
                      :rows="5"
                      placeholder="请输入备注"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <div
                style="
                  display: flex;
                  align-items: center;
                  justify-content: center;
                "
              >
                <div class="select-btn" @click="confirm">确认</div>
                <div class="select-btn" @click="closeDetail">取消</div>
              </div>
            </el-form>
          </div>
        </div>
      </div>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog
      :visible.sync="addShow"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      append-to-body
      custom-class="dialog-boxed"
      width="50%"
    >
      <div class="dialog-slot">
        <div class="closeClick">
          <div class="newplan">
            <div class="line"></div>
            <div class="span">查看省重点企业详细</div>
          </div>
          <img src="@/assets/images/close.png" alt="" @click="shutDownDetail" />
        </div>
        <div class="content">
          <div class="filtrate">
            <el-descriptions :column="2" border>
              <el-descriptions-item label="年份">
                {{ $moment(new Date(details.year)).format("yyyy") }}
              </el-descriptions-item>
              <el-descriptions-item label="企业名称">
                {{ details.enterpriseName }}
              </el-descriptions-item>
              <el-descriptions-item label="信用代码">{{
                details.entCode
              }}</el-descriptions-item>
              <el-descriptions-item label="备注">{{
                details.remark
              }}</el-descriptions-item>
            </el-descriptions>
          </div>
        </div>
      </div>
    </el-dialog>
    <!--  省重点导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            href="/doc/szd_tl.xlsx"
            target="_blank"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Loading } from "element-ui";
import executive from "./echarts/executive";
import {
  listEnterprise,
  addEnterprise,
  updateEnterprise,
  deleteEnterprise,
  exportEnterprise,
  getEnterprise,
} from "@/api/yingji/keyEnterprise.js";
// import { mapState } from "vuex";
import { getToken } from "@/utils/auth";

export default {
  components: {  executive },
  name: "Article",
  data() {
    //验证统一社会信用代码
    var checkSocialCreditCode = (rule, code, callback) => {
      // 空值直接返回false
      if (!code) {
        return callback(new Error("请输入统一社会信用代码"));
      }

      //18位及正则校验
      const reg = /^\w\w\d{6}\w{9}\w$/;
      if (code.length !== 18 || !reg.test(code)) {
        return callback(new Error("请输入正确的统一社会信用代码"));
      }

      const codeOrigin = "0123456789ABCDEFGHJKLMNPQRTUWXY"; // 统一社会信用代码可用字符 不含I、O、S、V、Z
      const weightedfactors = [
        1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28,
      ]; // 统一社会信用代码相对应顺序的加权因子

      let ci; // 统一社会信用代码相应顺序的值
      let wi; // 统一社会信用代码相应顺序的加权因子
      let total = 0; // 计算结果

      // 数值与加权因子相乘之和
      for (let i = 0; i < code.length - 1; i++) {
        ci = codeOrigin.indexOf(code[i]);
        wi = weightedfactors[i];
        total += ci * wi;
      }

      // 最后一位校验
      let logicCheckCode = 31 - (total % 31);
      if (logicCheckCode === 31) logicCheckCode = 0;
      logicCheckCode = codeOrigin[logicCheckCode];
      if (logicCheckCode !== code.slice(17)) {
        return callback(new Error("请输入正确的统一社会信用代码"));
      } else {
        callback();
      }
    };
    return {
      tableData: [],
      options: [],
      checkList: ["全部"],
      checkListTwo: [],
      loading: false,
      headerText: "",
      ids: null,
      //是否显示弹出框
      dialogVisible: false,
      queryParms: {
        year: "",
        entCode: "",
        enterpriseName: "",
      },
      queryParmsdistrict: [],
      district: [],
      xzList: [],
      props: {
        checkStrictly: true,
        value: "value",
        label: "label",
        children: "children",
      },
      isNoneDivision: false,
      searchBefore: {
        pageSize: 10,
        pageNum: 1,
      },
      //新增修改的参数
      form: {
        //企业名称
        enterpriseName: "",
        //企业代码
        entCode: "",
        //备注
        remark: "",
        //年份
        year: "",
      },
      addShow: false,
      //详情
      details: {},
      rules: {
        enterpriseName: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
        ],
        entCode: [{ validator: checkSocialCreditCode, trigger: "blur" }],
        year: [{ required: true, message: "请选择年份", trigger: "blur" }],
      },
      //总数
      total: 0,
      fileList: [],
      // 省重点导入参数
      upload: {
        // 是否显示弹出层（省重点导入）
        open: false,
        // 弹出层标题（省重点导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/pharmaceuticals/bKeyEnterprise/common/importExcel",
      },
    };
  },
  created() {
    this.queryParms.year = this.$moment(new Date()).format("yyyy");
    this.searchBefore = { ...this.searchBefore, ...this.queryParms };
    this.getList();
  },
  // computed: {
  //   ...mapState({
  //     //根据存储的dept.parentId判断是哪个等级
  //     dept: (state) => state.user.dept,
  //   }),
  // },
  methods: {
    handleImport() {
      this.upload.title = "省重点企业导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    searchList() {
      this.searchBefore = {
        pageSize: 10,
        pageNum: 1,
      };
      this.searchBefore = { ...this.searchBefore, ...this.queryParms };
      this.getList();
    },
    reset() {
      this.form = {
        //企业名称
        enterpriseName: "",
        //信用代码
        entCode: "",
        //备注
        remark: "",
        //大类
        // type: "",
        //年份
        year: "",
      };
    },
    //重置列表
    chongzhiList() {
      this.searchBefore = {
        pageSize: 10,
        pageNum: 1,
      };
      this.queryParms = {
        year: "",
        entCode: "",
        enterpriseName: "",
      };
      this.getList();
    },
    //获取列表
    getList() {
      this.loading = true;
      listEnterprise(this.searchBefore).then((res) => {
        // console.log(res);
        this.tableData = res.data.list;
        this.loading = false;
        this.total = res.data.total;
      });
    },
    //关闭省重点详情
    shutDownDetail() {
      this.addShow = false;
    },
    //新增打开
    addkeys() {
      this.reset();
      this.headerText = "新增";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form1.clearValidate();
      });
    },
    //关闭新增修改弹窗
    closeDetail() {
      this.reset();
      this.dialogVisible = false;
    },
    //查看详情
    look(e) {
      getEnterprise(e.id).then((res) => {
        console.log(res);
        this.details = res.data;
        this.addShow = true;
      });
    },
    //修改数据
    change(e) {
      this.headerText = "修改";
      getEnterprise(e.id).then((res) => {
        this.form = res.data;
        // this.queryParmsdistrict = this.form.district
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.form1.clearValidate();
        });
      });
    },
    //保存
    confirm() {
      this.$refs.form1.validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEnterprise(this.form).then((response) => {
              this.$model.msgSuccess("修改成功");
              this.dialogVisible = false;
              this.getList();
            });
          } else {
            addEnterprise(this.form).then((res) => {
              this.$model.msgSuccess("新增成功");
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    //删除数据
    deleteItem(row) {
      /** 删除按钮操作 */
      const ids = row.id || this.ids;
      if (!ids) {
        this.$model.msgError("请选择数据");
        return;
      }
      this.$confirm("是否确认删除该数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteEnterprise({ idList: ids });
        })
        .then(() => {
          this.$model.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
    // 多选框变化事件
    handleSelectionChange(rows) {
      let arr = rows.map((item) => item.id);
      this.ids = arr.join(",");
    },

    // tabs不同行给不同class
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "success-row";
      }
      return "";
    },
    // 根据状态数据修改字体颜色
    columnStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 3) {
        if (row.state == "未执法") {
          return "color: #F71052";
        } else if (row.state == "已执法") {
          return "color: #23B06C";
        }
      }
    },
    handleSuccess() {},
  },
  mounted() {
    this.checkListTwo = this.checkList;
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-descriptions-row th {
  width: 20% !important;
  font-size: 14px;
  font-family: "Alibaba PuHuiTi";
  font-weight: 400;
}
::v-deep .el-descriptions-row td {
  width: 30% !important;
  font-size: 14px;
  font-family: "Alibaba PuHuiTi";
  font-weight: 400;
}
.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;
    }
  }
}
::v-deep .el-date-editor {
  width: 100%;
}
::v-deep .el-dialog__body {
  padding-bottom: 10px;
}
.container-main {
  // height: calc(100vh - 84px);
  overflow: hidden;
  padding: 0 20px;
  height: 100%;
  background-color: #fff;
  .enforce-the-law-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 31px 0 12px 0;
    .filtrate-condition {
      display: flex;
      align-items: center;
      .select-input {
        margin-right: 20px;
        display: flex;
        align-items: center;
        .select-span {
          font-size: 14px;
          font-family: "Alibaba PuHuiTi";
          font-weight: 400;
          color: #525966;
          line-height: 40px;
          margin-right: 10px;
        }
        ::v-deep .el-select {
          background-color: transparent;
          width: 210px;
          .el-input__inner {
            height: 33px;
          }
          .el-input
            .el-input__suffix
            .el-input__suffix-inner
            .el-icon-arrow-up {
            display: flex;
            align-items: center;
            justify-content: center;
          }
          // border-radius: 2px;
          .el-input
            .el-input__suffix
            .el-input__suffix-inner
            .el-icon-arrow-up::before {
            content: "";
            background: url(../../assets/images/down2.png) center center
              no-repeat;
            background-size: cover;
            position: absolute;
            width: 10px;
            height: 7px;
            // top: 50%;
            // left: 50%;
            // transform: translate(-50%, -50%);
            transform: rotate(180deg);
          }
        }
      }
      .my-checkbox {
        margin-left: 50px;
        ::v-deep .el-checkbox-group {
          .el-checkbox__label {
            font-size: 14px;
            font-family: "Alibaba PuHuiTi";
            font-weight: 400;
            color: #525966;
          }
        }
      }
    }
    .export {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 6px 19px;
      background-color: #2378ec;
      margin-right: 10px;
      cursor: pointer;
      img {
        width: 22px;
        margin-right: 10px;
      }
      span {
        color: #ffffff;
        font-size: 14px;
        font-family: "Alibaba PuHuiTi";
        font-weight: 400;
      }
    }
    .export1 {
      background-color: #28b384;
    }
  }
  .tables {
    margin-top: 10px;
    background-color: #eaf2fd;

    ::v-deep .el-table {
      .success-row {
        background-color: rgba(234, 242, 253, 0.25);
      }
      td {
        height: 42px;
        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;
        .el-checkbox {
          display: none;
        }
      }
    }
    ::v-deep .el-table::before {
      height: 0;
    }
  }
  .pagination {
    margin-top: 40px;
  }
}
.dialog-slot {
  .closeClick {
    position: absolute;
    top: 0;
    right: 0;
    padding: 10px 20px;
    width: 100%;
    z-index: 1000;
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    background: #f8f9fa;
    box-shadow: 0px 1px 0px 0px #dbe0e8;
    background-color: #e0eaf8;
    img {
      width: 18px;
      height: 18px;
      cursor: pointer;
    }
    .newplan {
      display: flex;
      align-items: center;
      .line {
        margin-right: 10px;
        width: 5px;
        height: 16px;
        background-color: #1e70de;
      }
      .span {
        font-size: 16px;
        font-family: "Alibaba PuHuiTi";
        font-weight: bold;
        color: #1e70de;
      }
    }
  }
  .content {
    // padding: 15px 10px;
    .filtrate {
      padding-bottom: 10px;
      .select-input {
        // margin-right: 10px;
        margin-top: 20px;
        display: flex;
        align-items: center;
        .select-span {
          font-size: 13px;
          font-family: "Alibaba PuHuiTi";
          font-weight: 400;
          color: #525966;
          // line-height: 40px;
          margin-right: 10px;
        }
        ::v-deep .el-select,
        .el-cascader {
          background-color: transparent;
          width: 180px;
          .el-input__inner {
            height: 33px;
          }
          // border-radius: 2px;
          .el-input
            .el-input__suffix
            .el-input__suffix-inner
            .el-icon-arrow-up::before {
            content: "";
            background: url(../../assets/images/down2.png) center center
              no-repeat;
            background-size: cover;
            position: absolute;
            width: 10px;
            height: 7px;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
          }
        }
      }
      .select-input {
        width: 45%;
        ::v-deep .el-input {
          .el-input__inner {
            height: 33px;
          }
        }
      }
      .select-btn {
        width: 80px;
        height: 33px;
        background: #2378ec;
        border-radius: 2px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 15px;
        font-family: Alibaba PuHuiTi;
        font-weight: 500;
        color: #ffffff;
        cursor: pointer;
        margin-right: 10px;
      }
      .select-btn1 {
        background: #28b384;
      }
    }
  }
}
::v-deep .el-form-item__label {
  font-size: 14px;
  font-family: "Alibaba PuHuiTi";
  font-weight: 400;
  color: #525966;
  // line-height: 40px;
}
</style>
