<template>

  <div class="app-container sign-and-weigh-box">

    <sales-return-dialog :propForm="propForm"
                         :visibility="isVisibility"
                         v-on:dialog-comfirm="comfirm"
                         v-on:dialog-cancle="cancle"></sales-return-dialog>

    <div class="filter-container">

      <el-row>
        <el-col :span="8">
          <div>
            <span>入库时间:</span>
            <el-date-picker v-model="return_time" prefix-icon="el-icon-date" :editable="false" style="width: 196px;"
                            type="date" placeholder="选择日期时间" align="right" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd"></el-date-picker>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-row>
      <el-col>
        <el-form :rules="tableRules" :model="recordInfo" ref="tableForm">
          <el-table :data="recordInfo.recordData" :class="signAndWeighBoxPatients" style="width: 100%" border
                    max-height="450"
          >

            <el-table-column label="操作" align="center" min-width="30">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)">+
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">-
                </el-button>
              </template>
            </el-table-column>

            <el-table-column min-width="35" align="center">

              <template slot="header" slot-scope="scope">
                <span>物品类型<span style="color: red">*</span></span>
              </template>

              <template slot-scope="scope">
                <el-form-item style="padding-top: 15px">
                  <el-input placeholder="请输入物品类型" v-model="scope.row.good_type_id"
                            :value="typeName(scope.row.good_type_id)"
                            @focus="showDialog(scope.$index, scope.row)"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column min-width="35" align="center">
              <template slot="header" slot-scope="scope">
                <span>规格名称<span style="color: red">*</span></span>
              </template>
              <template slot-scope="scope">

                <el-form-item style="padding-top: 15px">
                  <el-input placeholder="请输入规格名称" v-model="scope.row.good_id"
                            :value="specificationName(scope.row.good_id)"
                            @focus="showDialog(scope.$index, scope.row)"></el-input>
                </el-form-item>

              </template>
            </el-table-column>


            <el-table-column min-width="23" align="center">
              <template slot="header" slot-scope="scope">
                <span>退货数量<span style="color: red">*</span></span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'recordData.' + scope.$index + '.count'" :rules='tableRules.count'
                              style="padding-top: 17px">
                  <el-input type="number" v-model="scope.row.count"></el-input>
                </el-form-item>
              </template>
            </el-table-column>


          </el-table>
        </el-form>
      </el-col>


      <span class="dialog-footer" style="margin-top: 20px;float:right">
        <el-button @click="back()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>

    </el-row>
  </div>
</template>

