<template>
  <!-- 未确定订单 -->
  <div>
    <div ref="grabble">
      <actionBar :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm" @openModel="openModel"
        @searchProtocol="searchProtocol"></actionBar>
    </div>
    <div class="card_content">
      <!-- 表单 -->
      <tables ref="refWaybill" controlswidth="450px" tableHeight="68vh" :suspension="true" :tableList="tableList"
        :tableData="tableData" @buttonData="buttonData">
      </tables>
      <!-- 分页 -->
      <div v-if="Total">
        <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
          @changeCurrent="changeCurrent">
        </pagination>
      </div>
    </div>
    <!-- 订单详情 -->
    <orderDetails ref="order" :waybillData="waybillData"></orderDetails>
    <!-- 处方 -->
    <prescriptionPicture ref="picture" :srcList="srcList" :fileList="fileList" :orderID="orderId"
      @prescription="prescription" @closeDialog="closeDialog"></prescriptionPicture>
    <!-- 添加编辑订单 -->
    <addEditOrder ref="orderAddEdit" :orderId="orderId" :staffTitle="staffTitle" :orderInformation="orderInformation"
      :operationType="operationType" @refreshList="refreshList">
    </addEditOrder>
    <el-dialog title="转门店" :visible.sync="signDialogVisible" width="500px" :close-on-click-modal="false"
      @close="closeDialog1">
      <forms ref="signRules" :formNewList="signRuleList" :ruleForm="signRuleForm" labelWidth="80px"
        @remoteMethod="remoteSearch">
      </forms>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="signDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="signConfirmLoading" @click="signHandleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getCreatesole,
    getWaybill,
    addWaybill,
    putWaybill,
    putUpdateStatus,
    putturnarounds
  } from '@/api/waybill'
  import {
    getAddress
  } from '@/api/address'
  import {
    getUser
  } from '@/api/user'
  import {
    getCompanyDept
  } from '@/api/company'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import orderDetails from '@/components/orderDetails'
  import prescriptionPicture from '@/components/prescriptionPicture'
  import waybillForm from '@/components/waybillForm'
  import forms from '@/components/forms'
  import addEditOrder from '@/components/addEditOrder'
  import {
    employee,
    formRules,
    consignmentList,
    addressBook,
    signList,
  } from "./uncertain.js";
  import {
    WaybillStatus
  } from '@/assets/js/blockSort'
  export default {
    name: 'OrderManagement',
    components: {
      actionBar,
      tables,
      pagination,
      orderDetails,
      prescriptionPicture,
      waybillForm,
      forms,
      addEditOrder
    },
    data() {
      return {
        operateList: [{
          type: 'sends',
          title: '批量确定',
          icon: 'el-icon-thumb',
          colour: 'success'
        }, {
          type: 'add',
          title: '添加订单',
          icon: 'el-icon-plus',
        }],
        formList: [{
          type: 'input',
          label: '订单编号',
          field: 'waybillNo',
          placeholder: '请输入订单编号',
        }, {
          type: 'input',
          label: '配送员姓名',
          field: 'delivery_name',
          placeholder: '请输入配送员姓名',
        }, {
          type: 'input',
          label: '患者名称',
          field: 'Consignee_address_name',
          placeholder: '请输入患者名称',
        }, {
          type: 'input',
          label: '患者手机号',
          field: 'Consignee_address_phone',
          placeholder: '请输入患者手机号',
        }],
        searchRuleForm: {
          waybillNo: '',
          delivery_name: '',
          Consignee_address_name: '',
          Consignee_address_phone: '',
        },
        searchValue: {},
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        Total: 0,
        operationType: '',
        tableData: [],
        tableList: [],
        btnData: [{
          type: 'confirm',
          label: '确定',
          icon: 'el-icon-thumb',
          style: 'success',
        }, {
          type: 'logs',
          label: '详情',
          icon: 'el-icon-tickets',
          style: 'success',
        }, {
          type: 'send',
          label: '处方',
          icon: 'el-icon-tickets',
          style: 'primary',
        }, {
          type: 'edit',
          label: '修改',
          icon: 'el-icon-edit',
          style: 'success',
        }, {
          type: 'sign',
          label: '转门店',
          icon: 'el-icon-share',
          style: 'warning',
        }, {
          type: 'delOrder',
          label: '取消订单',
          icon: 'el-icon-delete',
          style: 'danger',
        }],
        waybillData: {},
        srcList: [],
        fileList: [],
        delivererName: '',
        orderId: null,

        signDialogVisible: false,
        signRuleList: signList(),
        signRuleForm: {
          deptId: '',
          // rejectionReason: '',
        },
        staffTitle: '',
        signConfirmLoading: false,
        orderInformation: {},
      }
    },
    mounted() {
      let arr = employee()
      arr.forEach(item => {
        if (item.field == 'unpackBtn') {
          item.labelButton = this.btnData
        }
      })
      this.tableList = arr
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.Pagination.PageIndex = 1
        this.searchValue = value
        if (value.time) {
          this.searchValue.orderStartTime = value.time[0]
          this.searchValue.orderEndTime = value.time[1]
        }
        this.getList()
      },
      // 获取订单列表
      getList() {
        var params = {
          status: 1,
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        delete params.time
        getWaybill(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.tableData.forEach(item => {
              if (item.iceRaftCode) {
                item.iceRaftCode = item.iceRaftCode.toString()
              }
              item.visible = false
            })
            this.Total = res.data.count
          }
        })
      },
      openModel(type) {
        this.operationType = type
        if (type == 'add') {
          this.orderInformation = {}
          this.$refs.orderAddEdit.staffDialogVisible = true
        } else if (type == 'sends') {
          const arrID = this.$refs.refWaybill.waybillIds
          let arrList = []
          arrID.forEach(item => {
            arrList.push(item.id)
          })
          if (arrList.length != 0) {
            this.modifyOrderStatus(arrList)
          } else {
            this.$message({
              message: '请先选择需要批量操作的运单',
              type: 'warning'
            });
          }
        }
      },
      picture(arr) {
        var imgList = JSON.parse(JSON.stringify(arr))
        var imgArr = []
        imgList.forEach((item, index) => {
          imgArr.push(item.url)
        })
        var imgString = imgArr.join()
        return imgString
      },
      buttonData(row, type) {
        this.operationType = type
        this.orderId = row.id
        if (type == 'logs') {
          this.$refs.order.staffDialogVisible = true
          this.$refs.order.tableData = row.drugs
          this.waybillData = row
          this.waybillData.coolerBoxName = row.coolerBox.name
          this.waybillData.courierName = row.delivery.nickName
          this.waybillData.courierPhone = row.delivery.phone
          this.waybillData.reCheckName = row.reCheck.nickName
          this.waybillData.reCheckPhone = row.reCheck.phone
        } else if (type == 'send') {
          if (row.prescription) {
            this.srcList = row.prescription
          }
          this.$refs.picture.prescriptionVisible = true
        } else if (type == 'confirm') {
          let arrOrderId = []
          arrOrderId.push(row.id)
          this.modifyOrderStatus(arrOrderId, 2)
        } else if (type == 'edit') {
          this.staffTitle = '修改'
          this.orderInformation = {}
          this.$refs.orderAddEdit.staffDialogVisible = true
          this.$nextTick(() => {
            this.orderInformation = row
          })
        } else if (type == 'sign') {
          this.signDialogVisible = true
          this.waybillNo = row.waybillNo
          this.getAcquireStore()
        } else if (type == 'delOrder') {
          this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let arrOrderId = []
            arrOrderId.push(row.id)
            putUpdateStatus({
              id: arrOrderId,
              status: 8,
            }).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
            })
          }).catch(() => {})
        }
      },
      // 添加修改刷新列表
      refreshList() {
        this.getList()
      },
      // 修改订单状态
      modifyOrderStatus(arrID, type) {
        putUpdateStatus({
          id: arrID,
          status: type,
        }).then(res => {
          if (res.code == 200) {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.getList()
          }
        })
      },
      // 转门店
      signHandleAdd() {
        let flag = this.$refs['signRules'].validateForm();
        if (flag) {
          this.signConfirmLoading = true
          let params = {
            waybillNo: this.waybillNo,
            deptId: Number(this.signRuleForm.deptId),
          }
          putturnarounds(params).then(res => {
            if (res.code == 200) {
              this.signDialogVisible = false
              this.getList()
            }
            this.signConfirmLoading = false
          }).catch(() => {
            this.signConfirmLoading = false
          })
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      remoteSearch(value, type) {
        this.delivererName = value
        if (type == 'deliveryId') {
          this.personnelList.forEach(item => {
            if (item.field == 'deliveryId') {
              item.options = []
            }
          })
          this.getDelivererList()
        } else if (type == 'reCheckId') {
          this.personnelList.forEach(item => {
            if (item.field == 'reCheckId') {
              item.options = []
            }
          })
          this.getReviewerList()
        } else if (type == 'deptId') {
          this.signRuleList.forEach(item => {
            if (item.field == 'deptId') {
              item.options = []
            }
          })
          this.getAcquireStore()
        }
      },
      // 复核人
      getReviewerList() {
        var params = {
          page: 1,
          pageSize: 999,
          name: this.delivererName,
        }
        getUser(params).then(res => {
          if (res.code == 200) {
            let arr = res.data.list
            let arrList = []
            arr.forEach(item1 => {
              var arrData = {
                label: null,
                value: null,
              }
              arrData.label = item1.nickName
              arrData.value = item1.id
              arrList.push(arrData)
            })
            this.personnelList.forEach(item => {
              if (item.field == 'reCheckId') {
                item.options = item.options.concat(arrList)
              }
            })
          }
        })
      },
      getDelivererList() {
        var params = {
          page: 1,
          pageSize: 999,
          // type: 3,
          name: this.delivererName,
        }
        getUser(params).then(res => {
          if (res.code == 200) {
            let arr = res.data.list
            let arrList = []
            arr.forEach(item1 => {
              var arrData = {
                label: null,
                value: null,
              }
              arrData.label = item1.nickName
              arrData.value = item1.id
              arrList.push(arrData)
            })
            this.personnelList.forEach(item => {
              if (item.field == 'deliveryId') {
                item.options = item.options.concat(arrList)
              }
            })
          }
        })
      },
      // 获取门店
      getAcquireStore() {
        var params = {
          page: 1,
          pageSize: 999,
          // type: 3,
          name: this.delivererName,
        }
        getCompanyDept(params).then(res => {
          // signRuleList
          if (res.code == 200) {
            let arr = res.data.list
            let arrList = []
            arr.forEach(item1 => {
              var arrData = {
                label: null,
                value: null,
              }
              arrData.label = item1.name
              arrData.value = item1.id
              arrList.push(arrData)
            })
            this.signRuleList.forEach(item => {
              if (item.field == 'deptId') {
                item.options = item.options.concat(arrList)
              }
            })
          }
        })
      },
      // 处方更新
      prescription() {
        this.Pagination.PageIndex = 1
        this.getList()
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      // 清空表单
      closeDialog1() {
        this.$refs.signRules.resetCheck();
      },
      closeDialog() {
        this.srcList = []
        this.fileList = []
      },
    },
  }
</script>

<style lang="scss">
</style>
