<template>
  <div class="app-container">
    <div v-show="viewState === 1">
      <button-bar ref="btnbar" view-title="采购订单管理" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="isSearchShow = !isSearchShow">{{ isSearchShow ? '隐藏查询条件' : '显示查询条件' }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form :inline="true" class="tab-header" label-width="110px">
              <el-form-item label="订单编号">
                <el-input v-model="queryParams.params.orderNo" placeholder="请输入编号" clearable/>
              </el-form-item>
              <el-form-item label="订单类型">
                <el-select v-model="queryParams.params.orderTypeKey" style="width:200px" filterable placeholder="请选择" clearable>
                  <el-option v-for="item in billType_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="状态">
                <el-input v-model="queryParams.params.orderStatus" placeholder="请输入状态" clearable/>
              </el-form-item>
              <el-form-item label="车型" class="search_label">
                <el-input v-model="queryParams.params.modelName" placeholder="请输入车型" clearable/>
              </el-form-item>
              <el-form-item label="排产申请编号" class="search_label">
                <el-input v-model="queryParams.params.applicationCode" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="申请日期">
                <el-date-picker v-model="queryParams.params.startDate" type="date" placeholder="选择日期">
                </el-date-picker>
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="queryParams.params.endDate" type="date" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" icon="el-icon-search" size="small" @click="dosearch">查询</el-button>
              <el-button type="primary" icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </div>
        <!--Start 项目列表头部-->
        <div class="listtop">
          <div class="tit">采购订单列表</div>
          <pageye v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current" :limit.sync="queryParams.size" class="pagination" @pagination="loadList"/>
        </div>
        <!--End 项目列表头部-->
        <!--Start 项目列表-->
        <div class="">
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%">
            <el-table-column fixed width="60px" label="序号" type="index" :index="indexMethod" align="center"/>
            <el-table-column fixed label="操作" align="center" width="220">
              <template slot-scope="scope">
                <el-button type="primary" size="small" :disabled="scope.row.offlineDate !=='' || scope.row.orderStatus == '已作废'" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" :disabled="scope.row.offlineDate !=='' || scope.row.orderStatus == '已作废'" @click="handleCancellation(scope.row)">作废</el-button>
                <el-button type="primary" size="small" @click="handleLooK(scope.row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="orderStatus" label="采购订单状态" align="center" width="120"/>
            <el-table-column prop="orderNo" label="采购订单编号" header-align="center" align="left" width="200"/>
            <el-table-column prop="orderDate" label="采购订单日期" align="center" width="120"/>
            <el-table-column prop="orderType" label="采购订单类型" align="center" width="120"/>
            <el-table-column prop="applicationCode" label="排产申请编号" align="center" width="200"/>
            <el-table-column prop="subscriptionDate" label="认款日期" align="center" width="100"/>
            <el-table-column prop="insideCode" label="内部编码" header-align="center" align="left" width="220"/>
            <el-table-column prop="vehicleName" label="车型" align="center" width="200">
              <template slot-scope="scope">
                <span class="bluezi" @click="handlePeizhi(scope.row)">{{scope.row.vehicleName}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="platformNo" label="台数" align="center" width="80"/>
            <el-table-column prop="expectLaunchDate" label="预期上线时间" align="center" width="120"/>
            <el-table-column prop="launchDate" label="上线日期" align="center" width="100"/>
            <el-table-column prop="offlineDate" label="下线日期" align="center" width="100"/>
          </el-table>
        </div>
        <!--End 项目列表-->
        <div class="pages">
          <div class="tit"/>
          <!-- 翻页 -->
          <pagination v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current" :limit.sync="queryParams.size" class="pagination" @pagination="loadList"/>
        </div>
      </div>
    </div>
    <!--End 查询和其列表部分-->
    <!--新增修改部分组件-->
    <div-add v-show="viewState == 3" ref="divadd" @doback="resetState" @reloadlist="loadList"/>
    <!--详情部分组件-->
    <div-info v-show="viewState == 4" ref="divinfo" @doback="resetState"/>
    <!-- 车辆详情标准页面-->
    <vehicleconfiguration  v-show="viewState == 5" ref="divPeizhi" @doback="resetState"/>
    <!-- 作废说明 -->
    <el-dialog title="作废说明" :append-to-body="true" :visible.sync="dialogVisible">
      <el-form ref="" :model="formobj" class="formadd">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="24" class="tleftb">
            <el-form-item><el-input v-model="formobj.cancalRemarks" clearable placeholder=""/></el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleConfirm">确 定</el-button>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/cheliang/busvehicleorder'
import ButtonBar from '@/components/ButtonBar'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import divAdd from './cheliangcaigouAdd'
import divInfo from './cheliangcaigouInfo'
import vehicleconfiguration from '@/views/cheliang/cheliangcaigou/relation/vehicleconfiguration'
import { typeValues } from '@/api/cheliang/dictcommons'

export default {
  name: 'cheliangcaigou',
  components: {
    ButtonBar,
    Pagination,
    pageye,
    divAdd,
    divInfo,
    vehicleconfiguration
  },
  data() {
    return {
      btndisabled: false,
      viewState: 1, // 1、列表 2、添加 3、修改 4、查看
      isSearchShow: false, // 显示隐藏
      tableLoading: false,
      dialogVisible: false,
      // 下拉框
      billType_list: [], // 单据类型
      // 列表数据
      dataList: [],
      btnList: [
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      // sid数组
      sids: [], // 用于导出的时候保存已选择SID
      queryParams: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          orderNo: '',
          orderTypeKey: '',
          orderStatus: '',
          startDate: '',
          endDate: '',
          modelName: '',
          staffSid: '',
          userSid: '',
          applicationCode: '',
          orgPath: ''
        }
      },
      formobj: {
        sid: '',
        cancalRemarks: ''
      }
    }
  },
  created() {
    this.loadList()
    this.getBillType()
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX' + btnKey)
      switch (btnKey) {
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    resetState() {
      this.viewState = 1
    },
    handleUpdate(row) {
      this.viewState = 3
      this.$refs['divadd'].showEdit(row)
    },
    handleCancellation(row) {
      this.dialogVisible = true
      this.cancalRemarks = ''
      this.formobj.sid = row.sid
    },
    handleConfirm() {
      if (this.formobj.cancalRemarks === '') {
        this.$message({ showClose: true, type: 'error', message: '请填写作废说明' })
        return
      }
      req.cancal(this.formobj).then((res) => {
        if (res.success) {
          this.dialogVisible = false
          this.$message({ showClose: true, type: 'success', message: '操作成功' })
          this.loadList()
        }
      })
    },
    handleLooK(row) {
      this.viewState = 4
      this.$refs['divinfo'].showInfo(row)
    },
    handlePeizhi(row) {
      this.viewState = 5
      this.$refs['divPeizhi'].showInfo(row)
    },
    dosearch() {
      this.queryParams.current = 1
      this.loadList()
    },
    // 下拉框-单据类型 changeBillType
    getBillType() {
      typeValues({ type: 'orderType' }).then((res) => {
        if (res.success) {
          this.billType_list = res.data
        }
      })
    },
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          orderNo: '',
          orderTypeKey: '',
          orderStatus: '',
          startDate: '',
          endDate: '',
          modelName: '',
          staffSid: '',
          userSid: '',
          applicationCode: '',
          orgPath: ''
        }
      }
      this.dosearch()
    },
    // 查询
    loadList() {
      this.tableLoading = true
      this.queryParams.params.staffSid = window.sessionStorage.getItem('staffSid')
      this.queryParams.params.userSid = window.sessionStorage.getItem('userSid')
      this.queryParams.params.orgPath = window.sessionStorage.getItem('defaultOrgPath')
      req.listPage(this.queryParams).then(resp => {
        this.tableLoading = false
        if (resp.success) {
          const data = resp.data
          this.queryParams.total = data.total
          this.dataList = data.records
        } else {
          this.queryParams.total = 0
          this.dataList = []
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.queryParams.current - 1) * this.queryParams.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
</style>