<script>
  import { uParseTime } from '@/utils/tools'

  import {
    deleteCancelStockInfo,
    getCancelStockInfo,
    getCancelStockConfig,
    editCancelStockInfo
  } from '@/api/stock'
  import SalesReturnDialog from './Dialog/salesReturnDialog'

  export default {
    components: { SalesReturnDialog },
    name: 'cancelStockOrderEdit',

    data() {
      return {
        signAndWeighBoxPatients: 'sign-and-weigh-box-patients',
        warehousing_time: '',
        adminUserOptions: null,
        currentIndex: 0,
        return_time: '',
        recordInfo: {
          recordData: [],
          stock_in_code: '',
          current_index: ''

        },
        tableRules: {
          count: [
            { required: true, message: '数量不能为空', trigge: 'blur' }
          ]

        },
        ruleForm: {
          manufacturer: [
            { required: true, message: '请选择厂商', trigger: 'change' }
          ]

        },

        // prop
        isVisibility: false,
        propForm: {
          goodType: [],
          goodInfo: [],
          goodUnit: [],
          title: '入库'
        },

        form: {
          manufacturer: '',
          dealer: ''
        },

        cancelStock: {},
        manufacturer: [],
        dealer: [],
        goodType: []
      }
    },
    methods: {
      comfirm: function(val) {
        this.isVisibility = false
        if (val.selectedGoodInfo.length > 0) {
          for (let i = val.selectedGoodInfo.length - 1; ; i--) {
            if (i == 0) {
              this.recordInfo.recordData[this.currentIndex].good_type_id = val.goodTypeId
              this.recordInfo.recordData[this.currentIndex].good_id = val.selectedGoodInfo[i].id
            } else {
              const tempForm = {}
              tempForm['id'] = 0
              tempForm['good_type_id'] = val.goodTypeId
              tempForm['good_id'] = val.selectedGoodInfo[i].id
              tempForm['count'] = ''
              this.recordInfo.recordData.push(tempForm)
            }
          }
        }

        this.currentIndex = -1
      }, cancle: function() {
        this.isVisibility = false
      }, GetConfigInfo: function() {
        getCancelStockConfig().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            var warehouseOutInfoList = response.data.data.warehouseOutInfoList
            for (let i = 0; i < warehouseOutInfoList.length; i++) {
              this.propForm.goodInfo.push(warehouseOutInfoList[i].GoodInfo)
              this.propForm.goodType.push(warehouseOutInfoList[i].GoodInfo.GoodsType)

            }
            const obj3 = {}
            const obj4 = {}
            this.propForm.goodInfo = this.propForm.goodInfo.reduce((cur, next) => {
              obj4[next.id] ? '' : obj4[next.id] = true && cur.push(next)
              return cur
            }, [])
            this.propForm.goodType = this.propForm.goodType.reduce((cur, next) => {
              obj3[next.id] ? '' : obj3[next.id] = true && cur.push(next)
              return cur
            }, [])
          }
        })
      }, typeName: function(good_type_id) {
        let name = ''
        for (let i = 0; i < this.propForm.goodType.length; i++) {
          if (this.propForm.goodType[i].id == good_type_id) {
            name = this.propForm.goodType[i].type_name
          }
        }
        return name
      }, specificationName: function(good_info_id) {
        let name = ''
        for (let i = 0; i < this.propForm.goodInfo.length; i++) {
          if (this.propForm.goodInfo[i].id == good_info_id) {
            name = this.propForm.goodInfo[i].specification_name
          }
        }
        return name
      }, handleEdit: function(index, row) {
        const tempObj = {}
        tempObj['id'] = 0
        tempObj['good_type_id'] = 0
        tempObj['good_id'] = 0
        tempObj['count'] = ''
        this.recordInfo.recordData.push(tempObj)
      }, handleDelete: function(index, row) {
        if (row.id == 0) {
          this.recordInfo.recordData.splice(index, 1)
        } else {
          const params = {
            id: row.id
          }
          this.$confirm('确认删除该退库物品信息记录?', '删除退库物品信息记录', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteCancelStockInfo(params).then(response => {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                return false
              } else {
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.recordInfo.recordData.splice(index, 1)
              }
            })
          }).catch(() => {
          })
        }
      }, getTime(val, temp) {
        if (val != 0) {
          return uParseTime(val, temp)
        } else {
          return ''
        }
      }, showDialog(index, row) {
        this.currentIndex = index

        this.isVisibility = true
        console.log(this.form.manufacturer)
        for (let i = 0; i < this.propForm.goodInfo.length; i++) {
          if (this.propForm.goodInfo[i].manufacturer == this.form.manufacturer) {
            this.propForm.goodType.push(this.propForm.goodInfo[i].GoodsType)
          }
        }
        const obj3 = {}
        this.propForm.goodType = this.propForm.goodType.reduce((cur, next) => {
          obj3[next.id] ? '' : obj3[next.id] = true && cur.push(next)
          return cur
        }, [])

      }, back() {
        this.$router.go(-1)
      }, submit() {
        this.$refs['tableForm'].validate((valid) => {
          if (valid) {
            const array = this.recordInfo.recordData
            for (let i = 0; i < array.length; i++) {
              if (array[i].good_type_id == 0) {
                this.$message.error('物品类型不能为空')
                return
              }
              if (array[i].good_id == 0) {
                this.$message.error('规格名称不能为空')
                return
              }
            }

            const params = {
              'cancelStock': this.recordInfo.recordData
            }
            editCancelStockInfo(params, this.return_time, this.$route.query.id).then(response => {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                return false
              } else {
                this.$notify({
                  title: '成功',
                  message: '退库成功',
                  type: 'success',
                  duration: 2000
                })
                this.$router.back(-1)
              }
            })
          } else {
            return false
          }
        })
      }, GetOrderDetail: function(order_id) {
        const params = {
          'id': order_id
        }
        getCancelStockInfo(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            for (let i = 0; i < response.data.data.list.length; i++) {
              response.data.data.list[i].count =  response.data.data.list[i].count.toString();
              this.recordInfo.recordData.push(response.data.data.list[i])
            }
            this.cancelStock = response.data.data.info
            this.return_time = this.getTime(this.cancelStock.return_time, '{y}-{m}-{d}')
            this.form.manufacturer = this.cancelStock.manufacturer
            this.form.dealer = this.cancelStock.dealer
          }
        })
      }
    },
    created() {
      this.GetConfigInfo()
      this.propForm.goodUnit = this.$store.getters.good_unit
      const order_id = this.$route.query.id
      this.GetOrderDetail(order_id)
    }

  }
</script>

<style rel="stylesheet/css" lang="scss" scoped>
  .information {
    border: 1px #dcdfe6 solid;
    padding: 30px 20px 30px 20px;

  .border {
    border-bottom: 1px #dcdfe6 solid;
    margin: 0px 0 20px 0;
  }

  }

  .title {
    background: #409eff;
    height: 44px;
    line-height: 44px;
    padding: 0 0 0 10px;
    color: #fff;
    margin: 0 0 10px 0;
    border-radius: 4px 4px 0 0;
  }

  .edit_separater {
    border-top: 1px solid rgb(233, 233, 233);
    margin-top: 15px;
    margin-bottom: 15px;
  }

</style>

<style>
  .sign-and-weigh-box .sign-and-weigh-box-patients .cell {
    font-size: 12px;
  }

  .sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
    background: #6fb5fa;
  }

</style>
