<!-- 报价管理 -->
<template>
  <div>
    <template v-if="!addOrUpdateVisible && !detailVisible && !priceVisible && !accreditVisible && !resultVisible && !notifyVisible">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="queryData()"
      >
        <el-form-item label="项目名称">
          <el-input
            v-model="dataForm.projectName"
            placeholder="请输入项目名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="部门">
          <org-component v-model="dataForm.orgId" />
        </el-form-item>
        <el-form-item>
          <el-button @click="queryData()">查询</el-button>
          <el-button
            v-if="isAuth('quoted:price:save')"
            type="primary"
            @click="addOrUpdateHandle(0)"
            >新建</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="priceCode"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="报价编码"
        >
        </el-table-column>
        <el-table-column
          prop="projectName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="项目名称"
        >
        </el-table-column>
        <el-table-column
          prop="type"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="项目类别"
        >
          <template slot-scope="scope">
            {{productTypeOption.find(t => t.value === scope.row.type).label}}
          </template>
        </el-table-column>
        <el-table-column
          prop="materialCost"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="报价小计"
        >
        </el-table-column>
        <el-table-column
          prop="mouldPrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="模具费"
        >
        </el-table-column>
        <el-table-column
          prop="managePrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="管理费"
        >
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="报价附件">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.attachList || scope.row.attachList.length === 0" type="text" size="small" @click="attachDetails(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="ratePrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="税率"
        >
        </el-table-column>
        <el-table-column
          prop="totalPrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="总价"
        >
        </el-table-column>
        <el-table-column
          prop="precisionMaterialCost"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="精准报价小计"
        >
        </el-table-column>
        <el-table-column
          prop="precisionMouldPrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="精准模具费"
        >
        </el-table-column>
        <el-table-column
          prop="precisionManagePrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="精准管理费"
        >
        </el-table-column>
        <el-table-column
          prop="precisionRatePrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="精准税率"
        >
        </el-table-column>
        <el-table-column
          prop="precisionTotalPrice"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="精准总价"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="备注"
        >
        </el-table-column>
        <el-table-column
          prop="creatorName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="负责人"
        >
        </el-table-column>
        <el-table-column
          prop="orgName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="部门"
        >
        </el-table-column>
        <el-table-column
          prop="accredit"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="授权情况"
        >
        </el-table-column>
        <el-table-column
          prop="state"
          fixed="right"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="项目状态"
        >
          <template slot-scope="scope">
            {{projectStateOption.find(t => t.value === scope.row.state).label}}
          </template>
        </el-table-column>
        <el-table-column
          prop="approveState"
          fixed="right"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="项目评估状态"
        >
          <template slot-scope="scope">
            {{approveStateOption.find(t => t.value === scope.row.approveState).label}}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          min-width="80"
          :formatter="formatReadState"
          :show-overflow-tooltip="true"
          label="读写状态">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          min-width="180"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="showDetail(scope.row.priceId)"
              >查看</el-button
            >
            <el-button
              v-if="isAuth('quoted:price:update') && Number(scope.row.approveState) === 0 && Number(scope.row.state) === 1"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.priceId)"
              >编辑</el-button
            >
            <el-button
              v-if="isAuth('quoted:price:revoke') && Number(scope.row.approveState) === 1 && Number(scope.row.state) === 1"
              type="text"
              size="small"
              @click="revokeHandle(scope.row.priceId)"
              >撤回</el-button
            >
            <el-button
              v-if="isAuth('quoted:price:updateFirst') && Number(scope.row.approveState) === 3 && (Number(scope.row.state) === 1 || Number(scope.row.state) === 2)"
              type="text"
              size="small"
              @click="priceHandle(scope.row.priceId,'first')"
              >报价</el-button
            >
            <el-button
              v-if="isAuth('quoted:price:updateTwo') && Number(scope.row.approveState) === 3 && (Number(scope.row.state) === 4 || Number(scope.row.state) === 5)"
              type="text"
              size="small"
              @click="priceHandle(scope.row.priceId,'second')"
              >精准报价</el-button
            >
            <el-button
              v-if="isAuth('work:clt:insertBatch') && Number(scope.row.approveState) === 3 && (Number(scope.row.state) === 2 || Number(scope.row.state) === 5)"
              type="text"
              size="small"
              @click="notifyHandle(scope.row.priceId)"
              >通知报价</el-button
            >
            <!-- <el-button
              v-if="isAuth('quoted:price:updateRestart') && Number(scope.row.approveState) === 3 && Number(scope.row.state) > 6"
              type="text"
              size="small"
              @click="priceHandle(scope.row.priceId, 'restart')"
              >重新报价</el-button
            > -->
            <el-button
              v-if="isAuth('quoted:price:updateFinish') && Number(scope.row.approveState) === 3 && (Number(scope.row.state) === 2 || Number(scope.row.state === 5))"
              type="text"
              size="small"
              @click="finishHandle(scope.row.priceId)"
              >报价完成</el-button
            >
            <el-button
              v-if="isAuth('quoted:price:updateAccredit') && Number(scope.row.approveState) === 3 && (Number(scope.row.state) === 3 || Number(scope.row.state) === 6)"
              type="text"
              size="small"
              @click="accreditHandle(scope.row.priceId)"
              >授权</el-button
            >
            <el-button
              v-if="isAuth('quoted:price:updateResult') && Number(scope.row.approveState) === 3 && (Number(scope.row.state) === 4 || Number(scope.row.state) === 7)"
              type="text"
              size="small"
              @click="resultHandle(scope.row.priceId)"
              >报价结果</el-button
            >
            <el-button v-if="isAuth('quoted:price:read') && Number(scope.row.writeState) === 2" type="text" size="small" @click="readHandle(scope.row.priceId)">只读</el-button>
            <el-button v-if="isAuth('quoted:price:write') && Number(scope.row.writeState) === 1" type="text" size="small" @click="writeHandle(scope.row.priceId)">读写</el-button>
            <el-button type="text" size="small" @click="exportExcel(scope.row.priceId)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <quoted-add-or-update ref="addOrUpdate" v-if="addOrUpdateVisible" @onChose="onChose" @refreshDataList="queryData"/>
    <quoted-price ref="price" v-if="priceVisible" @onChose="onChose" @refreshDataList="queryData"/>
    <quoted-detail ref="detail" v-if="detailVisible" @onChose="onChose" />
    <quoted-accredit ref="accredit" v-if="accreditVisible" @onChose="onChose"/>
    <quoted-result ref="result" v-if="resultVisible" @onChose="onChose"/>
    <quoted-notify ref="notify" v-if="notifyVisible" @onChose="onChose"/>
    <attach-detail-dialog ref="attachDetail" @onChose="onChose" />
  </div>
