<template>
  <div class="arrival_order">
    <Toptitle title="到货列表">
      <slot name="titleButton">
        <!-- <Button @click="handleCodeRule"
                type="primary"
                ghost
                style="margin-right:10px;">编码规则</Button> -->
        <Button type="primary"
                @click="handleGoPage(1,'')"
                style="margin-right:10px;">新增</Button>
      </slot>
    </Toptitle>
    <div class="arrival_order_search">
      <Form :label-width="90"
            :model="searchData">
        <FormItem label="订单号:">
          <Input type="text"
                 size="small"
                 v-model="searchData.order_no"
                 style="width: 150px"
                 placeholder="订单号" />
        </FormItem>
        <!-- <FormItem label="项目名称:">
          <Input type="text"
                 size="small"
                 v-model="searchData.project_title"
                 style="width: 150px"
                 placeholder="项目名称" />
        </FormItem> -->
        <FormItem label="提交人:">
          <Select v-model="searchData.user_id"
                  size="small"
                  clearable
                  style="width: 150px">
            <Option v-for="(sitem) in userList"
                    :key="sitem.id"
                    :label="sitem.nickname"
                    :value="sitem.id">
            </Option>
          </Select>
        </FormItem>
        <!-- <FormItem label="订单状态:">
          <Select v-model="searchData.lock"
                  size="small"
                  clearable
                  style="width: 150px">
            <Option label='正常'
                    :value=0 />
            <Option label='已关闭'
                    :value=1 />
          </Select>
        </FormItem> -->
        <FormItem label="审批状态:">
          <Select v-model="searchData.state"
                  size="small"
                  clearable
                  style="width: 150px">
            <Option label='未提交'
                    :value=0 />
            <Option label='审核中'
                    :value=1 />
            <Option label='通过'
                    :value=2 />
            <Option label='驳回'
                    :value=3 />
          </Select>
        </FormItem>
        <FormItem label="制单时间:">
          <DatePicker type="date"
                      size="small"
                      style="width: 150px"
                      placeholder="年/月/日"
                      v-model="searchData.start_time"></DatePicker>
        </FormItem>
        <FormItem label="~">
          <DatePicker type="date"
                      size="small"
                      style="width: 150px"
                      placeholder="年/月/日"
                      v-model="searchData.end_time"></DatePicker>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  size="small"
                  @click="initData(searchData)"
                  style="margin-right:10px;">搜索</Button>
        </FormItem>
      </Form>
      <div class="content_right_topfrom_btn">
      </div>
    </div>
    <div class="arrival_order_content">
      <div class="arrival_order_content_table">
        <Table :columns="tableColumns"
               border
               :max-height="500"
               :data="tableData">
          <template slot="setSlot"
                    slot-scope="{row,index}">
            <a style="margin:0 5px"
               @click="handleSet(2,row,index)">详情</a>
            <a style="margin:0 5px"
               :disabled="row.state==1||row.state==2"
               @click="handleSet(3,row,index)">编辑</a>
            <a style="margin:0 5px"
               :disabled="row.state==1||row.state==2"
               @click="handleSet(4,row,index)">删除</a>
            <a style="margin:0 5px"
               :disabled="row.state==1||row.state==2"
               @click="handleSet(5,row,index)">提交</a>
          </template>
        </Table>
        <div class="arrival_order_content_page">
          <Page :page-size-opts="[10, 20, 30, 40,100]"
                @on-page-size-change='changeSize'
                @on-change='changePage'
                :current='pageIndex'
                show-total
                :total="total"
                show-sizer
                :page-size='pageSize' />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';

