<template>
  <div>
    <!-- 出库任务汇总单拆单策略配置 -->
    <p-card title="出库任务汇总单拆单策略配置">
      <span slot="extra">
        <p-button type="primary" style="margin-right:10px" @click="showmodel()">添加</p-button>
      </span>
      <p-grid-form :label-col="{ span: 8 }" :span="8" :items="config.items" @submit="hanlderSubmit" />
      <p-table
        ref="table"
        row-key="id"
        bordered
        :columns="config.columns"
        :remoteUrl="config.remoteUrl"
        :remoteParams="remoteParams"
        :rowSelectType="false"
        :doneBefore="doneBefore"
        :data-source="data"
      >
        <!-- <template slot="StartingArea" slot-scope="text, record">
          <a @click="DetailSourceArea(record)">查看</a>
        </template>
        <template slot="EndinggArea" slot-scope="text, record">
          <a @click="DetailTargetArea(record)">查看</a>
        </template> -->
        <template slot="operation" slot-scope="text, record">
          <!-- {{ text ? '禁用' : '启用' }} -->
          <a @click="DetailSourceArea(record)">策略明细</a>
          <a-divider type="vertical" />
          <a @click="showmodel(text)">策略修改</a>
          <a-divider type="vertical" />
          <a @click="del(text)">删除</a>
        </template>
      </p-table>
    </p-card>

    <p-modal
      title="详情"
      width="50%"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-grid-form :label-col="{ span: 8 }" ref="model" :span="8" :items="config.Modelitems" @submit="hanlderSubmit" />
    </p-modal>
    <!-- 
    <p-modal
      title="区域配置"
      width="80%"
      :visible="visibleArea"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >

    </p-modal> -->

    <p-modal
      :title="title"
      width="80%"
      :visible="visibleArea"
      :confirm-loading="confirmLoading"
      @ok="handleOkEdit"
      @cancel="handleCancel"
    >
      <p-button @click="addrow">添加</p-button>
      <p-table-edit-pro
        ref="tableEdit"
        auto-width
        dragle
        editor-row-table
        :columns="columns"
        :data-source.sync="tableData"
        :rowSelectType="false"
        :bordered="true"
        :scroll="{ x: 1000 }"
      >
        <template slot="operation" slot-scope="text, record">
          <a @click="delEditTable(record)">删除</a>
        </template>
      </p-table-edit-pro>
    </p-modal>
  </div>
</template>

<script>
import config from './config.js'

import { add, deletes, update, Detailapi, EditSourceArea, EditTargetArea, DetailTargetAreaapi } from './api'
import { area } from './areaNoSaerchModel'

//  {
//   // labelname = '区域', span = 24, required = true, name = 'startRegion'
//   ...area('起始区域', 6, false, 'sourceArea')
// },
export default {
  data() {
    return {
      config: config(this),
      data: [],
      type: true,
      title: '',
      columns: [
        {
          // labelname = '区域', span = 24, required = true, name = 'startRegion'
          ...area('区域编号', 6, true, 'sourceArea')
        },
        {
          type: 'edit',
          dataIndex: 'splitStrategy',
          title: '拆单策略',
          width: 200,
          required: true
        },
        {
          type: 'number',
          dataIndex: 'sortIndex',
          title: '排序',
          width: 200,
          required: true
        },
        {
          type: 'txt',
          dataIndex: 'code',
          title: '平均出库时间',
          width: 200
        },
        {
          title: '操作',
          key: 'operation',
          scopedSlots: { customRender: 'operation' },

          align: 'center'
        }
      ],
      tableData: [],
      remoteParams: {
        // containerTaskTypeNo: '',
        // typeName: ''
        // startTime: '',
        // endTime: ''
      },
      visible: false,
      visibleArea: false,
      confirmLoading: false
    }
  },
  mounted() {
    // this.getinfo()
  },
  methods: {
    handleOkEdit() {
      let that = this
      this.$refs.tableEdit.validateFields(async (err, value) => {
        if (!err) {
          console.log(value)
          //   value = value.map(item => item.areaNo)
          console.log(value)
          let data = {
            areaList: value,
            id: this.Areadetails.id
          }
          console.log(data)

          let res = await EditSourceArea(data)

          if (res.success) {
            this.visibleArea = false
            this.remoteParams = { ...this.remoteParams }
          } else {
            this.$exception(res)
          }
        }
      })
    },
    //添加行
    addrow() {
      this.$refs.tableEdit.createRow({ subAreaId: this.subAreaId })
    },
    //删除行
    delEditTable(record) {
      console.log(record, record._row_count - 1)
      this.tableData.splice(record._row_count - 1, 1)
    },
    //查看容器任务起点 详情
    async DetailSourceArea(data) {
      console.log(data)
      this.title = '起点区域配置'
      this.Areadetails = data
      let res = await Detailapi({ id: data.id })
      console.log(res)
      if (res.success) {
        this.visibleArea = true
        this.tableData = res.data
      } else {
        this.$exception(res)
      }
    },

    doneBefore(res) {
      //   res.data.list.forEach(item => {
      //     item.id = item.areaNo
      //   })
      console.log(res)
      return res
    },
    hanlderSubmit(e, form) {
      e.preventDefault()
      form.validateFields((error, values) => {
        console.log(error, values, 'valuesvaluesvaluesvaluesvalues')
        // this.queryName = values.name
        // this.queryType = values.type
        // this.getlistCompanyByAuthId()
        if (!error) {
          this.remoteParams = { ...this.remoteParams, ...values }
        }
      })
    },
    handleOk(e) {
      console.log(this.$refs.model)
      this.$refs.model.form.validateFields(async (err, value) => {
        if (!err) {
          console.log(value)
          this.confirmLoading = true
          if (this.type) {
            let res = await update(value)
            console.log(res)
            if (res.success) {
              this.$message.success(res.message)
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          } else {
            let res = await add(value)
            console.log(res)

            if (res.success) {
              this.$message.success(res.message)
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          }

          this.confirmLoading = false
          //
        }
      })
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
      this.visibleArea = false
    },
    //点击弹出
    showmodel(data) {
      console.log(data)
      this.visible = true
      if (data) {
        this.$nextTick(() => {
          this.$refs.model.form.setFieldsValue(data)
        })
        this.type = true
      } else {
        console.log(this.config.Modelitems)
        this.$nextTick(() => {
          this.$refs.model.form.resetFields()
        })
        this.type = false
      }
    },
    del(text) {
      let that = this
      console.log(text)
      this.$confirm({
        title: '确定是否要删除?',
        content: '',
        async onOk() {
          let res = await deletes({ id: text.id })
          if (res.success) {
            that.$message.success('删除成功')
            that.remoteParams = { ...that.remoteParams }
          } else {
            that.$exception(res)
          }
        },
        onCancel() {}
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
