<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form label-width="77px" class="ele-form-search">
        <el-row :gutter="15">
          <el-row :gutter="15">
            <el-col :md="6" :sm="12">
              <el-form-item label="线索编号:">
                <el-input
                  placeholder="请输入线索编号"
                  clearable
                  v-model="where.sid"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="手机号码:">
                <el-input
                  placeholder="请输入手机号"
                  clearable
                  v-model="where.title"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="关注车型:">
                <brand-select
                  v-model="where.brands"
                  type="provinceCity"
                  :props="{ multiple: true }"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="线索状态:">
                <el-select clearable v-model="where.status" class="ele-block">
                  <el-option
                    v-for="(item, index) in statusList"
                    :key="index"
                    :value="item.value"
                    :label="item.label"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :md="6" :sm="12">
              <el-form-item label="所在地区:">
                <regions-select
                  v-model="where.citys"
                  type="provinceCity"
                  placeholder="请选择省市"
                  class="ele-fluid"
                />
              </el-form-item>
            </el-col>
            <el-col
              v-bind="styleResponsive ? { lg: 12, md: 12 } : { span: 12 }"
            >
              <el-form-item label="入池时间:">
                <el-date-picker
                  unlink-panels
                  v-model="where.dateRange"
                  range-separator="-"
                  type="daterange"
                  end-placeholder="结束日期"
                  start-placeholder="开始日期"
                  :picker-options="pickerOptions"
                  value-format="yyyy-MM-dd"
                  class="ele-fluid"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--
          <el-col :md="6" :sm="12">
            <el-form-item label="线索归属:">
              <el-cascader
                v-model="where.belong"
                :options="belongOptions"
                :props="{ multiple: true, checkStrictly: true }"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          -->
          <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
            <el-form-item label="归属中心">
              <el-select v-model="where.centerNumber" clearable>
                <el-option
                  v-for="(item, index) in centerList"
                  :value="index"
                  :key="index"
                  :label="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
            <el-form-item label="归属机构">
              <el-select value="" v-model="where.orgName" clearable filterable>
                <el-option
                  v-for="(item, index) in orgNameList"
                  :value="item"
                  :key="index"
                  :label="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <el-form-item label="归属人员:">
              <el-input
                placeholder="请输入工号"
                clearable
                v-model="where.userCode"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
              >
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="receiverCluesTables"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar></template>
        <template v-slot:customer="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            <div>{{ row.name }}</div>
            <div>{{ row.mobile }}</div>
            <div>{{ row.sid }}</div>
          </el-link>
        </template>
        <template v-slot:belongUserName="{ row }">
          <div>{{ row.belongUserName.username }}</div>
          <div>{{ row.belongUserName.userCode }}</div>
        </template>
        <template v-slot:centerNumber="{ row }">
          <div>{{ centerList[row.belongUserName.centerNumber] }}</div>
        </template>
        <template v-slot:orgName="{ row }">
          <div>{{ row.belongUserName.orgName }}</div>
        </template>
        <template v-slot:brandSeries="{ row }">
          <div v-for="(item, index) in row.brandList" :key="index">{{
            item
          }}</div>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-button
            size="small"
            type="primary"
            :underline="false"
            @click="call(row)"
          >
            拨打电话
          </el-button>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
  import { pageClues, getSearch, callPhone } from '@/api/receiver/clues';
  import RegionsSelect from '@/components/RegionsSelect/index.vue';
  import BrandSelect from '@/components/BrandSelect/index.vue';
  import { pageCenterList, pageOrgNameList } from '@/api/auto';

  export default {
    name: 'receiverClues',
    components: { RegionsSelect, BrandSelect },
    data() {
      return {
        where: {
          title: '',
          citys: [],
          brands: [],
          belong: [],
          userCode: '',
          centerNumber: '',
          orgName: ''
        },
        // 表格列配置
        columns: [
          {
            prop: 'customer',
            label: '线索',
            minWidth: 115,
            align: 'center',
            fixed: 'left',
            slot: 'customer'
          },
          {
            prop: 'belongUserName',
            label: '归属',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80,
            slot: 'belongUserName'
          },
          {
            prop: 'centerNumber',
            label: '中心',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 50,
            slot: 'centerNumber'
          },
          {
            prop: 'orgName',
            label: '机构',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 50,
            slot: 'orgName'
          },
          {
            prop: 'brandSeries',
            label: '关注车型',
            align: 'center',
            minWidth: 80,
            resizable: false,
            showOverflowTooltip: true,
            slot: 'brandSeries'
          },
          {
            prop: 'statusCn',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 50
          },
          {
            prop: 'poi',
            label: '所属地区',
            align: 'center'
          },
          {
            prop: 'enTime',
            label: '入池时间',
            align: 'center',
            minWidth: 80,
            resizable: false
          },
          {
            prop: 'action',
            label: '操作',
            align: 'center',
            width: 100,
            slot: 'action'
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 日期时间选择器快捷项
        pickerOptions: {
          shortcuts: [
            {
              text: '今天',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '昨天',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24);
                end.setTime(end.getTime() - 3600 * 1000 * 24);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一周',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit('pick', [start, end]);
              }
            }
          ]
        },
        belongOptions: [],
        statusList: [],
        centerList: [],
        orgNameList: []
      };
    },
    created() {
      this.loadSearch();
      this.loadCenterList();
      this.loadOrgNameList();
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      loadOrgNameList() {
        pageOrgNameList()
          .then((data) => {
            this.orgNameList = data;
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      loadCenterList() {
        pageCenterList()
          .then((data) => {
            this.centerList = data;
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      loadSearch() {
        getSearch()
          .then((data) => {
            console.log(data);
            this.statusList = data.statusList;
            this.belongOptions = data.belongList;
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /* 下拉按钮点击 */
      dropClick(command, row) {
        if (command === 'edit') {
          this.current = row;
          this.showEdit = true;
        } else if (command === 'brokerage') {
          this.current = row;
          this.showEditBrokerage = true;
        } else if (command === 'coupon') {
          const path = '/car/product/coupon';
          this.$nextTick(() => {
            this.$router.push({
              path,
              query: row ? { id: row.id, title: row.title } : undefined
            });
          });
        }
      },
      goDetail(row) {
        const path = '/receiver/clues/detail';
        this.$router.push({
          path,
          query: row ? { id: row.id, title: row.title } : undefined
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return pageClues({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload({ page: 1, where: this.where });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
        this.reload();
      },
      /* 拨打电话 */
      call(row) {
        this.$confirm('确定拨打电话【' + row.mobile + '】吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            callPhone({ id: row.id })
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      }
    }
  };
</script>
<style scoped>
  ::v-deep .el-cascader__tags input::-webkit-input-placeholder {
    color: white;
    opacity: 0;
  }
</style>