export default {
  name: '',
  components: {

  },
  props: {},
  // import引入的组件需要注入到对象中才能使用
  data () {
    // 这里存放数据
    return {
      searchData: {
        order_no: '',
        project_title: '',
        supply_title: '',
        type_id: '',
        user_id: '',
        lock: '',
        state: '',
        start_time: '',
        end_time: '',
      },
      tableColumns: [
        { title: '订单号', key: 'purchase_order_arrive_no', align: 'center', minWidth: 140 },
        // { title: '项目名称', key: 'client_name', align: 'center', minWidth: 140 },
        { title: '供应商名称', key: 'supply_title', align: 'center', minWidth: 140 },
        {
          title: '订单状态', key: 'lock', align: 'center', minWidth: 120,
          render: (h, params) => {
            const { row } = params
            const text = row.lock == 0 ? '正常' : '已关闭'
            return h('span', {}, text)
          }
        },
        { title: '提交人', key: 'nickname', align: 'center', minWidth: 100 },
        {
          title: '审批状态', key: 'state', align: 'center', minWidth: 140,
          render: (h, params) => {
            const { row } = params
            const text = row.state == 0 ? '未提交' : row.state == 1 ? '审核中' : row.state == 2 ? '通过' : '驳回'
            return h('span', {}, text)
          }
        },
        {
          title: '预计到货日期', key: 'arrive_time', align: 'center', minWidth: 140,
          render: (h, params) => {
            const { row } = params
            return h('span', {}, this.func.replaceDateNoHMS(row.arrive_time))
          }
        },
        {
          title: '制单日期', key: 'crt_time', align: 'center', minWidth: 140,
          render: (h, params) => {
            const { row } = params
            return h('span', {}, this.func.replaceDate(row.crt_time))
          }
        },
        { title: '操作', key: 'code', align: 'center', minWidth: 200, slot: 'setSlot' },
      ],
      tableData: [{}],
      pageIndex: 1,
      pageSize: 10,
      total: 0,
      userList: [],
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    // 获取操作员
    this.axios('/api/user').then(res => this.userList = res.data.data)
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.initData()
  },
  methods: {
    initData () {
      let obj = JSON.parse(JSON.stringify(this.searchData))
      obj.start_time = parseInt(new Date(obj.start_time).getTime() / 1000) || ''
      obj.end_time = parseInt(new Date(obj.end_time).getTime() / 1000) || ''
      this.axios({
        method: 'get',
        url: '/api/purchase_arrive_list',
        params: {
          ...obj,
          page_index: this.pageIndex,
          page_size: this.pageSize
        }
      }).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total
      }).catch((err) => { });
    },
    handleSet (type, row, index) {
      // 1新增 2详情 3编辑 4删除 5提交
      switch (type) {
        case 1:
        case 2:
        case 3:
          this.handleGoPage(type, row.purchase_order_arrive_no)
          break;
        case 4:
          this.$Modal.confirm({
            title: '确认删除？',
            content: '此操作无法恢复，请确认！',
            onOk: () => {
              this.axios({
                method: 'post',
                url: '/api/purchase_arrive_del',
                data: {
                  purchase_order_arrive_no: row.purchase_order_arrive_no
                }
              }).then((res) => {
                this.$Message.success(res.msg)
                this.initData()
              }).catch((err) => { });
            },
            onCancel: () => { }
          })
          break;
        case 5:
          this.$Modal.confirm({
            title: '确认提交？',
            onOk: () => {
              this.axios({
                method: 'post',
                url: '/api/purchase_arrive_pull',
                data: {
                  purchase_order_arrive_no: row.purchase_order_arrive_no,
                  lock: row.lock,
                  state: row.state + 1
                }
              }).then((res) => {
                this.$Message.success(res.msg)
                this.initData()
              }).catch((err) => { });
            },
            onCancel: () => { }
          })
          break;
      }
    },
    handleGoPage (type, purchase_order_arrive_no) {
      this.$router.push({
        path: '/cms/PurchasingManage/ArrivalOrder/edit',
        query: {
          type,
          purchase_order_arrive_no
        }
      })
    },
    changeSize (e) {
      this.pageSize = e;
      this.initData()
    },
    changePage (e) {
      this.pageIndex = e;
      this.initData()
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  beforeCreate () { }, // 生命周期 - 创建之前
  beforeMount () { }, // 生命周期 - 挂载之前
  beforeUpdate () { }, // 生命周期 - 更新之前
  updated () { }, // 生命周期 - 更新之后
  beforeDestroy () { }, // 生命周期 - 销毁之前
  destroyed () { }, // 生命周期 - 销毁完成
  activated () { }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
.arrival_order_search {
  padding-top: 20px;
  /deep/.ivu-form {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  /deep/.ivu-form-item {
    display: inline-block;
    width: 250px;
  }
}
.arrival_order_content {
  border-top: 1px solid #f4f4f4;
  margin-top: 20px;
  .arrival_order_content_btn {
    display: flex;
    justify-content: flex-end;
    padding: 20px 0;
  }
  .arrival_order_content_page {
    display: flex;
    justify-content: center;
    padding-top: 20px;
  }
}
</style>
