<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-date-picker v-model="starTimeStr" type = "date" placeholder = "开始时间" value-format=" yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
                <span> - </span>
                <el-date-picker v-model="endTimeStr" type = "date" placeholder = "结束时间" value-format=" yyyy-MM-dd" format="yyyy-MM-dd" :picker-options="newPeopelpickerOptionsEnd"></el-date-picker>
            </el-form-item>
            <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 v-for="val in statusList" :key="val.id" :label="val.name" :value="val.id"> </el-option>
				</el-select>
            </el-form-item>
            <el-form-item label="所属大区">
				<el-select class="filter-item" v-model="listQuery.areaId" placeholder="请选择" @change="getProvinceRegions">
					<el-option :key="undefined" label="无" :value="undefined"></el-option>
					<el-option v-for="val in getAllZoneList" :key="val.id" :label="val.name" :value="val.id"> </el-option>
				</el-select>
			</el-form-item>
            <el-form-item label="所属公司">
				<el-select class="filter-item" v-model="listQuery.companyId" placeholder="请选择" @change="getAllBranchCompanyChange">
                    <el-option :key="undefined" label="无" :value="undefined"></el-option>
                    <el-option v-for="val in allBranchCompany" :key="val.id" :label="val.name" :value="val.id"> </el-option>
				</el-select>
			</el-form-item>
            <el-button class="filter-item" type="primary" v-waves 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 type="index" align="center" label="申请时间" width="200">
          <template scope="scope">
            <span>{{scope.row.crtTimeStr}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="状态" width="200">
          <template scope="scope">
            <span v-if="scope.row.status==0">未处理</span>
            <span v-if="scope.row.status==1">已处理</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="申请人" width="200">
          <template scope="scope">
            <span>{{scope.row.contactName}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="电话" width="200">
          <template scope="scope">
            <span>{{scope.row.contactPhone}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="看车时间" width="200">
          <template scope="scope">
            <span>{{scope.row.bookTimeStr}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="看车分公司" width="200">
          <template scope="scope">
            <span>{{scope.row.companyName}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="对接人/电话" width="400">
          <template scope="scope">
            <span>{{scope.row.leaderName}}/{{scope.row.leaderPhone}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作"  width="150" fixed="right">
          <template scope="scope">
            <el-button class="el-button el-button--text el-button--small" size="small" v-if="scope.row.status==0&&app_btn_buy_vehicle_status_update" @click="clickHandler(scope.row)">处理</el-button>
          </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 {
    getToken
  } from 'utils/auth';
  import {
    getApplyList,
    getAllBranchCompanyByZoneId,
    changeStatus
  } 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: 20,
          starTime:undefined,
          endTime:undefined,
          status:'',
          areaId:undefined,
          companyId:undefined,
        },
        inline: true,
        tableKey: 0,
        baranchQuery: {
			zoneId:null
        },
        allBranchCompany:[],
        app_btn_buy_vehicle_status_update:false,
      }
    },
    created() {
      this.getList();
      this.app_btn_buy_vehicle_status_update = this.elements['app:btn:buy_vehicle_status:update'];
    },
    computed: {
      ...mapGetters([
        'elements'
      ]),
      //获取大区列表
        getAllZoneList(){
            return getAllZone();
        },
    },
    methods: {
        //监听change事件
	    getProvinceRegions(item) {
			this.listQuery.areaId = item
			this.baranchQuery.zoneId = item
            this.listQuery.companyId = undefined;
			getAllBranchCompanyByZoneId(this.baranchQuery).then(response => {
				this.allBranchCompany = response.data;
			})
        },
        getAllBranchCompanyChange(item) {
			this.listQuery.companyId = item
		},
      /**
       * 获取列表
       * */
      getList() {
        this.listLoading = true;
        for(var key in this.listQuery){
          if(this.listQuery[key]==''){
            this.listQuery[key]=undefined
          }
        }
        if(this.starTimeStr){
            this.listQuery.startTime = new Date(this.starTimeStr).getTime();
        }else{
            this.listQuery.startTime = undefined
        }
        if(this.endTimeStr){
            this.listQuery.endTime = new Date(this.endTimeStr).getTime();
        }else{
            this.listQuery.endTime = undefined
        }
        getApplyList(this.listQuery).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);
              item.bookTimeStr = timestamp2Date(item.bookTime);
            });
          }
          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();
      },
      clickHandler(row){
        console.log(row)
        this.$confirm('确定处理吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          changeStatus(row.id).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>
