<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名片标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入名片标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item  label="名片类型" prop="type">
            <el-select filterable v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
            <el-option
                    v-for="item in typeOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
            </el-select>
      </el-form-item>

      <!-- <el-form-item label="名片访问次数" prop="lookNum">
        <el-input
          v-model="queryParams.lookNum"
          placeholder="请输入名片访问次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名片访问人数" prop="personNum">
        <el-input
          v-model="queryParams.personNum"
          placeholder="请输入名片访问人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="['company:companUsercard: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="['company:companUsercard: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="['company:companUsercard:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['company:companUsercard:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="55" label="Id" align="center" prop="id" />
      <el-table-column width="60" label="用户ID" align="center" prop="userId" />
      <el-table-column label="名片标题" align="center" prop="title" />

      <el-table-column label="名片图片" align="center" width="120">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img slot="reference" :src="scope.row.imageUrl" width="100">
            <img :src="scope.row.imageUrl" style="max-width: 150px;">
          </el-popover>
        </template>
      </el-table-column>
     
      <el-table-column label="名片短链接" width="100" align="center" prop="shortUrl" />

      <el-table-column label="创建时间" width="100" align="center" prop="createTime" />

      
     <el-table-column  label="名片类型"  align="center" prop="type">
        <template slot-scope="scope">
          <el-tag prop="type" v-for="(item, index) in typeOptions"    v-if="scope.row.type==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="是否访问" align="center" prop="isVisit" /> -->
      <el-table-column label="访问次数"  width="110"  align="center" prop="lookNum" />
      <!-- <el-table-column label="名片访问人数" align="center" prop="personNum" />
      <el-table-column label="名片访问ip数" align="center" prop="ipNum" /> -->
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sms"
            @click="handleSms(scope.row)"
          >发短信</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['company:companUsercard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:companUsercard: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"
    />

    <!-- 添加或修改card对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="名片标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入名片标题" />
        </el-form-item>
         <el-form-item label="名片类型" prop="type">
            <el-select style="width: 200px" v-model="form.type" placeholder="请选择类型" clearable size="small" >
              <el-option
                      v-for="item in typeOptions"
                      :key="item.dictValue"
                      :label="item.dictLabel"
                      :value="item.dictValue"
                    />
            </el-select>
        </el-form-item>

       <el-form-item label="名片图片" prop="imageUrl">
            <el-upload
              v-model="form.icon"
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>

       
        <!-- <el-form-item label="微信名片短链接" prop="shortUrl">
          <el-input v-model="form.shortUrl" placeholder="请输入微信名片短链接" />
        </el-form-item>
        <el-form-item label="是否访问" prop="isVisit">
          <el-input v-model="form.isVisit" placeholder="请输入微信名片短链接" />
        </el-form-item> -->
        <el-form-item label="名片访问次数"  prop="lookNum">
          <el-input v-model="form.lookNum" style="width:200px" placeholder="请输入名片访问次数" />
        </el-form-item>
        <!-- <el-form-item label="名片访问人数" prop="personNum">
          <el-input v-model="form.personNum" placeholder="请输入名片访问人数" />
        </el-form-item>
        <el-form-item label="名片访问ip数" prop="ipNum">
          <el-input v-model="form.ipNum" placeholder="请输入名片访问ip数" />
        </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="addSms.title" :visible.sync="addSms.open" width="800px" append-to-body>
        <add-sms ref="sms" @close="closeSms()" @handleAddCustomer="handleAddCustomer()"  ></add-sms>
    </el-dialog>

     <el-dialog :title="customer.title" v-if="customer.open"  :visible.sync="customer.open" width="900px" append-to-body>
        <customer-select  ref="customer"  @selectCustomer="selectCustomer" />
    </el-dialog>

  </div>
</template>

<script>
import { listCard, getCard, delCard, addCard, updateCard, exportCard } from "@/api/company/companyUserCard";
import addSms from './addSms.vue';
import customerSelect from './customerSelect.vue';
export default {
  name: "companyUserCard",
  components:{ addSms,customerSelect},
  watch: {
    imageArr: function(val) {
      this.form.imageUrl = val.join(',')
    },
  },
  data() {
    return {
      uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // card表格数据
      cardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      imageArr:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userId: null,
        urlScheme: null,
        companyId: null,
        imageUrl: null,
        type: null,
        shortUrl: null,
        isVisit: null,
        lookNum: null,
        personNum: null,
        ipNum: null,
      },
      
      typeOptions:[],
      addSms:{
         open:false,
         title:"发短信"
      },
      customer:{
         open:false,
         title:"选择客户"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("company_user_card_type").then((response) => {
        this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询card列表 */
    getList() {
      this.loading = true;
      listCard(this.queryParams).then(response => {
        this.cardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        userId: null,
        urlScheme: null,
        companyId: null,
        imageUrl: null,
        type: "0",
        shortUrl: null,
        isVisit: null,
        lookNum: null,
        personNum: null,
        ipNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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
      getCard(id).then(response => {
         this.form = response.data;
         if(this.form.type!=null){
              this.form.type = this.form.type.toString();
         }
        this.open = true;
        this.title = "修改个人名片";
      });
    },
     /** 提交按钮 */
     submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCard(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCard(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 delCard(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
     },
     /** 导出按钮操作 */
     handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCard(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
     },
     handleSms(item){
        this.addSms.open=true;
        var that=this;
        setTimeout(() => {
            that.$refs.sms.reset(item.shortUrl);
        }, 500);
     },
     closeSms(){
          this.addSms.open=false;
     },
     selectCustomer(row){
        this.$refs.sms.selectCustomer(row);
     },
     handleAddCustomer(){
         this.customer.open=true; 
     },
    handleAvatarSuccess(res, file) {
        if(res.code==200){
          this.form.imageUrl=res.url;
        }
        else{
          this.msgError(res.msg);
        }
    },
    beforeAvatarUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (!isLt1M) {
        this.$message.error('上传图片大小不能超过 1MB!');
      }
      return  isLt1M;
    },

  }
};
</script>
