<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input placeholder="请输入姓名、家长姓名、联系方式、备注" v-model="queryParam.keywords"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="录入日期">
                <a-range-picker v-model="queryParam.dateQ"
                  format="YYYY-MM-DD"
                  :placeholder="['开始日期', '结束日期']"
                  @change="onCreateTimeChange" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="学员类型">
                <!-- <j-dict-select-tag placeholder="请选择类型"  v-model="queryParam.type" dictCode="customer_pc_type"/> -->
                <a-select  v-model="selectValue" @change="handleInput">
                  <a-select-option value= 0 >全部</a-select-option>
                  <a-select-option v-for="(item, key) in typeOptions" :key="key" :value="item.value">
                    <span style="display: inline-block;width: 100%" :title=" item.text || item.label ">
                      {{ item.text || item.label }}
                    </span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          <template v-if="toggleSearchStatus">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="customer_pc_status"/>
              </a-form-item>
            </a-col>
           <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="校区ID">
              <j-dict-select-tag placeholder="请选择校区ID" v-model="queryParam.schoolDistrictId" dictCode="school_organization,name,id"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="采单员">
                <j-select-user-by-dep placeholder="请选择采单员" v-model="queryParam.collectinoCode"/>
              </a-form-item>
            </a-col>
            
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="学校">
                <j-dict-select-tag placeholder="请选择学校" v-model="queryParam.school" dictCode="school_school,name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="年级">
                <j-dict-select-tag placeholder="请选择年级" v-model="queryParam.grade" dictCode="customer_grade"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="渠道类型">
               <j-tree-select
                ref="treeSelect"
                placeholder="请选择渠道类型"
                v-model="queryParam.channelType"
                dict="promotion_channel,name,id"
                pidValue="0"
                >
              </j-tree-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="意向课程">
                <j-dict-select-tag placeholder="请选择意向课程" v-model="queryParam.intendedCourse" dictCode="education_course,name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="性别">
                <j-dict-select-tag placeholder="请选择性别" v-model="queryParam.sex" dictCode="sex"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <a-button type="primary" icon="download" @click="handleExportXls('公池管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- <a-button type="primary" icon="plus" @click="lookTest()">组件Test</a-button> -->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
      <!-- :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" 列表选择器 -->
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
           <a @click="handleDetail(record)">详情</a>
           <a-divider type="vertical" />
            <!-- <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm> -->
             <a @click="handleUserLog(record)" >操作记录</a>
           <a-divider type="vertical" v-has="'user:appoint'" />
           <a @click="handleAppoint(record)" v-has="'user:appoint'">顾问分配</a>
        </span>

      </a-table>
    </div>
      <a-modal
        title="分配顾问"
        ok-text="确认" 
        cancel-text="取消"
        :visible="dialogPvVisible"
        :confirm-loading="dialogloading"
        @ok="appointOk"
        @cancel="appointCancel">
        <a-form-item label="顾问选择" style="width: 300px">
          <j-select-user-by-dep :multi=false v-model="users"/>
          {{ getFormFieldValue('users') }}
        </a-form-item>
      </a-modal>
    <customer-public-modal ref="modalForm" @ok="modalFormOk"></customer-public-modal>
    <!-- 自定义组件调用 -->
    <CustomerLogs :show-child-dialog.sync="showCustomerLogs" :logInfo="logInfo" /> 

  </a-card>
</template>

