<!-- 采购合同管理 -->
<template>
  <div class="contract">
    <template v-if="!detailVisible && !addOrUpdateVisible && !changeFormVisible &&!changeAttachVisible && !attachVisible && !noticeChangeAttachVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="名称" prop="supplierName">
          <el-input v-model="dataForm.supplierName" placeholder="客户名称" clearable/>
        </el-form-item>
        <el-form-item label="申请日期">
          <el-date-picker
            v-model="dataForm.date"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
          <el-button v-if="isAuth('pur:purchaseContract:save')" type="primary" @click="addOrUpdateHandle(0)">录入</el-button>
          <el-button v-if="isAuth('cus:contractBook:noteChangeConfig')" type="primary" @click="setNoticeChangeHandel()">变更通知人设置</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="contractCode"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="合同编码">
        </el-table-column>
        <el-table-column
          prop="customerName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="客户名称">
        </el-table-column>
        <el-table-column
          prop="contractNumber"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="合同号">
        </el-table-column>
        <el-table-column
          prop="reCode"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="合同评审表编码">
        </el-table-column>
        <el-table-column
          prop="deliveryTime"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="合同交期">
        </el-table-column>
        <el-table-column
          prop="actualDeliveryTime"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="实际交期">
        </el-table-column>
        <el-table-column
          prop="totalAmount"
          header-align="center"
          align="center"
          min-width="100"
          label="合同总金额">
        </el-table-column>
        <el-table-column
          prop="isChange"
          header-align="center"
          align="center"
          label="是否进行合同更改">
        </el-table-column>
        <el-table-column
          prop="changeContentDesc"
          header-align="center"
          align="center"
          min-width="160"
          :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
          header-align="center"
          align="center"
          label="合同更改通知单附件">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.noticeAttachList || scope.row.noticeAttachList.length === 0" type="text" size="small" @click="changeDetails(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('cus:contractBook:info')" type="text" size="small" @click="detailHandle(scope.row.contractId)">查看</el-button>
            <el-button v-if="isAuth('cus:contractBook:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.contractId, false)">编辑</el-button>
            <el-button v-if="isAuth('cus:contractBook:changeContract')" type="text" size="small" @click="changeHandle(scope.row.contractId)">变更</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>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @onChose="onChose"></add-or-update>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
    <attach-detail v-if="changeAttachVisible" ref="changeDetail" @onChose="onChose"/>
    <attach-detail v-if="attachVisible" ref="attachDetail" @onChose="onChose"/>
    <notice-change-setting v-if="noticeChangeAttachVisible" ref="noticeChangeSetting" @onChose="onChose"/>
    <change-form v-if="changeFormVisible" ref="changeForm" @refreshDataList="getDataList" @onChose="onChose"/>
  </div>
</template>

<script>
import AddOrUpdate from '../cus/contract-record-add-or-update'
import Detail from '../cus/contract-record-detail'
import { getCusContractBookList } from '@/api/cus'
import AttachDetail from '../common/attach-detail'
import NoticeChangeSetting from '../cus/contract-record-notice-change-setting'
import ChangeForm from '../cus/contract-record-change'
export default {
  name: 'contract',
  components: {
    AttachDetail,
    AddOrUpdate,
    Detail,
    NoticeChangeSetting,
    ChangeForm
  },
  data () {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      attachVisible: false,
      changeAttachVisible: false,
      noticeChangeAttachVisible: false,
      changeFormVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: []
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    onChose () {
      this.addOrUpdateVisible = false
      this.attachVisible = false
      this.detailVisible = false
      this.noticeChangeAttachVisible = false
      this.changeFormVisible = false
      this.changeAttachVisible = false
    },
    // 查询
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      let params = {
        'current': this.pageIndex,
        'size': this.pageSize,
        'customerName': this.dataForm.customerName ? this.dataForm.customerName : null
      }
      getCusContractBookList(params).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
      })
    },
    deleteHandle (id) {
      if (!id) return
      let ids = []
      ids.push(id)
      this.$confirm(`确定删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/biz-service/cusContractBook/delete`),
          method: 'DELETE',
          data: ids
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 变更
    changeHandle (id) {
      this.changeFormVisible = true
      this.$nextTick(() => {
        this.$refs.changeForm.init(id)
      })
    },
    // 转换属性“产品类别”
    typeFormat (row) {
      if (this.optionsType) {
        for (let i = 0; i < this.optionsType.length; i++) {
          if (this.optionsType[i].code === row.productType) {
            return this.optionsType[i].value
          }
        }
      }
    },
    // 详情
    detailHandle (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    // 变更通知人设置
    setNoticeChangeHandel () {
      this.noticeChangeAttachVisible = true
      this.$nextTick(() => {
        this.$refs.noticeChangeSetting.init()
      })
    },
    // 产品更改通知单
    changeDetails (row) {
      this.changeAttachVisible = true
      this.$nextTick(() => {
        this.$refs.changeDetail.init(row.noticeAttachList)
      })
    },
    // 附件
    attachDetails (row) {
      this.attachVisible = true
      this.$nextTick(() => {
        this.$refs.attachDetail.init(row.attachList)
      })
    }
  }
}
</script>

<style scoped>

</style>
