<template>
  <div class="app-container zap-main">
    <search-bar
        :checkList="checkList"
        :tableList="tableList"
        :selfDom="selfDom"
        :tableId="tableId"
        @query="handleQuery"
        @reset="resetQuery">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="auto"
      >
        <el-form-item label="企业名称" prop="scyName">
          <el-input
            maxlength="30"
            v-model="queryParams.scyName"
            placeholder="请输入企业名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="社会统一代码" prop="scySocialCode">
          <el-input
            v-model="queryParams.scySocialCode"
            maxlength="30"
            placeholder="请输入社会统一代码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            maxlength="30"
            placeholder="请输入联系人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="userName">
          <el-input
            v-model="queryParams.userName"
            maxlength="11"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="企业状态" prop="scyStatus">
          <el-select v-model="queryParams.scyStatus" placeholder="企业状态" clearable>
            <el-option
              v-for="dict in scyStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </search-bar>

    <el-row class="zap-form zap-margin-top">
      <el-table
        style="margin-top: 20px;"
        v-loading="loading"
        :data="companyList"
        @selection-change="handleSelectionChange"
        stripe
        border
      >
        <el-table-column label="序号" type="index" width="50" align="center">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="企业名称"
          align="center"
          prop="scyName"
          :show-overflow-tooltip="true"
          v-if="uncheckList.scyName"
        />
        <el-table-column
          label="社会统一代码"
          align="center"
          prop="scySocialCode"
          v-if="uncheckList.scySocialCode"
        />
        <el-table-column label="联系人" align="center" prop="nickName" v-if="uncheckList.nickName" />
        <el-table-column label="联系电话" align="center" prop="userName" v-if="uncheckList.userName" />
        <el-table-column
          label="四要素状态"
          align="center"
          prop="scyAuthStatus"
          :formatter="scyAuthStatusFormat"
          v-if="uncheckList.scyAuthStatus"
        />
        <el-table-column
          label="状态"
          align="center"
          prop="scyStatus"
          :formatter="scyStatusFormat"
          v-if="uncheckList.scyStatus"
        />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.scyAuthStatus == '00' || scope.row.scyStatus == '00' || scope.row.scyStatus == '01'"
              class="zap-button-plain"
              size="mini"
              @click="handleDetail(scope.row)"
              v-hasPermi="['service:company:query']"
            >详情</el-button>
            <el-button
              v-if="scope.row.scyStatus == '00' && (scope.row.scyAuthStatus == '00' || scope.row.scyAuthStatus == '02')"
              class="zap-button-primary"
              size="mini"
              @click="handleExamine(scope.row)"
              v-hasPermi="['service:company:elementStatus']"
            >四要素审核</el-button>
            <el-button
              v-if="scope.row.scyAuthStatus == '00'"
              class="zap-button-success"
              size="mini"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['service:company:update']"
            >修改</el-button>
            <el-button
              v-if="scope.row.scyStatus == '00'"
              class="zap-button-confirmation"
              size="mini"
              @click="handleCompanyFrozen(scope.row)"
              v-hasPermi="['service:company:updateCompanyStatus']"
            >冻结</el-button>
            <el-button
              v-if="scope.row.scyStatus == '01'"
              class="zap-button-primary"
              size="mini"
              @click="handleCompanyStatus(scope.row)"
              v-hasPermi="['service:company:updateCompanyStatus']"
            >解冻</el-button>
            <el-button
              v-if="scope.row.scyStatus != '00'"
              class="zap-button-confirmation"
              size="mini"
              @click="handleDelete(scope.row)"
              v-hasPermi="['service:company:delete']"
            >删除</el-button>
            <el-button
              v-if="scope.row.scyStatus == '00'"
              class="zap-button-primary"
              size="mini"
              @click="handleToCore(scope.row)"
              v-hasPermi="['service:company:companyToCore']"
            >改为核心企业</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <!-- 添加或修改公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px" :inline="true">
        <el-form-item label="企业名称" prop="scyName">
          <el-input
            v-model="form.scyName"
            placeholder="请输入企业名称"
            style="width:560px"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="企业统一代码" prop="scySocialCode">
          <el-input
            v-model="form.scySocialCode"
            placeholder="请输入企业统一代码"
            maxlength="18"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="联系人" prop="nickMame">
          <el-input v-model="form.nickMame" placeholder="请输入联系人" maxlength="5" show-word-limit />
        </el-form-item>
        <el-form-item label="联系人手机号" prop="userName">
          <el-input v-model="form.userName" disabled maxlength="11" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情公司对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px" :inline="true">
        <el-form-item label="企业名称" prop="scyName">
          <el-input
            v-model="form.scyName"
            disabled
            style="width:560px"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="企业统一代码" prop="scySocialCode">
          <el-input v-model="form.scySocialCode" disabled maxlength="18" show-word-limit />
        </el-form-item>
        <el-form-item label="联系人" prop="nickName">
          <el-input v-model="form.nickName" disabled maxlength="5" show-word-limit />
        </el-form-item>
        <el-form-item label="联系人手机号" prop="userName">
          <el-input v-model="form.userName" disabled maxlength="11" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 四要素审核 -->
    <el-dialog :title="examTitle" :visible.sync="examOpen" width="400px" append-to-body>
      <el-form
        ref="examForm"
        :model="examForm"
        :rules="examRules"
        label-width="140px"
        :inline="true"
      >
        <template>
          <el-radio v-model="examForm.state" label="03">通过</el-radio>
          <el-radio v-model="examForm.state" label="04">拒绝</el-radio>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="examSubmit">确 定</el-button>
        <el-button @click="examCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompany,
  companyQuery,
  addCompany,
  updateCompany,
  updateCompanyStatus,
  getElementStatus,
  removeCompany,
  companyToCore
} from "@/api/service/company/company";
import { uploadFileNew } from "@/api/common/file";
import { getToken } from "@/utils/auth";
import { columnQuery, columnfilter } from "@/api/common/columnSetting";
import ColumnSetting from "../../../components/Table/columnSetting.vue";
import Cookies from "js-cookie";
export default {
  name: "supCompanyManage",
  components: {
    ColumnSetting
  },
  data() {
    return {
      //四要素审核相关
      examTitle: "",
      examOpen: false,
      examForm: {
        scyId: null,
        state: null
      },
      examRules: {
        state: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司表格数据
      companyList: [],
      scyAuthStatusOptions: [],
      scyStatusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scyName: null,
        scySocialCode: null,
        nickName: null,
        userName: null,
        scyStatus: null,
        scyType: "02" //供应商
      },
      //筛选按钮的数据列表，与table表头的数据一致 --显示隐藏列用
      tableList: [
        {
          label: "scyId",
          value: "企业编号"
        },
        {
          label: "scyName",
          value: "企业名称"
        },
        {
          label: "scySocialCode",
          value: "社会统一代码"
        },
        {
          label: "nickName",
          value: "联系人"
        },
        {
          label: "userName",
          value: "联系电话"
        },
        {
          label: "scyAuthStatus",
          value: "四要素状态"
        },
        {
          label: "scyStatus",
          value: "状态"
        }
      ],
      checkList: [], //筛选列选中的数据列表--显示隐藏列用
      uncheckList: {}, //控制筛选列显示隐藏--显示隐藏列用
      selfDom: this,
      tableId: "/sc-service/company/list",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scyName: [
          {
            required: true,
            message: "企业名称不能为空",
            trigger: ["blur", "change"]
          },
          {
            pattern: /^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$/,
            message: "企业名称不能含有特殊字符",
            trigger: ["blur", "change"]
          }
        ],
        nickName: [
          {
            required: true,
            message: "联系人不能为空",
            trigger: ["blur", "change"]
          },
          {
            pattern: /^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$/,
            message: "联系人不能含有特殊字符",
            trigger: ["blur", "change"]
          }
        ],
        userName: [
          {
            required: true,
            message: "联系人手机号不能为空",
            trigger: ["blur", "change"]
          },
          {
            pattern: /^((\d{3}-\d{7,8}|\d{4}-\d{7,8})|(1[3465789]\d{9}))$/,
            message: "手机号格式不正确",
            trigger: ["blur", "change"]
          }
        ],
        scySocialCode: [
          { required: true, message: "企业统一代码不能为空", trigger: "blur" },
          {
            pattern: /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/,
            message: "企业统一代码格式不正确",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  activated() {
    this.getList();
    this.getDicts("ser_scy_status").then(response => {
      this.scyStatusOptions = response.data;
    });
    this.getDicts("ser_scy_authStatus").then(response => {
      this.scyAuthStatusOptions = response.data;
    });
  },
  mounted() {
    this.columnQuery();
  },
  methods: {
    //获取当前客户是否之前设置过列展示隐藏
    columnQuery() {
      //获取页面路径
      var psfPagePath = window.location.pathname;
      //用请求后台的url作为唯一标识
      var psfTableName = this.tableId;
      var columnForm = {};
      columnForm.psfPagePath = psfPagePath;
      columnForm.psfTableName = psfTableName;
      columnQuery(columnForm).then(response => {
        if (response.data && response.data.psfShowData) {
          this.checkList = response.data.psfShowData;
        }
        this.filter();
      });
    },
    //控制隐藏显示的函数
    filter(checkList) {
      if (!!checkList) {
        this.checkList = checkList;
      }
      columnfilter(this.selfDom);
    },
    /** 查询公司列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      (this.openDetail = false), this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scyName: null,
        scySocialCode: null,
        scyLegal: null,
        scyPhone: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /* 企业详情 */
    handleDetail(row) {
      this.reset();
      this.resetForm("queryForm");
      let scyId = row.scyId || this.ids;
      let scyAuthStatus = row.scyAuthStatus || this.ids;
      let scyStatus = row.scyStatus || this.ids;
      if (scyStatus == "00") {
        Cookies.set(
          "/company/detailComPany/" + scyId + "/",
          this.$route.fullPath
        );
        this.$router.push("/company/detailComPany/" + scyId + "/");
      } else if (scyAuthStatus == "00" || scyStatus == "01") {
        companyQuery(scyId).then(response => {
          this.form = response.data[0];
          this.openDetail = true;
          this.title = "详情企业信息";
        });
      }
    },
    /* 冻结 */
    handleCompanyFrozen(row) {
      const scyId = row.scyId || this.ids;
      const scyName = row.scyName;
      const state = "01";
      this.$confirm('是否确认冻结公司名称为"' + scyName + '"的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return updateCompanyStatus(scyId, state);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("冻结成功");
        })
        .catch(c => {
          if (c == "cancel") {
            this.$message({
              type: "warning",
              message: "已取消冻结"
            });
          }
        });
    },

    /* 解冻 */
    handleCompanyStatus(row) {
      const scyId = row.scyId || this.ids;
      const scyName = row.scyName;
      const state = "00";
      this.$confirm('是否确认解冻公司名称为"' + scyName + '"的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return updateCompanyStatus(scyId, state);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("解冻成功");
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "已取消解冻"
          });
        });
    },

    //四要素审核
    handleExamine(row) {
      this.reset();
      this.resetForm("queryForm");
      const scyId = row.scyId || this.ids;
      Cookies.set(
        "/company/detailComPanyExam/" + scyId + "/",
        this.$route.fullPath
      );
      this.$router.push("/company/detailComPanyExam/" + scyId + "/");
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const scyId = row.scyId || this.ids;
      const scyName = row.scyName;
      this.$confirm('是否确认删除企业名称为"' + scyName + '"的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return removeCompany(scyId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "已取消删除"
          });
        });
    },

    /** 更改为核心企业 */
    handleToCore(row) {
      const scyId = row.scyId || this.ids;
      const scyName = row.scyName;
      this.$confirm(
        '确认把名称为"' + scyName + '"的企业改为核心企业?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return companyToCore(scyId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("更改成功");
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "已取消更改"
          });
        });
    },

    scyStatusFormat(row, column) {
      if (
        row.scyStatus == "02" ||
        row.scyStatus == "03" ||
        row.scyStatus == "04"
      ) {
        return this.selectDictLabel(this.scyStatusOptions, "02");
      } else {
        return this.selectDictLabel(this.scyStatusOptions, row.scyStatus);
      }
    },
    scyAuthStatusFormat(row, column) {
      return this.selectDictLabel(this.scyAuthStatusOptions, row.scyAuthStatus);
    }
  }
};
</script>