<script>
  import {getDictItemsFromCache} from '@/api/api'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CustomerPublicModal from './modules/CustomerPublicModal'
  import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
  import {postAction} from '@/api/manage'
  import CustomerLogs from '../modules/CustomerLogs.vue'
  import JEllipsis from "@/components/jeecg/JEllipsis"

  export default {
    name: 'CustomerPublicList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CustomerPublicModal,JSelectMultiUser,JEllipsis,CustomerLogs
    },
    data () {
      let ellipsis = (v, l = 20) => (<j-ellipsis value = {v} length = {l}/>)
      return {
        description: '公池管理管理页面',
        appointShow: false,
        dialogPvVisible: false,
        dialogloading: false,
        form: this.$form.createForm(this),
        appointRowID: '',
        schoolDistrictId_dictText: "",
        saleCode_dictText: "",
        users:"",
        queryParam: {
          type: "1"
        },
        selectValue: "1",
        userValue:"",
        userDisplay:"",
        showCustomerLogs: false,
        logInfo: {},
        // 表头
        columns: [
          // 序号
          // {
          //   title: '#',
          //   dataIndex: '',
          //   key:'rowIndex',
          //   width:60,
          //   align:"center",
          //   customRender:function (t,r,index) {
          //     return parseInt(index)+1;
          //   }
          // },
          {
            title:'校区ID',
            align:"center",
            dataIndex: 'schoolDistrictId_dictText'
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'name'
          },
          // {
          //   title:'性别',
          //   align:"center",
          //   dataIndex: 'sex_dictText'
          // },
          {
            title:'家长电话',
            align:"center",
            dataIndex: 'contactPhone'
          },
          // {
          //   title:'学校',
          //   align:"center",
          //   dataIndex: 'school_dictText'
          // },
          // {
          //   title:'年级',
          //   align:"center",
          //   dataIndex: 'grade_dictText'
          // },
          // {
          //   title:'渠道类型',
          //   align:"center",
          //   dataIndex: 'channelType_dictText'
          // },
          // {
          //   title:'意向课程',
          //   align:"center",
          //   dataIndex: 'intendedCourse_dictText'
          // },
          {
            title:'顾问',
            align:"center",
            dataIndex: 'saleCode_dictText',
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'markString',
            customRender: (text) => {
              return ellipsis(text);
            }
          },
          // {
          //   title:'推荐人',
          //   align:"center",
          //   dataIndex: 'referencesPhone'
          // },
          {
            title:'采单员',
            align:"center",
            dataIndex: 'collectinoCode_dictText'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'操作日期',
            align:"center",
            dataIndex: 'updateTime'
          },
          // {
          //   title:'录入时间',
          //   align:"center",
          //   dataIndex: 'createTime'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/customer/customerInfo/list",
          delete: "/customer/customerInfo/delete",
          deleteBatch: "/customer/customerInfo/deleteBatch",
          exportXlsUrl: "/customer/customerInfo/exportXls",
          importExcelUrl: "customer/customerInfo/importExcel",
        },
        dictOptions:{},
        superFieldList:[],
        typeOptions:[]
      }
    },
    created() {
      this.getSuperFieldList();
      this.typeOptions = getDictItemsFromCache('customer_pc_type');
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'schoolDistrictId',text:'校区ID',dictCode:"school_organization,name,id"})
        fieldList.push({type:'string',value:'name',text:'姓名',dictCode:''})
        fieldList.push({type:'int',value:'sex',text:'性别',dictCode:'sex'})
        fieldList.push({type:'string',value:'contactPhone',text:'家长电话',dictCode:''})
        fieldList.push({type:'string',value:'school',text:'学校',dictCode:"school_school,name,id"})
        fieldList.push({type:'string',value:'grade',text:'年级',dictCode:'customer_grade'})
        fieldList.push({type:'string',value:'channelType',text:'渠道类型'})
        fieldList.push({type:'string',value:'intendedCourse',text:'意向课程',dictCode:"education_course,name,id"})
        fieldList.push({type:'sel_user',value:'saleCode',text:'顾问'})
        fieldList.push({type:'string',value:'referencesPhone',text:'推荐人',dictCode:''})
        fieldList.push({type:'sel_user',value:'collectinoCode',text:'采单员'})
        fieldList.push({type:'int',value:'status',text:'状态',dictCode:'customer_pc_status'})
        fieldList.push({type:'date',value:'createTime',text:'创建日期'})
        this.superFieldList = fieldList
      },
      handleAppoint(row) {
        this.dialogPvVisible = true;
        this.appointShow = true;
        this.appointRowID = row.id;
        this.users = row.saleCode;
        this.schoolDistrictId_dictText = row.schoolDistrictId_dictText;
        this.saleCode_dictText = row.saleCode_dictText;
      },
      appointOk() {
        this.dialogloading = true;
        let params = {
          customerInfo: {
            id: this.appointRowID,
            saleCode: this.users,
            status: 1,
            follow_status: 1,
            type: 2
          },
          appointInfo: {
            saleName: this.saleCode_dictText,
            schoolName: this.schoolDistrictId_dictText
          }
        }
        postAction("/customer/customerInfo/appoint", params).then((res) => {
          if (res.success) {
            this.$emit('ok');
            this.$message.success(res.message);
          } else {
            this.$message.warn('分配失败:' + res.message);
          }
        }).finally(() => {
          this.dialogloading = false;
          this.dialogPvVisible = false;
          this.loadData();
        });
      },
      appointCancel() {
        this.users = "";
        this.appointRowID = "";
        this.dialogPvVisible = false;
        this.schoolDistrictId_dictText = "";
        this.saleCode_dictText  = "";
      },
      getFormFieldValue(field){
        return this.form.getFieldValue(field)
      },
      onCreateTimeChange: function (value, dateString) {
        this.queryParam.beginTime=dateString[0];
        this.queryParam.endTime=dateString[1];
      },
      testP() {
        console.log('子组件调用!');
      },
      handleUserLog(row) {
        this.showCustomerLogs  = true;
        this.logInfo = row;
      },
      searchReset() {
        this.queryParam = {};
        this.queryParam.type = "1";
        this.selectValue = "1";
        this.loadData(1);
      },
      handleInput(e) {
        if (e == 0) {
          this.queryParam.type = undefined
        } else {
          this.queryParam.type = e
        }
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>