<template>
  <div class="bg-in-stock">
    <el-form ref="orderReturnAddForm" :model="orderReturnAddForm" label-width="120px" inline style="width:1200px">
      <h3>退货单</h3>
      <el-form-item label="退货时间" prop="return_time">
        <el-date-picker v-model="orderReturnAddForm.return_time" value-format="timestamp" type="datetime"
          :disabled="type === 'detail' || type === 'audit'" placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属部门">
        <el-cascader v-model="orderReturnAddForm.dept_id" :options="deptList"
          :disabled="type === 'detail' || type === 'audit'" :props="{
            label: 'name',
            value: 'id',
            emitPath: false,
            checkStrictly: true
          }" clearable @change="changewarehouseList"></el-cascader>
      </el-form-item>
      <el-form-item label="仓库">
        <el-select v-model="orderReturnAddForm.warehouse_id" :disabled="type === 'detail' || type === 'audit'">
          <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="销售订单" style="white-space: nowrap;">
        <el-button v-if="type !== 'detail'" class="button" size="mini" type="primary" @click="handleSelectOrderSn"
          :disabled="type === 'audit'">选择</el-button>
        <span v-if="orderReturnAddForm.order_sn">{{ orderReturnAddForm.order_sn }}</span>
      </el-form-item>
      <el-form-item label="经手人" prop="handle_by">
        <!-- <el-input v-model="orderReturnAddForm.handle_by" :minlength="2" :maxlength="20" clearable
          :disabled="type === 'detail' || type === 'audit'" placeholder="请输入经手人"></el-input> -->
        <el-select v-model="orderReturnAddForm.handle_by_id" :disabled="type === 'detail' || type === 'audit'">
          <el-option v-for="item in adminList" :key="item.id" :label="item.real_name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item></el-form-item>
      <el-form-item label="退货说明" prop="return_remark" style="width: 900px;">
        <el-input style="width:525px" v-model="orderReturnAddForm.return_remark" type="textarea"
          :disabled="type === 'detail' || type === 'audit'" :autosize="{ minRows: 3, maxRows: 4 }"
          placeholder="请输入退货说明"></el-input>
      </el-form-item>
      <div class="mx">
        <h3>退货明细</h3>
      </div>
      <div style="display:block">
        <el-form-item>
          <el-table :data="orderReturnAddForm.item_list" border :cell-style="{ textAlign: 'center' }"
            :header-cell-style="{ textAlign: 'center' }" style="width: 100%; margin-top: 20px;">
            <el-table-column label="商品编号" width="150">
              <template slot-scope="scope">
                {{ scope.row.product_sn }}
              </template>
            </el-table-column>
            <el-table-column label="商品名称" width="150">
              <template slot-scope="scope">
                {{ scope.row.product_name }}
              </template>
            </el-table-column>
            <el-table-column label="规格型号" width="150">
              <template slot-scope="scope">
                {{ scope.row.product_specification }}
              </template>
            </el-table-column>
            <el-table-column label="单位" width="150">
              <template slot-scope="scope">
                {{ scope.row.product_unit }}
              </template>
            </el-table-column>
            <el-table-column label="单价" width="150">
              <template slot-scope="scope">
                ¥{{ scope.row.product_price }}
              </template>
            </el-table-column>
            <el-table-column label="购买数量" width="80">
              <template slot-scope="scope">
                {{ scope.row.out_num ? scope.row.out_num : scope.row.order_num }}
              </template>
            </el-table-column>
            <el-table-column label="退货数量" width="150">
              <template slot-scope="scope">
                <el-input maxlength="12" v-model="scope.row.return_num"
                  :disabled="type === 'detail' || type === 'audit'" />
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </div>
    </el-form>
    <div class="footer">
      <el-button v-if="type === 'audit'" type="primary" @click="auditorderReturnForm('AuditPass')">审核通过
      </el-button>
      <el-button v-if="type === 'audit'" type="danger" @click="auditorderReturnForm('AuditReject')">审核驳回
      </el-button>
      <el-button v-if="type !== 'audit' && type !== 'detail'" type="primary" @click="submitorderReturnAddForm">保存
      </el-button>
      <el-button @click="roBack()">返回
      </el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import * as API_Setting from '@/api/setting'
