<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"
        :wrapperCol="{ span: 8, offset: 2 }"
        :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"
        :scroll="{ x: 2500 }"
      >
        <template slot="operation" slot-scope="text, record">
          <!-- {{ text ? '禁用' : '启用' }} -->
          <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" />
    </p-modal>
  </div>
</template>

<script>
import config from './config.js'
import pick from 'lodash.pick'
import { add, deletes, update } from './api'
import { diffOption } from '@/views/kzyWMS/utils/index.js'
export default {
  data() {
    return {
      config: config(this),
      data: [],
      type: true,
      remoteParams: {},
      visible: false,
      confirmLoading: false,
    }
  },
  mounted() {
    // this.getinfo()
  },
  created() {
    diffOption(this.config.columns).then((res) => {
      this.columns = res
    })
  },
  methods: {
    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) => {
        if (!error) {
          console.log(error, values, 'valuesvaluesvaluesvaluesvalues')
          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.visible = false
              this.$message.success(res.message)
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
              this.visible = false
            }
          } 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.visible = false
            }
          }

          this.confirmLoading = false
          //
        }
      })
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
    },
    //点击弹出
    showmodel(data) {
      console.log(data)
      //   this.config.Modelitems = this.config.Modelitemsupdate1
      if (data && data.areaType == 20) {
        //接驳口
        this.config.Modelitems.forEach((item) => {
          //   item.name == 'gcsLocationNo' && (item.hidden = false) && (item.required = true)
          //   item.name == 'mcsLocationNo' && (item.hidden = false) && (item.required = true)
          //   item.name == 'rcsLocationNo' && (item.hidden = false) && (item.required = true)
          //   item.name == 'spsLocationNo' && (item.hidden = false) && (item.required = true)
          //   item.name == 'policy' && (item.hidden = true) && (item.required = false)
          //   item.name == 'locationStrategy' && (item.hidden = true) && (item.required = false)
          if (
            item.name == 'gcsLocationNo' ||
            item.name == 'mcsLocationNo' ||
            item.name == 'rcsLocationNo' ||
            item.name == 'spsLocationNo' ||
            item.name == 'x' ||
            item.name == 'y' ||
            item.name == 'rcsLocationNo'
          ) {
            item.hidden = false
            item.required = false
          }
          if (item.name == 'locationStrategy') {
            item.hidden = true
            item.required = false
          }
          if (item.name == 'pointX' || item.name == 'pointY') {
            item.hidden = true
            item.required = false
          }
        })
      } else if (data && data.areaType == 10) {
        //区域
        // this.config.Modelitems = this.config.Modelitemsupdate1
        this.config.Modelitems.forEach((item) => {
          if (
            item.name == 'gcsLocationNo' ||
            item.name == 'mcsLocationNo' ||
            item.name == 'rcsLocationNo' ||
            item.name == 'spsLocationNo' ||
            item.name == 'x' ||
            item.name == 'y' ||
            item.name == 'locationStrategy' ||
            item.name == 'rcsLocationNo'
          ) {
            item.hidden = true
            item.required = false
          }
          if (item.name == 'locationStrategy') {
            item.hidden = false
            item.required = true
          }
          if (item.name == 'pointX' || item.name == 'pointY') {
            item.hidden = false
            item.required = false
          }
        })
      }

      this.visible = true
      if (data) {
        this.config.Modelitems.forEach((item) => {
          item.name == 'areaType' && (item.disabled = true)
          item.name == 'areaNo' && (item.disabled = true)
        })
        this.$nextTick(() => {
          let keys = this.config.Modelitems.map((item) => item.name)
          console.log(this.config.Modelitems, keys, data, '====1')
          setTimeout(() => {
            this.$refs.model.form.setFieldsValue(pick(data, ...keys))
          })
        })
        this.type = true
      } else {
        this.config.Modelitemsupdate.forEach((item) => {
          item.disabled = false
        })
        this.config.Modelitems.forEach((item) => {
          item.disabled = false
        })
        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({ areaNo: text.areaNo })
          if (res.success) {
            that.$message.success('删除成功')
            that.remoteParams = { ...that.remoteParams }
          } else {
            that.$exception(res)
          }
        },
        onCancel() {},
      })
    },
  },
}
</script>

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