<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="所属公司" prop="companyId">
                <el-select filterable v-model="queryParams.companyId" @change="companyChange" placeholder="请选择所属公司" clearable size="small">
                    <el-option
                      v-for="item in companys"
                      :key="item.companyId"
                      :label="item.companyName"
                      :value="item.companyId"
                    />
              </el-select>
       </el-form-item>

      <el-form-item label="主体名称" prop="corpName">
        <el-input
          v-model="queryParams.corpName"
          placeholder="请输入主体名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="主体状态" prop="status">
          <el-select filterable  v-model="queryParams.status" placeholder="请选择" clearable size="small">
              <el-option
                v-for="item in statusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qywx:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qywx:company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qywx:company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['qywx:company:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="企业id" align="center" prop="corpId" />

      <el-table-column label="企业名称" width="160" align="center" prop="corpName" />
      <el-table-column label="企业全称" width="160" align="center" prop="corpFullName" />
      <el-table-column label="所属公司id" align="center" prop="companyId" />
      <el-table-column label="所属公司" align="center" prop="companyName" />


      <!-- <el-table-column label="企业类型" align="center" prop="subjectType" /> -->
      <!-- <el-table-column label="认证到期时间" align="center" prop="verifiedEndTime" /> -->
      <el-table-column label="授权应用" align="center" prop="agentName" />

       <el-table-column label="企业永久授权码" align="center" prop="permanentCode" />


      <el-table-column label="accessToken" width="240px" align="center" prop="accessToken" />
      <!-- <el-table-column label="token到期时间(秒)" align="center" prop="expiresIn" /> -->

      <el-table-column label="主体状态" align="center" prop="status" >
          <template slot-scope="scope" >
              <el-tag prop="status" v-for="(item, index) in statusOptions"  :type="scope.row.status==0?'danger':'success'"  v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qywx:company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qywx:company:remove']"
          >删除</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-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">

        <el-form-item label="企业名称" prop="corpName">
          <el-input v-model="form.corpName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业全称" prop="corpFullName">
          <el-input v-model="form.corpFullName" placeholder="请输入企业全称" />
        </el-form-item>

        <!-- <el-form-item label="企业类型" prop="subjectType">
          <el-select v-model="form.subjectType" placeholder="请选择企业类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item> -->

        <!-- <el-form-item label="授权应用" prop="agentId">
            <el-select disabled  v-model="form.agentId"  clearable size="small">
                <el-option
                  v-for="item in appList"
                  :key="item.agentId"
                  :label="item.name"
                  :value="item.agentId"/>
          </el-select>
        </el-form-item> -->

        <el-form-item label="销售公司" prop="companyId">
            <el-select filterable  v-model="form.companyId" placeholder="请选择公司名" clearable size="small">
                <el-option
                  v-for="item in companys"
                  :key="item.companyId"
                  :label="item.companyName"
                  :value="item.companyId"
                />
          </el-select>
        </el-form-item>

         <el-form-item label="会话存档私钥" prop="docPrivateKey">
          <el-input v-model="form.docPrivateKey" rows="10"  type="textarea" />
        </el-form-item>

        <el-form-item label="会话存档公钥" prop="docPublicKey">
          <el-input v-model="form.docPublicKey"  rows="10" type="textarea" />
        </el-form-item>

        <el-form-item label="会话存档密钥" prop="docPrivateSecret">
          <el-input v-model="form.docPrivateSecret"  type="text" />
        </el-form-item>

         <el-form-item label="企业永久授权码" prop="permanentCode">
          <el-input readonly  v-model="form.permanentCode" type="text" placeholder="请输入企业永久授权码" />
        </el-form-item>

        <el-form-item label="accessToken" prop="accessToken">
          <el-input v-model="form.accessToken" readonly type="textarea" />
        </el-form-item>


<!--
        <el-form-item label="token超时时间(秒)" prop="expiresIn">
          <el-input v-model="form.expiresIn" placeholder="请输入token超时时间(秒)" />
        </el-form-item> -->
        <el-form-item label="账户状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择账户状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </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>
  </div>
</template>

<script>
import { listCompany, getCompany, delCompany, addCompany, updateCompany, exportCompany } from "@/api/qywx/company";
import { getCompanyList } from "@/api/company/company";
import { listApp } from "@/api/qywx/app";

export default {
  name: "Company",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业微信三方应用授权公司表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 账户状态字典
      statusOptions: [],
        companys:[],
        appList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        permanentCode: null,
        corpName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        corpId: [
          { required: true, message: "企业id不能为空", trigger: "blur" }
        ],
        // permanentCode: [
        //   { required: true, message: "企业永久授权码不能为空", trigger: "blur" }
        // ],

        companyId: [
          { required: true, message: "请选择销售公司", trigger: "blur" }
        ],
        corpName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        corpFullName: [
          { required: true, message: "企业全称不能为空", trigger: "blur" }
        ],
        docPrivateKey: [
          { required: true, message: "会话存档私钥不能为空", trigger: "blur" }
        ],
        // docPublicKey: [
        //   { required: true, message: "会话存档公钥不能为空", trigger: "blur" }
        // ],
        docPrivateSecret: [
          { required: true, message: "会话存档密钥不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

    this.getDicts("qywx_company_status").then(response => {
      this.statusOptions = response.data;
    });
    getCompanyList().then(response => {
      this.companys = response.data;
    });
    this.getAppList();
  },
  methods: {
    /** 查询企业微信三方应用授权公司列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
   getAppList() {
      listApp(this.queryParams).then(response => {
        this.appList = response.data;
      });
    },

    // 账户状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        corpId: null,
        permanentCode: null,
        corpName: null,
        corpFullName: null,
        subjectType: null,
        verifiedEndTime: null,
        agentId: null,
        companyId: null,
        accessToken: null,
        expiresIn: null,
        status: null,
        createTime: null,
        updateTime: null,
        docPrivateKey: null,
        docPublicKey: null,
        docPrivateSecret: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业微信公司主体";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业微信公司主体";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompany(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCompany(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企业微信三方应用授权公司编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompany(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信三方应用授权公司数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompany(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
