<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cs:csExpandInteract:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['cs:csExpandInteract:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['cs:csExpandInteract:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cs:csExpandInteract:export']">导出</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['cs:csExpandInteract:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-download" size="mini" @click="handleExportTemplate"
          v-hasPermi="['cs:csExpandInteract:export']">导出模板</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleSearch">高级搜索</el-button>
      </el-col>
      <el-col :span="8">
        <span style="padding-right: 10px;">表格设置</span>
        <el-select v-model="tableHeaderList" multiple collapse-tags placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div v-if="showSearch" ref="showSearch" class="searchForm">

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="关键词">
          <el-input placeholder="请输入关键词" v-model="queryParams.keyword" />
        </el-form-item>
        <el-form-item label="交流时间" prop="accountingTime">
          <el-date-picker clearable v-model="queryParams.accountingTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择交流时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交流阶段" prop="exchangeClassId">
          <el-input v-model="queryParams.exchangeClassId" placeholder="请输入交流阶段" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="对接人" prop="companyContacts">
          <el-input v-model="queryParams.companyContacts" placeholder="请输入对接人" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="单位属性" prop="companyClassId">
          <el-input v-model="queryParams.companyClassId" placeholder="请输入单位属性" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="生态领域" prop="ecologicalField">
          <el-input v-model="queryParams.ecologicalField" placeholder="请输入生态领域" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="参与人员" prop="participant">
          <el-input v-model="queryParams.participant" placeholder="请输入参与人员" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="记录人" prop="recordPerson">
          <el-input v-model="queryParams.recordPerson" placeholder="请输入记录人" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="提交时间" prop="dateRange">
          <el-date-picker clearable v-model="queryParams.dateRange" type="daterange" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" 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>
    </div>
    <el-table v-loading="loading" :data="csExpandInteractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sort">
        <template slot-scope="scope">
          {{ ((queryParams.pageNum - 1) * queryParams.pageSize) + (scope.$index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="交流时间" align="center" prop="accountingTime" min-width="150"
        v-if="tableHeaderList.indexOf('accountingTime') != -1">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accountingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center" prop="companyNameStr"
        v-if="tableHeaderList.indexOf('companyName') != -1" />
      <el-table-column label="单位属性" align="center" prop="companyClass"
        v-if="tableHeaderList.indexOf('companyClass') != -1" />
      <el-table-column label="中心对接人" width="150" align="center" prop="centerLiaisonName"
        v-if="tableHeaderList.indexOf('centerLiaison') != -1" />
      <el-table-column label="参与人员" align="center" prop="participant"
        v-if="tableHeaderList.indexOf('participant') != -1" />
      <el-table-column label="交流内容" align="center" prop="exchangeContent"
        v-if="tableHeaderList.indexOf('exchangeContent') != -1">
        <template slot-scope="{row}">
          <div v-html="row.exchangeContent"></div>
        </template>
      </el-table-column> 
      <el-table-column label="下一步计划" align="center" width="150" prop="planOrTime"
        v-if="tableHeaderList.indexOf('planOrTime') != -1" />
      <el-table-column label="用户需求类型" align="center" prop="userNeedClass"
        v-if="tableHeaderList.indexOf('userNeedClass') != -1" />
      <!--   <el-table-column label="交流阶段" align="center" prop="exchangeClass"
        v-if="tableHeaderList.indexOf('exchangeClass') != -1" />
      <el-table-column label="单位联系人" align="center" prop="companyContacts"
        v-if="tableHeaderList.indexOf('companyContacts') != -1" />
      <el-table-column label="生态领域" align="center" prop="ecologicalField"
        v-if="tableHeaderList.indexOf('ecologicalField') != -1" />
      <el-table-column label="用户信息简介(产品和服务介绍)" align="center" prop="introduce"
        v-if="tableHeaderList.indexOf('introduce') != -1" />
      <el-table-column label="合作模式" align="center" prop="cooperateMode"
        v-if="tableHeaderList.indexOf('cooperateMode') != -1" />
      <el-table-column label="记录人" align="center" prop="recordPerson"
        v-if="tableHeaderList.indexOf('recordPerson') != -1" />
      <el-table-column label="交流凭证" align="center" prop="exchangeVoucher"
        v-if="tableHeaderList.indexOf('exchangeVoucher') != -1" /> -->
      <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="['cs:csExpandInteract:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cs:csExpandInteract: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" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="交流时间" prop="accountingTime">
          <el-date-picker clearable v-model="form.accountingTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择交流时间" style="width: 100%;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-cascader v-model="form.companyName" :options="csCompanyNameList"
            :props="{ value: 'dictCode', label: 'dictLabel' }" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="单位类别" prop="companyClassId">
          <el-select v-model="form.companyClassId" placeholder="请选择单位类别" style="width: 100%;">
            <el-option v-for="dict in dict.type.company_class" :key="dict.value" :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="中心对接人" prop="centerLiaisonId">
          <el-select v-model="form.centerLiaisonId" placeholder="中心对接人" style="width: 100%;">
            <el-option v-for="(item, index) in sysUserList" :key="index" :label="item.name" :value="JSON.stringify(item.userId)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与人员" prop="participant">
          <el-input v-model="form.participant" placeholder="请输入参与人员" />
        </el-form-item>
        <el-form-item label="交流阶段" prop="exchangeClassId">
          <el-select v-model="form.exchangeClassId" placeholder="交流阶段" style="width: 100%;">
            <el-option v-for="(item, index) in exchangeClassList" :key="index" :label="item.className"
              :value="item.classId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交流内容">
          <editor v-model="form.exchangeContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="下一步计划及截止时间" prop="planOrTime">
          <el-date-picker clearable v-model="form.planOrTime" type="date" value-format="yyyy-MM-dd"
            placeholder="下一步计划及截止时间" style="width: 100%;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户需求类型" prop="userNeedClassId">
          <el-select v-model="form.userNeedClassId" placeholder="用户需求类型" style="width: 100%;">
            <el-option v-for="(item, index) in userNeedClassList" :key="index" :label="item.className"
              :value="item.classId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位联系人" prop="companyContacts">
          <el-input v-model="form.companyContacts" placeholder="请输入单位联系人" />
        </el-form-item>

        <el-form-item label="生态领域" prop="ecologicalField">
          <el-input v-model="form.ecologicalField" placeholder="请输入生态领域" />
        </el-form-item>



        <el-form-item label="合作模式" prop="cooperateModeId">
          <el-select v-model="form.cooperateModeId" placeholder="合作模式" style="width: 100%;">
            <el-option v-for="(item, index) in cooperateModeList" :key="index" :label="item.className"
              :value="item.classId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户信息简介" prop="introduce">
          <el-input v-model="form.introduce" placeholder="请输入用户信息简介" />
        </el-form-item>


        <el-form-item label="交流凭证" prop="exchangeVoucher">
          <!-- <el-input v-model="form.exchangeVoucher" placeholder="请输入交流凭证" /> -->
          <ImageUpload :value="form.exchangeVoucher" :isShowTip="false" :limit="1" />
        </el-form-item>
        <el-form-item label="记录人" prop="recordPerson">
          <el-input v-model="form.recordPerson" placeholder="请输入记录人" />
        </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="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 + '?updateSupport=' + upload.updateSupport" :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">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
        </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 { getToken } from "@/utils/auth";
  import {
    listCsExpandInteract,
    getSysUserList,
    getCsExpandInteract,
    delCsExpandInteract,
    addCsExpandInteract,
    updateCsExpandInteract
  } from "@/api/cs/csExpandInteract";
  import { getClassListByMark } from "@/api/cs/csResourceOrderApply";
  import { listDataTree } from "@/api/system/dict/data";
  export default {
    name: "CsExpandInteract",
    dicts: ['company_class'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 拓展交流表格数据
        csExpandInteractList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          keyword: '',
          dateRange: [],
          resId: null,
          resName: null,
          resType: null,
          resourceClassId: null,
          resourceClass: null,
          accountingTime: null,
          exchangeClassId: null,
          exchangeClass: null,
          companyName: null,
          deptId: null,
          deptName: null,
          companyContacts: null,
          companyClassId: null,
          companyClass: null,
          ecologicalField: null,
          centerLiaisonId: null,
          centerLiaison: null,
          participant: null,
          userNeedClassId: null,
          userNeedClass: null,
          cooperateModeId: null,
          cooperateMode: null,
          introduce: null,
          exchangeContent: null,
          planOrTime: null,
          exchangeVoucher: null,
          annexUrl: null,
          recordPerson: null,
          remarks: null,
          content: null,
          status: null,
          sort: null,
          createAt: null,
          updateAt: null,
          delFlag: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        options: [{
          value: 'accountingTime',
          label: '交流时间'
        }, {
          value: 'exchangeClass',
          label: '交流阶段'
        }, {
          value: 'companyName',
          label: '单位名称'
        }, {
          value: 'companyContacts',
          label: '单位联系人'
        }, {
          value: 'companyClass',
          label: '单位属性'
        }, {
          value: 'ecologicalField',
          label: '生态领域'
        }, {
          value: 'centerLiaison',
          label: '中心对接人'
        }, {
          value: 'participant',
          label: '参与人员'
        }, {
          value: 'introduce',
          label: '用户信息简介'
        }, {
          value: 'exchangeContent',
          label: '交流内容'
        }, {
          value: 'planOrTime',
          label: '下一步计划及截止时间'
        }, {
          value: 'userNeedClass',
          label: '用户需求类型'
        }, {
          value: 'cooperateMode',
          label: '合作模式'
        }, {
          value: 'recordPerson',
          label: '记录人'
        }, {
          value: 'exchangeVoucher',
          label: '交流凭证'
        }, ],
        tableHeaderList: ['accountingTime', 'exchangeClass', 'companyName',
          'companyContacts', 'companyClass', 'ecologicalField', 'centerLiaison',
          'participant', 'introduce', 'exchangeContent',
          'planOrTime', 'userNeedClass', 'cooperateMode', 'recordPerson', 'exchangeVoucher'
        ],
        exchangeClassList: [],
        companyClassList: [],
        userNeedClassList: [],
        cooperateModeList: [],
        csCompanyNameList: [],
        sysUserList: [],
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: false,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/cs/csExpandInteract/importCsExpandInteract"
        },
      };
    },
    created() {
      this.getList();
      this.getDictList()
    },
    mounted() {
      // document.addEventListener(
      //   "click",
      //   (e) => {
      //     let showSearch = this.$refs.showSearch;
      //     if (!showSearch.contains(e.target)) {
      //       this.showSearch = false;
      //     }
      //   },
      //   true
      // );
    },
    methods: {
      //     交流阶段类别 exchangeClass
      // 单位类别 company_class
      // 用户需求类型 userNeedClass
      // 合作模式 cooperateMode
      // 单位名称 cs_company_name
      getDictList() {
        getClassListByMark({ classMark: 'exchangeClass' }).then(res => {
          this.exchangeClassList = res.data
        })
        getClassListByMark({ classMark: 'company_class' }).then(res => {
          this.companyClassList = res.data
        })
        getClassListByMark({ classMark: 'userNeedClass' }).then(res => {
          this.userNeedClassList = res.data
        })
        getClassListByMark({ classMark: 'cooperateMode' }).then(res => {
          this.cooperateModeList = res.data
        })
        listDataTree({ dictType: 'cs_company_name' }).then(res => {
          this.csCompanyNameList = res.rows
        })
        getSysUserList().then(res => {
          this.sysUserList = res.data
        })
        // getClassListByMark({classMark:'exchangeClass'}).then(res=>{
        //   this.exchangeClassList = res.data
        // })
      },
      /** 查询拓展交流列表 */
      getList() {
        this.loading = true;
        if (null != this.queryParams.dateRange && '' != this.queryParams.dateRange) {
          this.queryParams["beginCreateAt"] = this.queryParams.dateRange[0];
          this.queryParams["endCreateAt"] = this.queryParams.dateRange[1];
        }
        listCsExpandInteract(this.queryParams).then(response => {
          this.csExpandInteractList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleSearch() {
        this.showSearch = !this.showSearch
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          expandInteractId: null,
          resId: null,
          resName: null,
          resType: null,
          resourceClassId: null,
          resourceClass: null,
          accountingTime: null,
          exchangeClassId: null,
          exchangeClass: null,
          companyName: null,
          deptId: null,
          deptName: null,
          companyContacts: null,
          companyClassId: null,
          companyClass: null,
          ecologicalField: null,
          centerLiaisonId: null,
          centerLiaison: null,
          participant: null,
          userNeedClassId: null,
          userNeedClass: null,
          cooperateModeId: null,
          cooperateMode: null,
          introduce: null,
          exchangeContent: null,
          planOrTime: null,
          exchangeVoucher: null,
          annexUrl: null,
          recordPerson: null,
          remarks: null,
          content: null,
          status: "0",
          sort: null,
          createAt: null,
          updateAt: null,
          delFlag: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.showSearch = false
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams = {
            pageNum: 1,
            pageSize: 10,
            keyword: '',
            dateRange: [],
            resId: null,
            resName: null,
            resType: null,
            resourceClassId: null,
            resourceClass: null,
            accountingTime: null,
            exchangeClassId: null,
            exchangeClass: null,
            companyName: null,
            deptId: null,
            deptName: null,
            companyContacts: null,
            companyClassId: null,
            companyClass: null,
            ecologicalField: null,
            centerLiaisonId: null,
            centerLiaison: null,
            participant: null,
            userNeedClassId: null,
            userNeedClass: null,
            cooperateModeId: null,
            cooperateMode: null,
            introduce: null,
            exchangeContent: null,
            planOrTime: null,
            exchangeVoucher: null,
            annexUrl: null,
            recordPerson: null,
            remarks: null,
            content: null,
          },
          this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.expandInteractId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加拓展交流";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const expandInteractId = row.expandInteractId || this.ids
        getCsExpandInteract(expandInteractId).then(response => {
          this.form = response.data;
          var companyNameArr = [];
          companyNameArr.push(parseInt(this.form.companyName));
          companyNameArr.push(parseInt(this.form.deptId));
          this.form.companyName = companyNameArr;
          this.open = true;
          this.title = "修改拓展交流";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.form.deptId = this.form.companyName[1]
        this.form.companyName = this.form.companyName[0]
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.expandInteractId != null) {
              updateCsExpandInteract(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCsExpandInteract(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const expandInteractIds = row.expandInteractId || this.ids;
        this.$modal.confirm('是否确认删除拓展交流编号为"' + expandInteractIds + '"的数据项？').then(function() {
          return delCsExpandInteract(expandInteractIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导入按钮操作 */
      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();
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('cs/csExpandInteract/export', {
          ...this.queryParams
        }, `拓展交流_${new Date().getTime()}.xlsx`)
      },
      handleExportTemplate() {
        this.download('/cs/csExpandInteract/exportTemplate', {
          ...this.queryParams
        }, `拓展交流模板_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
<style scoped>
  ::v-deep .dialog1 {
    background-color: #F8F8F8;
  }

  .searchForm {
    width: 650px;
    padding: 20px;
    position: absolute;
    left: 200px;
    z-index: 100;
    background-color: #FFFFFF;
    border: 1px solid #777777;
  }
</style>
