<!-- 工种管理 -->
<template>
  <div class="work-type">
    <template v-if="!addOrUpdateVisible && !detailVisible && !assignVisible && !selectionVisible && !noticeChangeVisible">
      <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="任务号">
          <el-input v-model="dataForm.orderCode" placeholder="" clearable/>
        </el-form-item>
         <el-form-item label="责任人">
          <el-input v-model="dataForm.responsibilityPerson" placeholder="" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryData()">查询</el-button>
          <el-button @click="noticeSettingHandle(null, false)">通知设置</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="100"
          align="center">
        </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="160"
          :show-tooltip-when-overflow="true"
          label="任务号">
        </el-table-column>
        <el-table-column
          prop="productName"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="物料名称">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          :formatter="formatState"
          label="状态">
        </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
          fixed="right"
          header-align="center"
          align="center"
          width="220"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="detailHandle(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="assignHandle(scope.row)">分派</el-button>
            <el-button type="text" size="small" @click="selectionHandle(scope.row, false)">选择清单</el-button>
            <el-button type="text" size="small" @click="importHandle(scope.row)">导入</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>
    <!-- 弹窗, 新增 / 修改 -->
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
    <assign v-if="assignVisible" ref="assign" @onChose="onChose"/>
    <selection v-if="selectionVisible" ref="selection" @onChose="onChose"/>
    <notice-change v-if="noticeChangeVisible" ref="noticeChange" @onChose="onChose"/>

    <el-dialog title="导入" :visible.sync="importVisible">
      <el-upload
        class="upload-demo"
        ref="upload"
        :on-remove="handleRemove"
        action="#"
        :limit="1"
        :file-list="fileList"
        :auto-upload="false"
        :http-request="handleUpload"
        v-loading="importLoading"
        >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button size="small" type="success" @click="submitUpload" >开始上传</el-button>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
  import Detail from './project-product-detail'
  import Assign from './project-product-assign'
  import Selection from './project-product-selection'
  import NoticeChange from './project-product-notice-change-setting'
  export default {
    name: 'file-manage',
    components: {
      Detail, Assign, Selection, NoticeChange
    },
    data () {
      return {
        addOrUpdateVisible: false,
        detailVisible: false,
        assignVisible: false,
        selectionVisible: false,
        importVisible: false,
        noticeChangeVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsLevel: [],
        importLoading: false,
        importData: {},
        fileList: [],
        stateOption: [
          {label: '待处理', value: '1'},
          {label: '已处理', value: '2'}
        ]
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
        this.detailVisible = false
        this.assignVisible = false
        this.selectionVisible = false
        this.noticeChangeVisible = false
      },
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.addOrUpdateVisible = false
  
        this.$http({
          url: this.$http.adornUrl(`/biz-service/projProduct/list`),
          method: 'get',
          params: this.$http.adornParams({
            'current': this.pageIndex,
            'size': this.pageSize,
            'orderCode': this.dataForm.orderCode,
            'projectName': this.dataForm.projectName,
            'responsibilityPerson': this.dataForm.responsibilityPerson
          })
        }).then(({data}) => {
          this.dataListLoading = false
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (item, disable) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(item, disable)
        })
      },
      // 详情
      detailHandle (item) {
        this.detailVisible = true
        item.stateStr = this.getStateStr(item.state)
        this.$nextTick(() => {
          this.$refs.detail.init(item)
        })
      },
      assignHandle (item) {
        this.assignVisible = true
        this.$nextTick(() => {
          this.$refs.assign.init(item)
        })
      },
      selectionHandle (item) {
        this.selectionVisible = true
        this.$nextTick(() => {
          this.$refs.selection.init(item)
        })
      },
      importHandle (item) {
        this.importVisible = true
        this.importData = item
      },
      // 上传
      submitUpload () {
        this.$refs.upload.submit()
      },
      // 移除
      handleRemove (file, fileList) {
        this.$emit('input', fileList)
      },
      handleUpload (file) {
        if (file == null) {
          this.$message.error('请上传文件')
          return
        }

        this.importLoading = true

        const formData = new FormData()
        formData.append('importFile', file.file)
        formData.append('productTechnologyId', this.importData.productTechnologyId)
        this.$http({
          url: this.$http.adornUrl(`/biz-service/projProduct/importExcel`),
          method: 'POST',
          data: formData,
          timeout: 0
        }).then(({data}) => {
          this.importLoading = false
          if (data && data.code === '200') {
            this.$message({
              message: '导入成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.importVisible = false
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      formatState (row) {
        if (!row.state) return ''
        
        return this.getStateStr(row.state)
      },
      getStateStr(state){
        let option = this.stateOption.find(t => t.value === state)
        if (option != null) {
          return option.label
        }
        return ''
      },
      noticeSettingHandle(){
        this.noticeChangeVisible = true
        this.$nextTick(() => {
          this.$refs.noticeChange.init()
        })
      }
    }
  }
</script>

<style scoped>

</style>