</template>

<script>
import OrgComponent from '../common/org-component'
import {getList, revoke, updateFinish} from '@/api/quoted'
import QuotedAddOrUpdate from './quoted-add-or-update'
import {productTypeOption, approveStateOption, projectStateOption, writeStateOption} from '@/utils/enums'
import QuotedPrice from './quoted-price'
import QuotedDetail from './quoted-detail'
import QuotedAccredit from './quoted-accredit'
import QuotedResult from './quoted-result'
import QuotedNotify from './quoted-notify'
import AttachDetailDialog from '@/views/modules/common/attach-detail-dialog'
import Vue from 'vue'

export default {
  name: 'cus-quoted',
  components: { AttachDetailDialog, OrgComponent, QuotedAddOrUpdate, QuotedPrice, QuotedDetail, QuotedAccredit, QuotedResult, QuotedNotify },
  data () {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      priceVisible: false,
      accreditVisible: false,
      resultVisible: false,
      notifyVisible: false,
      productTypeOption: productTypeOption,
      approveStateOption: approveStateOption,
      projectStateOption: projectStateOption,
      writeStateOption: writeStateOption,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    onChose () {
      this.addOrUpdateVisible = false
      this.detailVisible = false
      this.priceVisible = false
      this.accreditVisible = false
      this.resultVisible = false
      this.notifyVisible = false
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
      // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    queryData () {
      this.pageIndex = 1
      this.getDataList()
    },
    getDataList () {
      this.dataListLoading = true
      let param = {
        current: this.pageIndex,
        size: this.pageSize,
        ...this.dataForm
      }
      getList(param).then(({data}) => {
        if (data && data.code === '200') {
          this.dataList = data.data.records
          this.totalPage = Number(data.data.total)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    addOrUpdateHandle (id, type) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type)
      })
    },
    priceHandle (id, type) {
      this.priceVisible = true
      this.$nextTick(() => {
        this.$refs.price.init(id, type)
      })
    },
    notifyHandle (id) {
      this.notifyVisible = true
      this.$nextTick(() => {
        this.$refs.notify.init(id)
      })
    },
    showDetail (priceId) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(priceId)
      })
    },
    accreditHandle (priceId) {
      this.accreditVisible = true
      this.$nextTick(() => {
        this.$refs.accredit.init(priceId)
      })
    },
    revokeHandle (priceId) {
      this.$confirm('是否确认要撤回?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        revoke({id: priceId}).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消撤回'
        })
      })
    },
    resultHandle (priceId) {
      this.resultVisible = true
      this.$nextTick(() => {
        this.$refs.result.init(priceId)
      })
    },
    finishHandle (priceId) {
      this.$confirm('是否确认完成报价?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateFinish({id: priceId}).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消撤回'
        })
      })
    },
    attachDetails (attachList) {
      this.$refs.attachDetail.init(attachList)
    },
    formatReadState (row) {
      if (!row.writeState) return ''
      let option = this.writeStateOption.find(t => t.value === row.writeState)
      if (option != null) {
        return option.label
      }
      return ''
    },
    readHandle (id) {
      if (!id) return
      this.$confirm(`确定只读?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/biz-service/quoted/read`),
          method: 'POST',
          data: {id: id}
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    writeHandle (id) {
      if (!id) return
      this.$confirm(`确定读写?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/biz-service/quoted/write`),
          method: 'POST',
          data: {id: id}
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    exportExcel (id) {
      let url = this.$http.adornUrl(`/biz-service/quoted/exportExcel/${id}`) +
          '?_token=' + Vue.cookie.get('token')
      window.open(url, '_blank')
    }
  }
}
</script>

<style scoped>
</style>
