<template>
  <div class="app-container">
    <!-- Start 列表页面 -->
    <div v-show="viewState == 1">
      <button-bar view-title="车型报价" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="switch_btn">
          <el-button :class="{btn_style:viewState === 1}">待报价列表</el-button>
          <el-button class="" @click="handleClick">已报价列表</el-button>
        </div>
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form ref="listQueryform" :inline="true" :model="listQuery" class="tab-header">
              <el-form-item label="品牌">
                <el-select v-model="listQuery.params.brandSid" placeholder="请选择" clearable class="addinputw">
                  <el-option v-for="item in brand_list" :key="item.sid" :label="item.brandName" :value="item.sid"/>
                </el-select>
              </el-form-item>
              <el-form-item label="车型">
                <el-input v-model="listQuery.params.vehModel" clearable placeholder="" class="addinputw"></el-input>
              </el-form-item>
              <el-form-item label="询价日期">
                <div class="block" style="float: left;">
                  <el-date-picker v-model="listQuery.params.inquiryStartDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
                </div>
                <div style="float: left;margin: 0px 10px">至</div>
                <div class="block" style="float: left;">
                  <el-date-picker v-model="listQuery.params.inquiryEndDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
                </div>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" icon="el-icon-search" @click="handleFilter" size="small">查询</el-button>
              <el-button type="primary" icon="el-icon-refresh" @click="handleReset" size="small">重置</el-button>
            </div>
          </div>
        </div>
        <div class="listtop">
          <div class="tit">待报价列表</div>
          <pageye v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
        <div class="">
          <el-table :key="tableKey" v-loading="listLoading" :data="list" border style="width: 100%;">
            <el-table-column width="80px" label="序号" type="index" :index="indexMethod" align="center"/>
            <el-table-column label="操作" width="150px" align="center">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handLook(scope.row)">查看</el-button>
                <el-button type="primary" size="mini" @click="handBaoJia(scope.row)">报价</el-button>
              </template>
            </el-table-column>
            <el-table-column label="品牌" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="车型" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.vehModel }}</span>
              </template>
            </el-table-column>
            <el-table-column label="询价日期" align="center" width="180">
              <template slot-scope="scope">
                <span>{{ scope.row.createTime }}</span>
              </template>
            </el-table-column>
            <el-table-column label="询价人" align="center" width="180">
              <template slot-scope="scope">
                <span>{{ scope.row.inquirer }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pages">
          <!-- 翻页 -->
          <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
      </div>
    </div>
    <!-- End 列表页面 -->
    <diabaojiaAdd v-show="viewState == 2" ref="divAdd" @doback="resetState" @reloadlist="init"/>
    <daibaojiaInfo v-show="viewState == 3" ref="divInfo" @doback="resetState"/>
    <yibaojia v-show="viewState == 4" ref="divYiBao" @doback="resetState"/>
  </div>
</template>

<script>
import { pagerList } from '@/api/cheliang/chexingbaojia'
import { brandDown, getOrgSidByPath } from '@/api/cheliang/dictcommons'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import ButtonBar from '@/components/ButtonBar'
import diabaojiaAdd from './diabaojiaAdd'
import daibaojiaInfo from './daibaojiaInfo'
import yibaojia from '../yibaojiaguanli/yibaojia'

export default {
  name: 'daibaojia',
  components: {
    Pagination,
    pageye,
    ButtonBar,
    diabaojiaAdd,
    daibaojiaInfo,
    yibaojia
  },
  data() {
    return {
      viewState: 1,
      btndisabled: false,
      btnList: [
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      // 查询条件 -----------
      tableKey: 0,
      list: [],
      brand_list: [], // 功能
      FormLoading: false,
      listLoading: false,
      listQuery: {
        params: {
          inquiryStartDate: '',
          inquiryEndDate: '',
          createOrgSid: '',
          brandSid: '',
          vehModel: ''
        },
        current: 1,
        size: 10,
        total: 0
      },
      rules: {}
    }
  },
  created() {
    // 初始化变量
    this.init()
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    init() {
      getOrgSidByPath({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.listQuery.params.createOrgSid = res.data
          // 加载列表
          this.getList()
          brandDown({ useOrg: this.listQuery.params.createOrgSid }).then((res) => {
            if (res.success) {
              this.brand_list = res.data
            }
          })
        }
      })
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      pagerList(this.listQuery).then((response) => {
        console.log('列表查询结果：', response)
        this.listLoading = false
        if (
          response.code === '200' &&
          response.data &&
          response.data.total > 0
        ) {
          this.list = response.data.records
          this.listQuery.total = response.data.total
        } else {
          this.list = []
          this.listQuery.total = 0
        }
      })
    },
    handleClick() {
      this.viewState = 4
      this.$refs['divYiBao'].init()
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.init()
    },
    handleReset() {
      this.listQuery = {
        params: {
          inquiryStartDate: '',
          inquiryEndDate: '',
          createOrgSid: '',
          brandSid: '',
          vehModel: ''
        },
        current: 1,
        size: 10,
        total: 0
      }
      this.init()
    },
    handBaoJia(row) {
      this.viewState = 2
      this.$refs['divAdd'].showInfo(row)
    },
    handLook(row) {
      this.viewState = 3
      this.$refs['divInfo'].showInfo(row)
    },
    resetState() {
      this.viewState = 1
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
.btn_style {
  background-color: #018ad2;
  color: white;
  font-weight: 600;
}
</style>
