<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <span>机床编号:</span>
        <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="searchTable1();machineChange()">
          <el-option
            v-for="item in machineNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <div style="float: left">{{ item.label }}</div>
            <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
          </el-option>
        </el-select>
      </el-row>
    </el-card>
    <el-card>
      <div v-for="(file, key) in hadFile" :key="key">
        <div class="wrapper">
          <div class="content">
            <div class="action"/>
            <div v-if="file.suffix==='pdf'" class="icon"><svg-icon icon-class="pdf" /></div>
            <div v-else-if="file.suffix==='docx'||file.suffix === 'doc'" class="icon"><svg-icon icon-class="doc" /></div>
            <div v-else-if="file.suffix==='xlsx' || file.suffix === 'xls'" class="icon"><svg-icon icon-class="excel" /></div>
            <div v-else-if="file.suffix==='pptx' || file.suffix === 'ppt'" class="icon"><svg-icon icon-class="ppt" /></div>
            <div v-else-if="file.suffix==='jpg' || file.suffix === 'jpeg'|| file.suffix === 'png'" class="icon"><svg-icon icon-class="pic" /></div>
            <div v-else class="icon"><svg-icon icon-class="file" /></div>
            <a :href="file.url" target="view_window"><div class="info">{{ file.name }}</div></a>
          </div>
        </div>
      </div>
    </el-card>
    <el-card>
      <h3 style="text-align: center;">机床信息</h3>
      <el-table :data="tableData1" border size="mini" highlight-current-row style="width: 100%;margin-top: 15px" height="200px">
        <el-table-column label="项目名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床编号" align="center">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="机床名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床数量" align="center">
          <template slot-scope="scope">
            {{ scope.row.机床数量 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <el-button size="small" icon="el-icon-circle-plus" type="primary" style="float:left;margin: 7px 0 0 0" @click="addTable()">增加装配计划</el-button>
      <el-table :data="tableData2" border size="mini" highlight-current-row style="width: 100%;margin-top: 15px" height="380px">
        <el-table-column label="组号" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-select v-model="scope.row.组件流水号" filterable clearable size="small" style="width: 100px" @change="groupChange(scope.row, scope.$index)">
                <el-option
                  v-for="item in groupNum"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </template>
            <template v-else>
              {{ scope.row.组号 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="任务名称" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-input v-model="scope.row.任务名称" size="mini" style="width: 100%"/>
            </template>
            <template v-else>
              {{ scope.row.任务名称 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="计划工时" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-input v-model="scope.row.计划工时" size="mini" style="width: 100%"/>
            </template>
            <template v-else>
              {{ scope.row.计划工时 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250px" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row.edit" type="success" plain size="small" @click="confirmEdit(scope.row)">确定</el-button>
            <el-button v-else type="primary" size="small" plain @click="scope.row.edit=!scope.row.edit">编辑</el-button>
            <el-button v-if="scope.row.edit" type="warning" plain size="small" @click="cancel(scope.row)">取消</el-button>
            <el-button type="danger" plain size="small" @click="deleteData(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>import { initProject, searchgroup, searchgroupName, getFileData, searchTable1, searchTable2, deletedata, editData, addData } from '@/api/Assembly/MakeAssemblyPlan'
import { ServerIP } from '@/utils/request'
// import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNum: [],
      tableData1: [],
      tableData2: [],
      hadFile: []
    }
  },
  created() {
  },
  mounted() {
    this.initProject()
  },
  methods: {
    getFileData(machineValue) {
      this.hadFile = []
      getFileData(machineValue).then(res => {
        if (res.data.length > 0) {
          res.data.map(item => {
            this.hadFile.push({
              url: `${ServerIP}${item.文件标识}.${item.文件后缀}`,
              name: `${item.文件名称}.${item.文件后缀}`,
              suffix: item.文件后缀,
              id: item.文件标识
            })
          })
        }
      })
    },
    groupChange(row, index) {
      this.groupNumberValue = ''
      this.tableData2[index].任务名称 = ''
      if (row.组件流水号 !== '' && row.组件流水号 !== null) {
        searchgroupName(row.组件流水号).then(response => {
          console.log(row, 22222)
          this.groupNumberValue = response.data[0].组号
          this.tableData2[index].任务名称 = response.data[0].组件名称
        })
      }
    },
    initProject() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    machineChange() {
      this.groupNumberValue = ''
      this.groupNum = []
      searchgroup(this.machineNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groupNum.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
      this.getFileData(this.machineNumberValue)
    },
    confirmEdit(row) {
      editData(row.计划流水号, row.组件流水号, this.groupNumberValue, row.任务名称, row.计划工时).then(response => {
        if (response.data[0].result === '1') {
          row.edit = false
          this.searchTable2()
          this.$message.success('编辑成功')
        } else {
          this.$message.error('编辑失败')
        }
      })
    },
    cancel(row) {
      row.edit = !row.edit
      this.searchTable2()
    },
    deleteData(row) {
      console.log(row.计划流水号)
      this.deleteRow(deletedata, row.计划流水号, function() { this.searchTable2() })
    },
    addTable() {
      if (this.machineNumberValue !== '') {
        addData(this.machineNumberValue, 1).then(response => {
          console.log(response)
          if (response.data[0].result === '1') {
            this.searchTable2()
            this.$message.success('增加成功')
          } else {
            this.$message.error('增加失败')
          }
        })
      } else {
        this.$message.warning('请选择机床')
      }
    },
    searchTable1() {
      this.tableData1 = []
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check2 = 1 } else { this.check2 = 0 }
      if (this.check2 === 0) {
        this.tableData1 = []
        this.tableData2 = []
      } else {
        searchTable1(this.machineNumberValue, this.check2).then(response => {
          this.tableData1 = response.data
        }).then(() => {
          this.searchTable2()
        })
      }
    },
    searchTable2() {
      this.tableData2 = []
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) {
        this.check2 = 1
        searchTable2(this.machineNumberValue, 1).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.tableData2.push({
              计划流水号: response.data[i].计划流水号,
              组件流水号: response.data[i].组件流水号,
              组号: response.data[i].组号,
              任务名称: response.data[i].任务名称,
              计划工时: response.data[i].计划工时,
              edit: false
            })
          }
        })
        console.log(this.tableData2, 222222)
      } else { this.check2 = 0 }
    }
  }
}

</script>

<style scoped>

</style>
