<template>
  <div>
    <Modal v-model="showModal" width="1100" class="modal-footer-none zIndex-1100">
      <p slot="header">
        出库单
      </p>
      <div>
        <Row>
          <!--查询-->
          <Col span="24" />
          <Form inline onsubmit="return false" :label-width="70">
            <label class="label-sign" />
            <!--            <Form-item label="耗材编号:">-->
            <!--              <Input-->
            <!--                v-model="formObj.num"-->
            <!--                placeholder="请输入耗材编号"-->
            <!--                style="width: 200px"-->
            <!--                clearable-->
            <!--                @on-enter="_search"-->
            <!--              />-->
            <!--            </Form-item>-->
            <!--            <Form-item label="耗材名称:">-->
            <!--              <Input-->
            <!--                v-model="formObj.name"-->
            <!--                placeholder="请输入耗材名称"-->
            <!--                style="width: 200px"-->
            <!--                clearable-->
            <!--                @on-enter="_search"-->
            <!--              />-->
            <!--            </Form-item>-->

            <!--            <Form-item label="耗材类别:">-->
            <!--              <Input-->
            <!--                v-model="formObj.category"-->
            <!--                placeholder="请输入耗材类别"-->
            <!--                style="width: 200px"-->
            <!--                clearable-->
            <!--                @on-enter="_search"-->
            <!--              />-->
            <!--            </Form-item>-->

            <!--            <Form-item label="规格:">-->
            <!--              <Input-->
            <!--                v-model="formObj.spec"-->
            <!--                placeholder="请输入规格"-->
            <!--                style="width: 200px"-->
            <!--                clearable-->
            <!--                @on-enter="_search"-->
            <!--              />-->
            <!--            </Form-item>-->
            <!--            <Form-item class="search-btn">-->
            <!--              <Button type="primary" @click="_search">-->
            <!--                搜索-->
            <!--              </Button>-->
            <!--            </Form-item>-->
          </Form>
          </Col>
          <!--按钮-->
          <el-col :span="24">
            <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
          </el-col>
          <!-- 表格 -->
          <Col span="24" />
          <element-table
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            show-check-box
            @on-result-change="_tableResultChange"
          >
            <el-table-column
              v-for="item in pageColumns"
              :key="item.key"
              show-overflow-tooltip
              :prop="item.key"
              :label="item.title"
              :min-width="item.width"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
                <span v-if="item.key==='type'">
                  {{ scope.row[item.key] === 0 ? '流转' : scope.row[item.key] === 1 ? '退回': scope.row[item.key] === 2 ?'更新' :
                    scope.row[item.key] === 3 ? '转发':'文件' }}
                </span>
                <!--使用人-->
                <div v-else-if="item.applys" @click.stop="_handleRow(scope)">
                  <Select v-model="scope.row.applyId" label-in-value>
                    <Option v-for="item in salesmanList" :key="item.id" :value="item.id">
                      {{ item.name }}
                    </Option>
                  </Select>
                </div>

                <span v-else-if="item.key==='ctime'">{{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}</span>
                <span v-else>{{ scope.row[item.key] }}</span>
              </template>
            </el-table-column>
          </element-table>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>

      <Modal v-model="showReasonModal" :mask-closable="false">
        <p slot="header">
          {{ applyTitle }}
        </p>
        <div>
          <Form ref="applyFormObj" :model="applyFormObj" :rules="ruleValidate">
            <Form-item label="申请原因" prop="reason">
              <Input v-model="applyFormObj.reason" type="textarea" :rows="4" placeholder="请输入申请原因 ..." /></Input>
            </Form-item>
          </Form>
        </div>
        <div slot="footer">
          <Button type="ghost" style="margin-left: 8px" @click="_applyCancel">
            取消
          </Button>
          <Button type="primary" @click="_applyOk">
            确定
          </Button>
        </div>
      </Modal>

      <DeliverySubmit ref="deliverySubmitModal" @on-result-change="_search" />
    </Modal>
  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import global from '../../../../api/config'
import { equipDeliveryOrder, equipRelDelivery, sysUser } from '../../../../api'
import DeliverySubmit from '../out-apply-order/DeliverySubmit'
export default {
  components: {
    DeliverySubmit
  },
  data() {
    return {
      showModal: false,
      searchOpen: false,
      getPage: {},
      applyTitle: '',
      salesmanList: [],
      salesmanListTemp: [],
      selectIds: [],
      footerList: [
        { name: '取消', type: '' },
        { name: '草稿', type: 'success' },
        { name: '提交审批', type: 'primary' }
      ],
      btn: [
        { type: 'success', id: '', name: '批量移除' },
        { type: 'success', id: '', name: '导出' },
        { type: 'success', id: '', name: '打印' }
      ],
      applyFormObj: {
        remark: '' // 申请原因
      },
      pageColumns: [
        { title: '仪器编号', key: 'equipNum', width: 120, fixed: 'left' },
        { title: '仪器名称', key: 'equipName', width: 160, fixed: 'left' },
        {
          title: '使用人',
          key: 'applyId',
          width: 160,
          applys: true
        },
        {
          title: '使用开始时间',
          key: 'useStartTime',
          width: 180,
          dateTime1: true
        },
        {
          title: '使用结束时间',
          key: 'useEndTime',
          width: 180,
          dateTime: true
        },
        {
          title: '开始性能',
          key: 'performanceStart',
          width: 120,
          performanceStarts: true
        },
        {
          title: '结束性能',
          key: 'performanceEnd',
          width: 120,
          performanceEnd: true
        },
        {
          title: '用途',
          key: 'uses',
          width: 160,
          user: true
        },
        {
          title: '备注',
          key: 'remark',
          width: 210,
          remark: true
        },
        { title: '制造厂商', key: 'manufacturer', width: 200 },
        { title: '规格', key: 'spec', width: 200 }
      ],
      formObj: {
        num: '',
        category: '',
        spec: '',
        name: '',
        classifyId: ''
      },
      classifyId: '',
      showReasonModal: false,
      applyValue: '',
      ruleValidate: {
        remark: [
          { required: true, message: '申请原因不能为空', trigger: 'blur' }
        ]
      },
      submitData: {
        // 要提交至归类的数据
        classifyId: '',
        chemicalIds: []
      },
      currentRow: {},
      currentIndex: -1
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal') - 120
    }
  },
  methods: {
    // 点击当前行
    _handleRow(scope) {
      this.currentRow = scope.row
      this.currentIndex = scope.$index
    },
    _applyCancel() {
      this.showReasonModal = false
    },
    _applyOk() {
      // 申请原因提交
      if (
        this.applyFormObj.reason === '' ||
        undefined === this.applyFormObj.reason
      ) {
        this.$Message.error('表单验证失败!')
      } else {
        this._purOrder({
          approve: this.applyValue,
          reason: this.applyFormObj.reason
        })
        this.showReasonModal = false
      }
    },

    _purOrder: async function(data) {
      const result = await equipDeliveryOrder.purOrder(data)
      if (result) {
        this.$Message.success('操作成功！')
        this.showModal = false
        this.showReasonModal = false
        this.$emit('on-result-change')
      }
    },

    // 人员列表
    _getSalesmanList: async function() {
      const result = await sysUser.list({})
      const salesmanList = result
      const salesmanListTemp = []
      for (let i = 0; i < salesmanList.length; i++) {
        salesmanListTemp.push({
          name: salesmanList[i].realname,
          id: salesmanList[i].userId
        })
      }
      if (salesmanListTemp.indexOf(this.formObj.realname) === -1) {
        salesmanListTemp.push({
          name: global.getUserInfo().realname,
          id: global.getUserInfo().id
        })
      }
      this.salesmanList = salesmanListTemp
      console.log(this.salesmanList, 666)
    },

    _open() {
      this.showModal = true
      this._page()
      this._getSalesmanList()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const database = this.$serializeForm(this.formObj)
      const result = await equipRelDelivery.pageTemp(database)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },

    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '草稿':
          this._ok(false)
          break
        case '提交审批':
          this._ok(true)
          break
      }
    },
    _cancel() {
      this.showModal = false
      this.classifyId = ''
      this.submitData = []
      this.formObj = []
      this._hideLoading()
    },
    _ok(approve) {
      if (this.getPage.records.length === 0) {
        this.$Message.warning('请先添加仪器！')
      } else {
        this.showReasonModal = true
        this.applyValue = approve
        this.applyFormObj.reason = ''
        if (approve) {
          // 提交审批
          this.applyTitle = '申请单提交审批'
        } else {
          // 保存
          this.applyTitle = '申请单保存'
        }
        this._hideLoading()
        // this.$refs.deliverySubmitModal._open(approve)
      }
    },
    _save: async function(data) {
      const result = await equipRelDelivery.saveClassifyConsume(data)
      if (result) {
        this.$Message.success('归类成功！')
        this.showModal = false
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>
