<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
        <el-form-item label="线索创建时间" prop="createTime">
            <el-date-picker ref="dtPicker" clearable size="small" style="width: 250px"
                v-model="dateRange"
                type="daterange"
                value-format="yyyy-MM-dd hh:mm:ss"
                start-placeholder="开始日期" end-placeholder="结束日期"
                :default-time="['00:00:00','23:59:59']">
            </el-date-picker>
         </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="warning"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入线索客户</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> 

      <el-table v-loading="loading" :data="customerLineList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="clue_id" />
        <el-table-column label="进线日期" width="102" align="center" prop="create_time" />
        <!-- <el-table-column label="来源渠道编码" align="center" prop="sourceCode" /> -->
        <el-table-column label="客户名称" align="center" prop="name" />
        <el-table-column label="客户手机" align="center" prop="telephone" />
        <el-table-column  label="进线方式" align="center" prop="clue_type">
            <template slot-scope="scope">
                <el-tag prop="clue_type" :key="'clueType'+index" v-for="(item, index) in thirdClueTypeOptions"    v-if="scope.row.clue_type==item.dictValue">{{item.dictLabel}}</el-tag>
            </template>
        </el-table-column>
       
        <el-table-column label="流量来源" width="90" align="center" >
             <template>
                  {{"字节-抖音"}}
            </template>
        </el-table-column>

       <el-table-column  label="是否导入"   align="center" prop="has_import">
            <template slot-scope="scope">
                <el-tag prop="has_import"  :type="scope.row.has_import==1?'danger':'success'" >{{  scope.row.has_import==1?'已导入':'未导入'}}</el-tag>
            </template>
        </el-table-column>

         <el-table-column label="进线客户链接" width="150" align="center" prop="external_url" />
         <el-table-column label="进线客户提交日期" width="100"  align="center" prop="create_time_detail" />
        <el-table-column label="省市区" align="center" prop="location" width="120" >
            <template slot-scope="scope">
                {{scope.row.location}}
            </template>
       </el-table-column>

       <el-table-column label="广告主名称" width="150" align="center" prop="advertiser_name" />

       <el-table-column label="进线客户详情" align="center" prop="remark" />

     
    <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button
            size="mini"
            type="text"
            @click="handleShow(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCancel(scope.row)"
            v-hasPermi="['crm:customerAssign:cancel']"
          >撤销</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="customer.title" :visible.sync="customer.open" width="1000px" append-to-body>
          <el-table border  :data="customers" >
              <el-table-column label="客户名称" align="center" prop="customerName" />
              <el-table-column label="客户手机号" align="center" prop="mobile" />
          </el-table>
      </el-dialog> -->
    </div>
  </template>

 <script>

  import { getClues,importThird } from "@/api/crm/third";
  export default {
    name: "FeiyuCrmLineList",
    data() {
      return {
        customers:[],
        companys:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户分配记录表格数据
        customerLineList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //飞鱼进线方式
        thirdClueTypeOptions: [],
        dateRange:[],
        //查询参数
        queryParams: {
           pageNum: 1,
           pageSize: 10,
        },
      };
    },
    created() {
        this.getDicts("crm_third_feiyu_clue_type").then(response => {
            this.thirdClueTypeOptions = response.data;
        });
    },
    methods: {
        getData(data){
            this.queryParams=data;
            this.queryParams.pageNum=1;
            this.queryParams.pageSize=20;
            this.dateRange=[];
            this.getList()
        },
        /** 查询飞鱼线索客户列表 */
        getList() {
            this.loading = true;
            let beginDate = new Date();
            let endDate = new Date();
            let startTime="";
            let endTime="";
            if(this.dateRange!=null&&this.dateRange.length==2){
                var dateRange1=this.dateRange[0];
                var dateRange2=this.dateRange[1];
                if(typeof(dateRange1)==Date){
                    beginDate=dateRange1;
                    endDate=dateRange2;
                }else{
                    beginDate=new Date(dateRange1);
                    endDate=new Date(dateRange2);
                }
                startTime=beginDate.getFullYear()+"-"+this.repair(beginDate.getMonth() + 1)+"-"+this.repair(beginDate.getDate());
                endTime=endDate.getFullYear()+"-"+this.repair(endDate.getMonth() + 1)+"-"+this.repair(endDate.getDate());
            }
            else{
                 beginDate.setDate(beginDate.getDate() - 7);
                 startTime=beginDate.getFullYear()+"-"+this.repair(beginDate.getMonth() + 1)+"-"+this.repair(beginDate.getDate());
                 endTime=endDate.getFullYear()+"-"+this.repair(endDate.getMonth() + 1)+"-"+this.repair(endDate.getDate());
                 this.dateRange = [beginDate, endDate]; 
             }
             this.queryParams.startTime=startTime+" 00:00:00";
             this.queryParams.endTime=endTime+" 23:59:59";
             
             getClues(this.queryParams).then(response => {
                  this.customerLineList = response.data.list;
                  this.total = response.data.page_info.total_number;
                  this.loading = false;
             });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids=selection;
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        handleImport(){
              const ids = this.ids;
              let that=this;
              this.$confirm('是否确认批量导入线索客户数据', "警告", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
              }).then(function() {
                  let postData={"crmList":ids,"account":that.queryParams.account};
                  return importThird(postData);
              }).then(() => {
                  this.getList();
                  this.msgSuccess("导入成功");
               }).catch(function() {});
          },
        repair(i){
            if (i >= 0 && i <= 9) {
                return "0" + i;
            } else {
                return i;
            }
        }
    }
  };
</script>

<style lang="scss" scoped>
    .app-container{
        padding-top: 0;
        margin-top: 0;
        padding-left: 0;
        padding-right: 0;
    }
   .el-form-item {
        margin-bottom: 10px;
   }   
</style>