<template>
  <div class="app-container calendar-list-container" v-loading.body="showLoadingBody">
    <div>
      <div class="filter-container" ref="filter-container">
        <el-form ref="queryForm" :inline="inline" :model="listQuery" label-width="80px">
          <el-row>
            <el-form-item label="状态">
              <el-select class="filter-item" v-model="listQuery.status" placeholder="请选择">
                <el-option :key="undefined" label="无" :value="undefined"></el-option>
                <el-option label="已读" value="1"></el-option>
                <el-option label="未读" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="类型">
              <el-select class="filter-item" v-model="listQuery.type" placeholder="请选择">
                <el-option :key="undefined" label="无" :value="undefined"></el-option>
                <el-option label="公司入驻" value="1"></el-option>
                <el-option label="个人房车入驻" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="时间筛选">
              <el-col :span="11">
                <el-date-picker
                  type="datetime"
                  placeholder="选择时间"
                  v-model="listQuery.startTime"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
              <el-col class="line" :span="1">至</el-col>
              <el-col :span="11">
                <el-date-picker
                  type="datetime"
                  placeholder="选择时间"
                  v-model="listQuery.endTime"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>

            <el-button
              class="filter-item"
              type="primary"
              v-waves
              icon="el-icon-search"
              @click="handleFilter"
            >搜索</el-button>
          </el-row>
        </el-form>
      </div>
      <el-table :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
        <el-table-column align="center" label="类型">
          <template slot-scope="{row}">
            <span v-if="row.type==1">公司入驻</span>
            <span v-else>个人房车入驻</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系人" prop="contact"></el-table-column>
        <el-table-column align="center" label="联系人手机" prop="moblie"></el-table-column>
        <el-table-column align="center" label="公司名称">
          <template slot-scope="{row}">
            <span v-if="row.name">{{row.name}}</span>
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="提交时间" prop="crtTimeStr"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="{row}">
            <span v-if="row.status==0">未读</span>
            <span v-else>已读</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <span v-if="row.status==0">
              <el-button type="text" v-show="elements['btn_set']" @click="set_to_readed(row)">设为已读</el-button>
            </span>
            <span v-else>/</span>
          </template>
        </el-table-column>
      </el-table>
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="listQuery.page"
          :page-sizes="[10,20,30,40,50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDate } from "utils/dateFormattor";

import {
  toEast8Date,
  deepCopyDate,
  newEast8Date,
  convertDate2Str,
  timestamp2Date,
} from "utils/dateUtils";

import { mapGetters } from "vuex";

import { settlement_list, set_to_read } from "api/applyManagements";
import { getAllZone } from "api/base_info/constant/";
import Element1 from "../admin/menu/components/element";
import ElRow from "element-ui/packages/row/src/row";
import ElCol from "element-ui/packages/col/src/col";

export default {
  name: "carApplyList",
  components: {
    ElCol,
    ElRow,
    Element1,
  },
  data() {
    return {
      starTimeStr: "",
      endTimeStr: "",
      newPeopelpickerOptionsEnd: {
        disabledDate: (time) => {
          const beginDateVal = new Date(this.starTimeStr).getTime();
          if (beginDateVal) {
            return time.getTime() < beginDateVal + 8.64e7;
          }
        },
      },
      statusList: [
        {
          name: "未处理",
          id: "0",
        },
        {
          name: "已处理",
          id: "1",
        },
      ],
      BASE_API: process.env.BASE_API,
      showLoadingBody: false,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        startTime: undefined,
        endTime: undefined,
        status: undefined,
        type: undefined,
      },
      inline: true,
      tableKey: 0,
      baranchQuery: {
        zoneId: null,
      },
      allBranchCompany: [],
      app_btn_buy_vehicle_status_update: false,
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(["elements"]),
  },
  methods: {
    /**
     * 获取列表
     * */
    getList() {
      this.listLoading = true;
      let start = "";
      let end = "";
      if (this.listQuery.startTime) {
        start = Date.parse(this.listQuery.startTime);
      }
      if (this.listQuery.endTime) {
        end = Date.parse(this.listQuery.endTime);
      }
      let data = {
        page: this.listQuery.page,
        limit: this.listQuery.limit,
        startTime: start,
        endTime: end,
        type: this.listQuery.type,
        status: this.listQuery.status,
      };
      settlement_list(data).then((response) => {
        let totalCountRs = undefined;
        let listRs = undefined;
        if (
          !this.$utils.isEmpty(response.data.data) &&
          this.$utils.isInteger(response.data.totalCount)
        ) {
          listRs = response.data.data;
          totalCountRs = response.data.totalCount;
          listRs.map(function (item) {
            item.crtTimeStr = timestamp2Date(item.crtTime);
          });
        }
        this.listLoading = false;
        this.list = listRs;
        this.total = totalCountRs;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.getList();
        } else {
          return false;
        }
      });
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    set_to_readed(row) {
      console.log(row);
      this.$confirm("确定设为已读?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        set_to_read({ id: row.id, status: 1 }).then((response) => {
          if (response.status == 200) {
            this.$notify({
              title: "成功",
              message: "处理成功",
              type: "success",
              duration: 2000,
            });
            this.getList();
          } else {
            this.$notify({
              title: "失败",
              message: response.message,
              type: "error",
              duration: 2000,
            });
          }
        });
      });
    },
  },
};
</script>
