<!-- 数控程序管理 -->
<template>
  <div>
    <template v-if="!addOrUpdateVisible && !detailVisible && !assignVisible && !noteChangeVisible">
      <el-form :inline="true" :model="dataForm" label-width="80px" @keyup.enter.native="queryData()">
        <el-form-item label="项目名称">
          <el-input v-model="dataForm.projectName" placeholder="请输入项目名称" clearable />
        </el-form-item>
        <el-form-item label="任务号">
          <el-input v-model="dataForm.orderCode" placeholder="请输入任务号" clearable />
        </el-form-item>
        <el-form-item label="责任人">
          <user-component v-model="dataForm.responsibilityPerson" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="dataForm.state" placeholder="请选择状态" clearable>
            <el-option v-for="item in optionsState" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input v-model="dataForm.productName" placeholder="请输入物料名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button @click="queryData()">查询</el-button>
          <el-button v-if="isAuth('pro:program:save')" type="primary" @click="addOrUpdateHandle(0)">新建</el-button>
          <el-button v-if="isAuth('pro:program:noteChangeConfig')" type="primary"
            @click="noteChange()">变更通知人设置</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="programCode" 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="orderCode" header-align="center" align="center" min-width="140"
          :show-tooltip-when-overflow="true" label="任务号">
        </el-table-column>
        <el-table-column prop="mapNumber" header-align="center" align="center" min-width="140"
          :show-tooltip-when-overflow="true" label="图号">
        </el-table-column>
        <el-table-column prop="productName" header-align="center" align="center" min-width="140"
          :show-tooltip-when-overflow="true" label="物料名称">
        </el-table-column>
        <el-table-column prop="planCnt" header-align="center" align="center" min-width="80"
          :show-tooltip-when-overflow="true" label="数量">
        </el-table-column>
        <el-table-column prop="state" header-align="center" align="center" min-width="80" fixed="right" label="状态">
          <template slot-scope="scope">
            <span>{{
              optionsState.find(t => t.value === scope.row.state.toString())
                .label
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="attachList1" header-align="center" align="center" min-width="100" label="技术协议">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.attachList1 || scope.row.attachList1.length === 0
              " type="text" size="small" @click="attachDetails(scope.row.attachList1)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="attachList2" header-align="center" align="center" min-width="100" label="技术文件">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.attachList2 || scope.row.attachList2.length === 0
              " type="text" size="small" @click="attachDetails(scope.row.attachList2)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="deliveryDate" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="合同交期">
        </el-table-column>
        <el-table-column prop="responsibilityPerson" header-align="center" align="center" min-width="100"
          :show-tooltip-when-overflow="true" label="责任人">
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="135" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showDetail(scope.row.programId)">查看</el-button>
            <el-button v-if="isAuth('pro:program:assign') && Number(scope.row.state) === 1" type="text" size="small"
              @click="showAssign(scope.row)">分派</el-button>
            <el-button v-if="isAuth('pro:program:updateProcessing') && Number(scope.row.state) === 1" type="text"
              size="small" @click="addOrUpdateHandle(scope.row.programId, 1)">处理</el-button>
            <el-button v-if="isAuth('pro:program:update') && Number(scope.row.state) === 2" type="text" size="small"
              @click="addOrUpdateHandle(scope.row.programId, 2)">编辑</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>
    <attach-detail-dialog ref="attachDetail" />
    <program-add-or-update ref="addOrUpdate" v-if="addOrUpdateVisible" @onChose="onChose"
      @refreshDataList="getDataList"></program-add-or-update>
    <program-detail ref="detail" v-if="detailVisible" @onChose="onChose"></program-detail>
    <program-assign ref="assign" v-if="assignVisible" :programId="assignId" @close="onAssignClose"></program-assign>
    <note-change ref="noteChange" v-if="noteChangeVisible" @onChose="onChose"></note-change>
  </div>
</template>

<script>
import { getProductList } from '@/api/product'
import { getList } from '@/api/program'
import AttachDetailDialog from '../common/attach-detail-dialog'
import ProgramAddOrUpdate from './program-add-or-update'
import ProgramDetail from './program-detail'
import ProgramAssign from './program-assign'
import { isAuth } from '@/utils'
import UserComponent from '@/views/modules/common/user-component.vue'
import NoteChange from './program-note-change'
export default {
  name: 'program',
  components: {
    UserComponent,
    AttachDetailDialog,
    ProgramAddOrUpdate,
    ProgramDetail,
    ProgramAssign,
    NoteChange
  },
  data() {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      noteChangeVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      // 状态列表数据
      optionsState: [
        { value: '1', label: '待处理' },
        { value: '2', label: '已处理' }
      ],
      assignVisible: false,
      assignId: null
    }
  },
  created() {
    this.queryData()
    this.getProductList()
  },
  methods: {
    isAuth,
    onChose() {
      this.addOrUpdateVisible = false
      this.detailVisible = false
      this.assignVisible = false
      this.noteChangeVisible = false
    },
    getProductList() {
      let params = {
        current: 1,
        size: 100
      }
      getProductList(params).then(({ data }) => {
        if (data && data.code === '200') {
          this.materialList = data.data.records
        }
      })
    },
    // 查询
    queryData() {
      this.pageIndex = 1
      this.getDataList()
    },
    getDataList() {
      let param = {
        current: this.pageIndex,
        size: this.pageSize,
        ...this.dataForm
      }
      getList(param).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataList = data.data.records
        }
      })
    },
    addOrUpdateHandle(id, type) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type)
      })
    },
    showDetail(id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    attachDetails(attachList) {
      this.$refs.attachDetail.init(attachList)
    },
    showAssign(row) {
      this.assignVisible = true
      this.$nextTick(() => {
        this.$refs.assign.init(row)
      })
    },
    onAssignClose() {
      this.assignVisible = false
      this.assignId = null
      this.getDataList()
    },
    noteChange() {
      this.noteChangeVisible = true
      this.$nextTick(() => {
        this.$refs.noteChange.init()
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    }
  }
}
</script>

<style scoped></style>
