<!--外协厂家外协厂家外协厂家外协厂家外协厂家外协厂家外协厂家外协厂家外协厂家外协厂家外协厂家-->
<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 7px; margin-bottom: 7px">
        <el-select v-model="selectTheOutsourcingManufacturersValue" style="width:150px;" placeholder="外协厂家" size="small" filterable clearable @change="pageCurrent = 1;pageSize = 100;searchTable()">
          <el-option
            v-for="item in selectTheOutsourcingManufacturers"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-input v-model="partsName" style="width:150px" placeholder="名称/图号" size="small" clearable @keyup.enter.native="pageCurrent = 1;pageSize = 100;searchTable()"/>
        <span style="font-size: 13px;color: black">外协时间</span>
        <el-date-picker
          v-model="dateRange"
          size="small"
          type="daterange"
          align="center"
          style="width: 240px;"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="～"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="pageCurrent = 1;pageSize = 100;searchTable()"/>
        <el-button icon="el-icon-search" type="primary" plain size="small" style="margin-left: 10px" @click="pageCurrent = 1;pageSize = 100;searchTable()">查询</el-button>
        <el-button type="warning" plain icon="el-icon-plus" size="small" @click="editType('form')">记录</el-button>
      </div>
    </el-card>
    <el-card>
      <el-table v-loading="loading" :data="tableData" highlight-current-row border stripe size="small" style="width: 950px" height="750">
        <el-table-column type="index" label="序号" align="center" width="50"/>
        <el-table-column width="150px" label="模具名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.名称 }}
          </template>
        </el-table-column>
        <el-table-column width="150px" label="图号或型号" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.代号 }}
          </template>
        </el-table-column>
        <el-table-column :width="setColumnWidth('数量')" label="数量" align="center">
          <template slot-scope="scope">
            {{ scope.row.数量 }}
          </template>
        </el-table-column>
        <el-table-column :width="setColumnWidth('外协厂家')" label="外协厂家" align="center">
          <template slot-scope="scope">
            {{ scope.row.外协厂家名称 }}
          </template>
        </el-table-column>
        <el-table-column :width="setColumnWidth('日期')" label="外协时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.外协时间 }}
          </template>
        </el-table-column>
        <el-table-column :width="setColumnWidth('日期')" label="送回时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.送回时间 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row)"/>
            <el-button type="text" icon="el-icon-top" size="mini" @click="uploadPicture(scope.row)">照片</el-button>
            <el-button size="mini" type="text" icon="el-icon-picture-outline" @click="handlePicture(scope.row)"/>
            <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChanges"
          @current-change="handleCurrentChanges"/>
      </div>
      <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible" :title="title" center width="660px">
        <el-form ref="form" :model="form" :rules="rules" label-position="center" label-width="100px" class="demo-ruleForm">
          <el-form-item v-if="title === '增加记录'" label="模具名称" prop="模具名称" style="display: inline-block;">
            <el-input v-model="form.模具名称" style="width:180px;display: inline-block" placeholder="双击选择" size="small" readonly @dblclick.native="getMaterial"/>
          </el-form-item>
          <el-form-item v-if="title === '增加记录'" label="图号或型号" prop="图号或型号" style="display: inline-block;margin-left: 10px">
            <el-input v-model="form.图号或型号" style="width:180px;display: inline-block" placeholder="图号或型号" size="small" readonly/>
          </el-form-item>
          <el-form-item label="外协厂家" prop="外协厂家" style="display: inline-block">
            <el-select v-model="form.外协厂家" style="width:180px;display: inline-block" placeholder="外协厂家" size="small" filterable clearable>
              <el-option
                v-for="item in selectTheOutsourcingManufacturers"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="数量" prop="数量" style="display: inline-block;margin-left: -4px">
            <el-input-number v-model="form.数量" style="width:180px;display: inline-block;margin-left: 14px" placeholder="数量" size="small" clearable/>
          </el-form-item>
          <el-form-item label="外协时间" prop="外协时间" style="display: inline-block">
            <el-date-picker
              v-model="form.外协时间"
              size="small"
              align="center"
              style="width: 180px;display: inline-block"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="选择日期"/>
          </el-form-item>
          <el-form-item label="送回时间" prop="送回时间" style="display: inline-block;;margin-left: 10px">
            <el-date-picker
              v-model="form.送回时间"
              size="small"
              align="center"
              style="width: 180px;display: inline-block"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="选择日期"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="title === '增加记录'" @click="callOff('form')">取消</el-button>
          <el-button v-if="title === '增加记录'" type="primary" @click="submit('form')">确定</el-button>
          <el-button v-if="title === '编辑记录'" @click="callOff('form')">取消</el-button>
          <el-button v-if="title === '编辑记录'" type="primary" @click="submitUpdate('form')">确定</el-button>
        </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible5" width="673px">
        <el-row style="margin-bottom: 10px">
          <span>名称：</span>
          <el-input v-model="materialName" placeholder="名称" style="width: 170px" size="small" clearable/>
          <span>图号/型号：</span>
          <el-input v-model="materialSpec" placeholder="图号/型号" style="width: 170px" size="small" clearable/>
          <el-button type="success" size="small" icon="el-icon-search" style="margin-left:10px" @click="pageCurrent1=1;pageSize1=10;total1=0;searchMaterial()">查询</el-button>
        </el-row>
        <el-table
          :data="tableData1"
          :header-cell-style="{background: '#383E4B',color: 'white'}"
          highlight-current-row
          border
          stripe
          size="small"
          style="width: 621px"
          height="510px"
          @row-dblclick="selectMaterial">
          <el-table-column align="center" label="名称">
            <template slot-scope="scope">
              {{ scope.row.名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="图号/型号">
            <template slot-scope="scope">
              {{ scope.row.代号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="库存">
            <template slot-scope="scope">
              {{ scope.row.库存 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="材料">
            <template slot-scope="scope">
              {{ scope.row.材料 }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin: 10px 0;">
          <el-pagination
            :current-page="pageCurrent1"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize1"
            :total="total1"
            :pager-count="5"
            style="display:inline-block;width:50%"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange1"
            @current-change="handleCurrentChange1"/>
        </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible2" title="上传图片" center width="320px">
        <el-upload
          id="invoiceScan"
          ref="upload"
          :multiple="true"
          :auto-upload="false"
          :on-success="uploadSuccess"
          :on-exceed="warningExceed"
          :limit="limit"
          :data="Data"
          :action="upload"
          style="margin-left: 60px"
          list-type="picture-card">
          <i class="el-icon-plus"/>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible2=false">取消</el-button>
          <el-button type="distribution" size="small" @click="submit2('form')">确定</el-button>
        </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible1" title="图片" center style="" width="600px">
        <div v-for="(url, key) in urls" :key="key" style="margin-bottom: 25px">
          <!--        <div class="action">-->
          <!--          <a class="delete" @click="deleteFileData1(url.id)">删除</a>-->
          <!--        </div>-->
          <el-button type="danger" size="mini" style="float: right;margin-bottom: 5px;margin-right: 10px" @click="deleteFileData1(url.id)">删除</el-button>
          <el-image :key="url.url" :src="url.url" :preview-src-list="urls1" lazy style="width: 500px; height: 500px"/>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { url } from '@/utils/request'
export default {
  data() {
    return {
      Data: {},
      urls: [],
      urls1: [],
      limit: 1000,
      upload: url,
      purchaseContractSerialNumber: '',
      selectTheOutsourcingManufacturersValue: '',
      selectTheOutsourcingManufacturers: [],
      partsName: '',
      dateRange: '',
      form: {
        模具名称: '',
        图号或型号: '',
        外协厂家: '',
        数量: '',
        外协时间: '',
        送回时间: '',
        物料流水号: ''
      },
      rules: {
        模具名称: [{ required: true, message: '请输入模具名称', trigger: 'change' }],
        图号或型号: [{ required: true, message: '请输入图号或型号', trigger: 'blur' }],
        外协厂家: [{ required: true, message: '请选择外协厂家', trigger: 'change' }],
        数量: [{ required: true, message: '请输入数量', trigger: 'blur' }]
      },
      moldValue: '',
      title: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible5: false,
      materialName: '',
      materialSpec: '',
      loading: false, // 数据加载
      total: 0, // 分页总数
      total1: 0, // 分页总数
      tableData: [], // 合同信息表
      tableData1: [], // 合同信息表
      pageCurrent: 1, // 分页当前页
      pageCurrent1: 1, // 分页当前页
      pageSize: 100, // 分页每页显示条数
      pageSize1: 100 // 分页每页显示条数
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.searchTable()
    this.getSelectTheOutsourcingManufacturers()
  },
  methods: {
    getMaterial() {
      this.dialogFormVisible5 = true
    },
    warningExceed() {
      this.$message.error('仅可上传五张照片')
    },
    uploadSuccess(res) { // 上传成功回调
      this.$message.success('上传成功')
      this.dialogFormVisible2 = false
      this.$refs.upload.clearFiles()
    },
    submit2() {
      var param = []
      param[0] = ['模具记录流水号', this.moldValue]
      param[1] = ['文件名称', '']
      param[2] = ['文件后缀', '']
      param[3] = ['文件内容', null]
      var Data1 = this.CreateData('15', '仓储管理_模具_上传图片', param)
      this.Data.param = Data1
      this.$refs.upload.submit()
      this.dialogFormVisible2 = false
      this.$nextTick(() => {
        this.searchTable()
      })
    },
    uploadPicture(row) {
      this.dialogFormVisible2 = true
      this.moldValue = row.模具记录流水号
    },
    handlePicture(row) {
      this.dialogFormVisible1 = true
      this.purchaseContractSerialNumber = row.模具记录流水号
      this.urls = []
      this.urls1 = []
      var param = []
      param[0] = ['模具记录流水号', row.模具记录流水号]
      var Data = this.CreateData('11', '仓储管理_模具_图片_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data[i].文件内容 !== null) {
            this.urls.push({
              url: 'data:image/png;base64,' + response.data[i].文件内容,
              id: response.data[i].id
            })
            this.urls1.push('data:image/png;base64,' + response.data[i].文件内容)
          }
        }
      })
    },
    deleteFileData1(id) {
      // console.log(id, '图片iad')
      var param = []
      param[0] = ['id', id]
      var Data = this.CreateData('12', '仓储管理_模具_图片_删除数据', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('图片删除成功')
          this.urls = []
          this.urls1 = []
          var param1 = []
          param1[0] = ['模具记录流水号', this.purchaseContractSerialNumber]
          var Data1 = this.CreateData('11', '仓储管理_模具_图片_查询数据', param1)
          this.ExecDatabase(Data1).then(response => {
            for (let i = 0; i < response.data.length; i++) {
              if (response.data[i].文件内容 !== null) {
                this.urls.push({
                  url: 'data:image/png;base64,' + response.data[i].文件内容,
                  id: response.data[i].id
                })
                this.urls1.push('data:image/png;base64,' + response.data[i].文件内容)
              }
            }
          })
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    selectMaterial(row) {
      this.form.模具名称 = row.名称
      this.form.图号或型号 = row.代号
      this.form.物料流水号 = row.物料流水号
      this.dialogFormVisible5 = false
    },
    getSelectTheOutsourcingManufacturers() {
      var param = []
      var Data = this.CreateData('11', '外协厂家_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.selectTheOutsourcingManufacturers.push({
            label: response.data[i].外协厂家简称,
            value: response.data[i].外协厂家流水号
          })
        }
      })
    },
    editType(formName) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.模具名称 = ''
      this.form.图号或型号 = ''
      this.form.物料流水号 = ''
      this.title = '增加记录'
      this.dialogFormVisible = true
    },
    // 增加删除
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 增加确定
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['物料流水号', this.form.物料流水号]
          param[1] = ['外协厂家', this.form.外协厂家]
          param[2] = ['外协时间', this.form.外协时间]
          param[3] = ['送回时间', this.form.送回时间]
          param[4] = ['数量', this.form.数量]
          var Data = this.CreateData('12', '仓储管理_模具记录_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '信息增加成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('信息增加失败')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['模具记录流水号', row.模具记录流水号]
        var Data = this.CreateData('12', '仓储管理_模具记录_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            if (this.tableData && this.tableData.length === 1 && this.pageList > 1) { // 判断删除的是否是最后一页最后一行，如果是且不是第一页，页数减一
              this.pageList--
            }
            this.searchTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchTable() {
      let selectTheOutsourcingManufacturersValue_check, dateRange_check, partsName_check
      this.selectTheOutsourcingManufacturersValue ? selectTheOutsourcingManufacturersValue_check = 1 : selectTheOutsourcingManufacturersValue_check = 0
      this.partsName ? partsName_check = 1 : partsName_check = 0
      this.dateRange ? dateRange_check = 1 : (dateRange_check = 0, this.dateRange = '')
      var param = []
      param[0] = ['外协厂家流水号_check', selectTheOutsourcingManufacturersValue_check]
      param[1] = ['外协厂家流水号', this.selectTheOutsourcingManufacturersValue]
      param[2] = ['物料名称_check', partsName_check]
      param[3] = ['物料名称', this.partsName]
      param[4] = ['外协时间_check', dateRange_check]
      param[5] = ['开始时间', this.dateRange[0]]
      param[6] = ['结束时间', this.dateRange[1]]
      var Data = this.CreateData('11', '仓储管理_模具记录_查询', param, this.pageSize, this.pageCurrent)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    handleEdit(row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '编辑记录'
      this.form.模具名称 = row.名称
      this.form.图号或型号 = row.代号
      this.form.物料流水号 = row.物料流水号
      this.form.数量 = row.数量
      this.form.外协时间 = row.外协时间
      this.form.送回时间 = row.送回时间
      this.form.外协厂家 = Number(row.外协厂家流水号)
      this.moldValue = row.模具记录流水号
      this.dialogFormVisible = true
    },
    submitUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['数量', this.form.数量]
          param[1] = ['外协厂家', this.form.外协厂家]
          param[2] = ['外协时间', this.form.外协时间]
          param[3] = ['送回时间', this.form.送回时间]
          param[4] = ['模具记录流水号', this.moldValue]
          var Data = this.CreateData('12', '仓储管理_模具记录_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('编辑失败')
            }
          })
        }
      })
    },
    sortChange(column) {
      if (column.prop === '状态') {
        this.orderName = 1
      }
      if (column.order === 'ascending') {
        this.order = 1
      } else if (column.order === 'descending') { this.order = 2 } else {
        this.order = ''
      }
      this.searchTable()
    },
    searchMaterial() {
      let materialName_check, materialSpec_check
      this.materialName ? materialName_check = 1 : materialName_check = 0
      this.materialSpec ? materialSpec_check = 1 : materialSpec_check = 0
      var param = []
      param[0] = ['名称_check', materialName_check]
      param[1] = ['名称', this.materialName]
      param[2] = ['代号_check', materialSpec_check]
      param[3] = ['代号', this.materialSpec]
      var Data = this.CreateData('11', '物料信息_查询新', param, this.pageSize1, this.pageCurrent1)
      this.ExecDatabase(Data).then(response => {
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchMaterial()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchMaterial()
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