import * as API_BasicSetting from '@/api/basicSetting'
import * as API_orderReturn from '@/api/orderReturn'
import { Foundation } from '~/ui-utils'
import * as API_Auth from '@/api/auth'
export default {
  name: 'orderReturnAdd',
  data() {
    return {
      adminList: [],
      fileList: [],
      deptList: [],
      id: '',
      warehouseList: [],
      /** 商品列表 */
      goodsList: [],
      /** 供应商退货表单*/
      orderReturnAddForm: {
        order_sn: '',
        order_id: '',
        item_list: []
      },
      type: ''
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  mounted() {
    this.GET_DeptList()
    this.GET_AdministratorList()
  },
  watch: {
    $route: {
      immediate: true,
      handler(newVal) {
        if (this.$route.name === 'orderReturnAudit') {
          this.type = 'audit'
        } else if (this.$route.name === 'orderReturnAdd') {
          this.orderReturnAddForm = {
            order_sn: '',
            order_id: '',
            item_list: []
          }
          this.orderReturnAddForm.handle_by_id = this.user.uid
          this.orderReturnAddForm.dept_id = this.user.dept_id
          this.GET_WarehouseList({ dept_id: this.user.dept_id })
          this.$set(this.orderReturnAddForm, 'return_time', new Date().getTime())
          this.type = 'add'
        } else if (this.$route.name === 'orderReturnDetail') {
          this.type = 'detail'
        }
        if (this.$route.params.id) {
          this.id = this.$route.params.id
          this.orderReturnAddForm.id = this.$route.params.id
          this.GET_orderReturnDetail()
        }
      }
    }
  },
  methods: {
    /** 获取人员列表 */
    GET_AdministratorList() {
      API_Auth.getAdministratorList({
        page_no: 1,
        page_size: 9999,
        user_state: 0
      }).then(response => {
        this.adminList = response.data
      })
    },
    /** 选择订单编号 */
    async handleSelectOrderSn() {
      const orders = await this.$EnPickerOrder({
        orderApi: 'admin/erp/order',
        selectedIds: [this.orderReturnAddForm.order_sn],
        orderApiParams: { ship_flag: true },
        limit: 1
      })
      this.orderReturnAddForm.order_sn = orders[0].sn
      this.orderReturnAddForm.order_id = orders[0].id
      if (orders) {
        API_orderReturn.getWarehouseOutProduct({ order_sn: orders[0].sn }).then(response => {
          const product_list = response.data.map(item => {
            item.warehouse_out_item_id = item.id
            return item
          })
          this.orderReturnAddForm.item_list = response.data
        })
      }
    },
    changewarehouseList(e) {
      this.$set(this.orderReturnAddForm, 'warehouse_id', '')
      this.GET_WarehouseList({ dept_id: e })
    },
    /** 获取仓库 */
    GET_WarehouseList(e) {
      API_BasicSetting.getWarehouseListAll(e).then(response => {
        this.warehouseList = response
      })
    },
    auditorderReturnForm(status) {
      this.$confirm('确定吗？', '提示', { type: 'warning' }).then(() => {
        API_orderReturn.auditorderReturn(this.id, { status: status }).then(response => {
          this.$message.success('审核完成')
          const { callback } = this.$route.params
          if (typeof callback === 'function') callback()
          this.$store.dispatch('delCurrentViews', {
            view: this.$route,
            $router: this.$router
          })
          this.$router.push({ name: 'orderReturn' })
        })
      }).catch(() => { })
    },
    roBack() {
      const { callback } = this.$route.params
      if (typeof callback === 'function') callback()
      this.$store.dispatch('delCurrentViews', {
        view: this.$route,
        $router: this.$router
      })
      this.$router.push({ name: 'orderReturn' })
    },
    submitorderReturnAddForm() {
      const { id } = this.orderReturnAddForm
      const params = this.MixinClone(this.orderReturnAddForm)
      params.return_time = Math.floor(params.return_time / 1000)
      if (id) {
        API_orderReturn.editorderReturn(id, params).then(response => {
          this.$message.success('修改成功！')
          const { callback } = this.$route.params
          if (typeof callback === 'function') callback()
          this.$store.dispatch('delCurrentViews', {
            view: this.$route,
            $router: this.$router
          })
          this.$router.push({ name: 'orderReturn' })
        })
      } else {
        API_orderReturn.addorderReturn(params).then(() => {
          this.$message.success('添加成功！')
          const { callback } = this.$route.params
          if (typeof callback === 'function') callback()
          this.$store.dispatch('delCurrentViews', {
            view: this.$route,
            $router: this.$router
          })
          this.$router.push({ name: 'orderReturn' })
        })
      }
    },
    /** 获取部门 */
    GET_DeptList() {
      API_Setting.getDeptList().then(response => {
        this.deptList = Foundation.buildTree(response, '0')
      })
    },
    /** 获取采购计划详情页面数据 */
    GET_orderReturnDetail() {
      API_orderReturn.getorderReturnInfo(this.id).then(response => {
        this.orderReturnAddForm = response
        this.GET_WarehouseList({ dept_id: response.dept_id })
        this.orderReturnAddForm.return_time = Math.floor(response.return_time * 1000)
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
/** 底部步骤 */
.footer {
  width: 100%;
  padding: 10px;
  bottom: 0px;
  text-align: center;
  z-index: 999;
}

.mx {
  display: flex;
  align-items: center;

  .button {
    margin-left: 10px;
  }
}

.bg-in-stock {
  background-color: #fff;
  margin: 10px;
  padding: 25px;
}

.goods-info {
  display: flex;

  .goods-name-box {
    text-align: left;

    .goods-name {
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      line-height: 16px;
    }

    .specs {
      color: #999999;
    }
  }
}

/deep/ {
  .el-input--suffix .el-input__inner {
    padding-right: 10px;
  }

  .error-input {
    .el-input__inner {
      border: 1px solid red;
    }
  }
}

/deep/ .el-form-item {
  width: 380px;
}
</style>
