<template>
  <a-card :bordered="false" :body-style="{ padding: '0px 0px 8px' }">
    <!-- 查询区域 -->
    <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.courseId" dictCode="education_course,name,id"/>
            </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.lecturerId" dictCode="sys_user,realname,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.salesCode" dictCode="sys_user,realname,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.auditionStatus" dictCode="customer_audition_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>
          </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 -->

    <!-- 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">

        <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="handleConfirm(record)">标为已试听</a>
          <a-divider type="vertical"/>
          <a @click="handleAudition(record)">重新预约</a>
          <a-divider type="vertical"/>
          <a @click="handleCancel(record)">取消预约</a>
        </span> 
        <a slot="detail" slot-scope="text, record" @click="handleFollowDetail(record)" >
          {{text}}({{record.nickname}})
        </a>
      </a-table>
    </div>
    <a-modal :visible="confirmVisible" title="确认信息" @ok="confirmOk"  @cancel="confirmCancel">
      <p>是否确认{{this.selected.name}}已完成试听?</p>
    </a-modal>
    
    <a-modal :visible="cancelVisible" title="确认信息" @ok="cancelOk"  @cancel="cancelCancel">
      <p>是否取消预约来访?</p>
    </a-modal>
    <CustomerInfo  @info-event='infoReturn' :infoShow="infoShow" :dataPara="infoRow" activeKey = "tab2"></CustomerInfo>
    <CustomerAuditionAdd @audition-event='auditionReturn' :auditionShow="auditioningAddVisible"  :dataPara="addRow" />
    <customer-audition-modal ref="modalForm" @ok="modalFormOk"></customer-audition-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { postAction,deleteAction } from '@/api/manage'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CustomerAuditionModal from './CustomerAuditionModal'
  import CustomerAuditionAdd from '../../modules/CustomerAuditionAdd.vue'
  import CustomerInfo from '../../modules/CustomerInfo.vue'
  import store from '@/store'
  export default {
    name: 'CustomerAuditionList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CustomerAuditionModal,CustomerAuditionAdd,CustomerInfo
    },
    data () {
      return {
        description: '试听管理管理页面',
        confirmVisible: false,
        cancelVisible: false,
        reVisible: false,
        infoShow: false,
        queryParam: {},
        infoRow: {},
        selected: {},
        auditioningAddVisible: false,
        addRow: {},
        // 表头
        columns: [
          {
            title:'学员姓名',
            align:"center",
            dataIndex: 'name',
            scopedSlots: { customRender: 'detail' }
          },
          {
            title:'联系方式',
            align:"center",
            dataIndex: 'contactPhone',
            customRender: function (t, r, index) {
              return r.contactPhone +'('+ r.contactName_dictText + ')'
            }
          },
          {
            title:'顾问',
            align:"center",
            dataIndex: 'salesCode_dictText'
          },
          {
            title:'试听课程',
            align:"center",
            dataIndex: 'courseId_dictText'
          },
          {
            title:'试听时间',
            align:"center",
            dataIndex: 'auditionTime'
          },
          {
            title:'老师',
            align:"center",
            dataIndex: 'lecturerId_dictText'
          },
          {
            title:'教室',
            align:"center",
            dataIndex: 'classroomId_dictText'
          },
          {
            title:'试听状态',
            align:"center",
            dataIndex: 'auditionStatus_dictText'
          },
          {
            title:'校区',
            align:"center",
            dataIndex: 'schoolDistrictId_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/customer/customerAudition/listAll",
          delete: "/customer/customerAudition/delete",
          deleteBatch: "/customer/customerAudition/deleteBatch",
          exportXlsUrl: "/customer/customerAudition/exportXls",
          importExcelUrl: "customer/customerAudition/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    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:'cpName',text:'潜客姓名',dictCode:"customer_public,name,name"})
        fieldList.push({type:'string',value:'cpPhone',text:'潜客手机',dictCode:''})
        fieldList.push({type:'datetime',value:'auditionTime',text:'试听日期'})
        fieldList.push({type:'string',value:'salesCode',text:'顾问',dictCode:''})
        fieldList.push({type:'string',value:'auditionStatus',text:'试听状态',dictCode:''})
        fieldList.push({type:'datetime',value:'realAuditionTime',text:'到听日期'})
        fieldList.push({type:'Blob',value:'mark',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'scheduleId',text:'课表ID',dictCode:''})
        this.superFieldList = fieldList
      },
      handleConfirm(record) {
        this.confirmVisible = true;
        this.selected = record;
      },
      handleCancel(record) {
        this.cancelVisible = true;
        this.selected = record;
      },
     
      onCreateTimeChange: function (value, dateString) {
        this.queryParam.beginTime=dateString[0];
        this.queryParam.endTime=dateString[1];
      },
      confirmOk() {
        let params = {
          id: this.selected.id,
          auditionStatus: 2,
          cpId: this.selected.cpId,
          updateBy:store.getters.userInfo.id,
        }
        postAction("/customer/customerAudition/edit", params).then((res) => {
          if (res.success) {
              this.$message.success('编辑成功');
              this.$emit('childReturn',false);
          }
          this.confirmVisible = false;
          this.loadData();
        })
      },
      confirmCancel() {
        this.confirmVisible = false;
        this.selected = {};
      },
      cancelOk() {
        let params = {
          id: this.selected.id,
          auditionStatus: 4,
          cpId: this.selected.cpId,
          updateBy:store.getters.userInfo.id,
        }
        postAction("/customer/customerAudition/edit", params).then((res) => {
          if (res.success) {
              this.$message.success('编辑成功');
              this.$emit('childReturn',false);
          }
          this.cancelVisible = false;
          this.loadData();
        })
      },
      cancelCancel() {
        this.cancelVisible = false;
        this.selected = {};
      },
      handleAudition(row) {
        this.auditioningAddVisible = true;
        this.selected = row
        this.addRow = {
          id: row.cpId,
          sqlType: 'update', //标记为重新预约
          scheduleId_old:row.scheduleId
        }
      },
      auditionReturn(message){
        // 如果返回值是正确的话  那么删除之前的试听记录并刷新也没
        if (message === 'success') {
          let id = String(this.selected.id)
          deleteAction("/customer/customerAudition/delete", {id: id}).then((res) => {
          })
        } 
        this.selected = {};
        this.loadData();
        this.auditioningAddVisible = false;
      },
      handleFollowDetail(row) {
        this.infoShow = true;
        this.infoRow = row;
        this.infoRow.id = row.cpId
      },
      infoReturn() {
        this.infoShow = false;
        this.loadData();
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>