<template>
  <nm-container>
    <nm-list ref="list" v-bind="list">
      <!--查询条件-->
      <template v-slot:querybar> </template>

      <!--按钮-->
      <template v-slot:querybar-buttons>
        <nm-button v-bind="buttons.add" @click="add" />
      </template>

      <!--自定义列-->

      <template v-slot:col-materialTemplets="{ row }">
        <span>{{ row.materialTemplets[0].label }}</span>
      </template>

      <template v-slot:col-isLocked="{ row }">
        <v-chip v-if="row.isLocked === false" color="green" dark>未锁定</v-chip>
        <v-chip v-else color="red" dark>锁定</v-chip>
      </template>
      <template v-slot:col-isFrozen="{ row }">
        <v-chip v-if="row.isFrozen === false" color="green" dark>未冻结</v-chip>
        <v-chip v-else color="red" dark>冻结</v-chip>
      </template>

      <!--操作列-->
      <template v-slot:col-operation="{ row }">
        <nm-button v-bind="buttons.edit" @click="edit(row)" />
        <nm-button-delete v-bind="buttons.del" :id="row.id" :action="removeAction" @success="refresh" />
      </template>
    </nm-list>

    <save-page :id="curr.id" :visible.sync="dialog.save" @success="refresh" :materialTemplateId="materialTemplateNid" ref="savepage" />
    <form-create v-model="formModel" :rule="rule" :option="option" v-show="this.index_LoadMode"></form-create>
  </nm-container>
</template>
<script>
import { mixins } from 'xmanager-ui'
import page from './page'
import cols from './cols'
import SavePage from '../components/save'

const api = $api.materialApp.materialTemplateProperty

export default {
  name: page.name,
  mixins: [mixins.list],
  components: { SavePage },
  data() {
    return {
      materialTemplateNid: null,
      list: {
        title: '',
        cols,
        action: this.query,
        model: {
          /** 物料模板 */
          materialTemplate: '',
          /** 主键id */
          nId: '',
          /** 物料值 */
          propertyValue: '',
          /** 物料属性类型 */
          propertyType: '',
          /** 物料属性单位id */
          propertyUoMNId: ''
        },
        isVuetify: true,
        isSearch: true,
        isAdd: true,
        isExport: false,
        showSelect: true,
        singleSelect: false,
        noHeader: true
      },

      removeAction: api.remove,
      buttons: page.buttons,
      /**Code Manage 开始 */
      list_LoadMode: false,
      index_LoadMode: false,
      save_LoadMode: false,
      formModel: {},
      rule: [],
      option: {
        submitBtn: false
      }
    }
  },
  methods: {
    query() {
      if (this.materialTemplateNid == null || this.materialTemplateNid == '') {
        return new Promise(resolve => {
          api.query().then(datas => {
            let options = datas
            resolve(options)
          })
        })
      } else {
        return new Promise(resolve => {
          api.query({ MaterialTemplate: this.materialTemplateNid }).then(datas => {
            let options = datas
            resolve(options)
          })
        })
      }
    }
  },
  mounted() {},
  watch: {
    materialTemplateNid: function() {
      // console.log(val)
      this.$refs.list.query()
    }
  }
}
</script>
